如何通过点击标签在Xamarin.Forms中拨打电话?

时间:2016-05-31 16:56:59

标签: c# xamarin xamarin.android xamarin.forms

您好我有一个应用程序我正在使用Xamarin.Forms从Web服务获取联系信息,然后在标签中显示该信息但是我想制作一个标签,列出电话号码,以便在点击时拨打电话。我该怎么做呢?

我的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="ReadyMo.ContactInfo">
  <ContentPage.Content>
     <Frame Padding="0,0,0,8" BackgroundColor="#d2d5d7">
            <Frame.Content>
              <Frame Padding="15,15,15,15"   OutlineColor="Gray" BackgroundColor="White">
                <Frame.Content>
        <ScrollView Orientation="Vertical" VerticalOptions="FillAndExpand">
           <StackLayout Padding="20,0,0,0"  Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
            <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand">
              <Label Text="Emergency Coordinators"  HorizontalOptions="Center" FontFamily="OpenSans-Light"
                                       FontSize="20"
                                       TextColor="#69add1">
              </Label>
              <Label x:Name="CountyName"   HorizontalOptions="Center" FontFamily="OpenSans-Light"
                                   FontSize="16"
                                   TextColor="#69add1">
              </Label>
              <Label x:Name="FirstName" HorizontalOptions="Center">
              </Label>
              <Label x:Name ="LastName" HorizontalOptions="Center">
              </Label>
              <Label x:Name="County" HorizontalOptions="Center">
              </Label>
              <Label x:Name ="Adress" HorizontalOptions="Center">
              </Label>
                <Label x:Name ="City" HorizontalOptions="Center">
              </Label>

//This is the label that displays the phone number!
              <Label x:Name="Number"  HorizontalOptions="Center">
              </Label>           
            </StackLayout>
          </StackLayout>
        </ScrollView>
       </Frame.Content>
      </Frame>
     </Frame.Content>
    </Frame>
  </ContentPage.Content>
</ContentPage>

我的代码背后是:

using Newtonsoft.Json;
using ReadyMo.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace ReadyMo
{
    public partial class ContactInfo : ContentPage
    {
        private County item;

        public ContactInfo()
        {
            InitializeComponent();
            var contactpagetext = ContactManager.GetContactString(item.id);

        }

        public ContactInfo(County item)
        {
            InitializeComponent();
            this.item = item;

            //var contactpagetext = ContactManager.GetContactString(item.id).Result;
            //Emergency Coordinators Code
            ContactInfoModel TheContactInfo = ContactManager.CurrentContactInfo;
            CountyName.Text = TheContactInfo.name;
            FirstName.Text = TheContactInfo.First_Name;
            LastName.Text = TheContactInfo.Last_Name;
            Adress.Text = TheContactInfo.Address1;
            City.Text = TheContactInfo.Address2;
            Number.Text = TheContactInfo.BusinessPhone;





        }
    }
}

提前致谢!

4 个答案:

答案 0 :(得分:19)

标签不是交互式的,因此您需要使用手势使其响应水龙头:

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};

// attache the gesture to your label
number.GestureRecognizers.Add(tapGestureRecognizer);

拨打电话,您可以使用内置的Device.OpenUri()方法和“tel:1234567890”参数,也可以使用Messaging插件:

var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall) 
    phoneDialer.MakePhoneCall("+272193343499");

答案 1 :(得分:11)

快速使用Xamarin Forms手机拨号应用的快速摘录:

alert

答案 2 :(得分:2)

Xamarin Essentials PhoneDialer

public void PlacePhoneCall(string number)
    {
        try
        {
            PhoneDialer.Open(number);
        }
        catch (ArgumentNullException anEx)
        {
            // Number was null or white space
        }
        catch (FeatureNotSupportedException ex)
        {
            // Phone Dialer is not supported on this device.
        }
        catch (Exception ex)
        {
            // Other error has occurred.
        }
    }

答案 3 :(得分:1)

Device.OpenUri()已过时。使用Xamarin.Essentials

Launcher.OpenAsync("tel:" + PhoneNumber);