如何通过单击标签在 Xamarin.Forms 中拨打电话? [英] How to make a phone call in Xamarin.Forms by clicking on a label?

查看:17
本文介绍了如何通过单击标签在 Xamarin.Forms 中拨打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello I have an app i'm working on in Xamarin.Forms that gets contact info from a web service and then displays that info in labels however I want to make the label that lists the phone number to make a call when clicked. How do I go about doing this?

在我的 XAML 中:

Heres in my 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;





        }
    }
}

提前致谢!

推荐答案

Label 不是交互式的,因此您需要使用 Gesture 使其响应点击:

A Label is not interactive, so you need to use a Gesture to make it respond to taps:

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 插件:

to make a phone call, you can either use the built in Device.OpenUri() method with a "tel:1234567890" argument, or use the Messaging plugin:

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

这篇关于如何通过单击标签在 Xamarin.Forms 中拨打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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