首页 > 解决方案 > 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

标签: c#xamarin.formsxamarin.iosxamarin.androidvisual-studio-2017

解决方案


Xamarin 表单具有相同的直接方法:

   Device.OpenUri("tel:738284739");

推荐阅读