c# - Intent ActionCall 没有以 xamarin 形式拨打电话
问题描述
我正在开发一个应用程序,该应用程序需要在单击按钮时拨打某个电话号码我正在使用 Xamarin Forms 来执行此操作,这是我的代码:
IPhoneCall
界面:
using System;
using System.Collections.Generic;
using System.Text;
namespace DirectCall.Model
{
public interface IPhoneCall
{
void MakeQuickCall(string PhoneNumber);
}
}
PhoneCallPage.xaml
:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DirectCall.View.PhoneCallPage">
<ContentPage.Content>
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout>
<Entry Placeholder="Enter the Your Phone Number" x:Name="Txt_PhoneNumber" WidthRequest="100" HeightRequest="40"/>
<Button Text="ActionCall" x:Name="Btn_ActionCall" Clicked="Btn_ActionCall_Clicked" WidthRequest="100" HeightRequest="40"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
PhoneCallPage.xaml.cs
:
using DirectCall.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace DirectCall.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PhoneCallPage : ContentPage
{
public PhoneCallPage ()
{
InitializeComponent ();
}
private void Btn_ActionCall_Clicked(object sender, EventArgs e)
{
try
{
if (Txt_PhoneNumber.Text == "")
{
DisplayAlert("Alert", "Specify the number to start the call.", "OK");
}
else
{
DependencyService.Get<IPhoneCall>().MakeQuickCall(Txt_PhoneNumber.Text.ToString());
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
PhoneCall_Droid
接口实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using DirectCall.Droid;
using DirectCall.Model;
using Xamarin.Forms;
[assembly: Dependency(typeof(PhoneCall_Droid))]
namespace DirectCall.Droid
{
public class PhoneCall_Droid : IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
var uri = Android.Net.Uri.Parse(string.Format("tel:+91{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
}
单击按钮时如何进行操作调用?我收到以下错误:
System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid
目标框架:.NET Standard 2.0 VisualStudio 2017
解决方案
Xamarin 表单具有相同的直接方法:
Device.OpenUri("tel:738284739");
推荐阅读
- windows - Azure 文件共享用户特定的访问权限
- laravel - 发出 Illuminate....Http 请求后等待很长时间并且没有任何错误
- joomla - DT Register 是否与 Joomla 核心用户自定义字段集成?
- javascript - 如何检查新创建的对象是否与数组内的对象相同
- javascript - useReducer 钩子不起作用,如何解决?
- delphi - VirtualStringTree 可以避免将数据附加到节点吗?
- swift - UITableView 中的预览标签运行两次
- python - 如何将 opentelmetry 与多个微服务一起使用(python)
- wordpress - 检查类别是否包含在 WooCommerce 中销售的任何产品?
- vue.js - quasar [vuex] 未知动作类型:loadUsers