在.NET MAUI中调用拨号界面
前置要求:
Visual Studio 2022
安装包“.NET Multi-platform App UI 开发”
- 新建一个MAUI项目
- 在解决方案资源管理器窗口中找到
Platforms/Android/AndroidManifest.xml
- 在AndroidManifest.xml中添加下文中
<queries>...</queries>
块如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<queries>
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel"/>
</intent>
</queries>
</manifest>
- 在代码中如下调用,重点是
PhoneDialer.Default.Open
try
{
if (PhoneDialer.Default.IsSupported)
PhoneDialer.Default.Open(phoneNumber);
}
catch (ArgumentNullException)
{
await DisplayAlert("Unable to dial", "Phone number was not valid.", "OK");
}
catch (Exception)
{
// Other error has occurred.
await DisplayAlert("Unable to dial", "Phone dialing failed.", "OK");
}
当phoneNumber为111111时,效果如下:
注:Microsoft.Maui.ApplicationModel.Communication.PhoneDialer