2016-05-31 67 views
3

你好我有一個應用程序,我正在Xamarin.Forms中獲取來自Web服務的聯繫信息,然後在標籤中顯示該信息,但是我想使標籤列出電話號碼以便在點擊時撥打電話。我如何去做這件事?如何通過點擊標籤在Xamarin.Forms中打電話?

繼承人在我的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; 





     } 
    } 
} 

提前感謝!

回答

9

標籤是沒有互動,所以你需要使用一個手勢,使其應對水龍頭:

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

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

來打個電話,你可以使用內置的Device.OpenUri()方法與「電話:1234567890" 的說法,或者使用Messaging插件:

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

賈森感謝您的快速回復唯一的問題是,數字的變化取決於它是如何「phoneDialer.MakePhoneCall(」+ 272193343499「)」;「將無法使用它必須調用Number.text中的內容我該如何去做這件事? – Phoneswapshop

+0

如果你想讓它撥打標籤中的值,那麼只需使用Number.Text代替硬編碼的字符串 – Jason

+0

工作正常!非常感謝! – Phoneswapshop

2

快速片段,快使用手機的撥號程序從Xamarin形式:

var CallUsLabel = new Label { Text = "Tap or click here to call" }; 
    CallUsLabel.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => { 
      Device.OpenUri(new Uri("tel:038773729")); 
     }) }); 
相關問題