Intent ActionCall 不以 xamarin 形式拨打电话 [英] Intent ActionCall is not making phone call in xamarin forms

查看:36
本文介绍了Intent ActionCall 不以 xamarin 形式拨打电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在单击按钮时拨打某个电话号码的应用程序我正在使用 Xamarin Forms 来执行此操作,这是我的代码:

I am working on a app that needs to call a certain phone number when the button is clicked i am using Xamarin Forms to do this here's my code:

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();
            }
        }

    }
}

如何在单击按钮时进行操作调用?我收到以下错误:

How to make an Action Call when clicking on Button? I'm getting the following error:

System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid

目标框架:.NET Standard 2.0视觉工作室 2017

Target Framwork:.NET Standard 2.0 VisualStudio 2017

推荐答案

Xamarin 表单有一个直接的方法:

Xamarin forms has a direct method for the same :

   Device.OpenUri("tel:738284739");

这篇关于Intent ActionCall 不以 xamarin 形式拨打电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆