在.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
本文介绍如何在.NETMAUI应用中实现拨打电话的功能。通过在AndroidManifest.xml中配置<queries>元素并使用PhoneDialer.Default.Open方法来启动拨号界面。文章还展示了错误处理的方法。
887

被折叠的 条评论
为什么被折叠?



