web-dev-qa-db-fra.com

Comment passer un appel téléphonique dans Xamarin.Forms en cliquant sur une étiquette?

Bonjour, j'ai une application sur laquelle je travaille dans Xamarin.Forms qui obtient les informations de contact d'un service Web, puis affiche ces informations dans des étiquettes, mais je veux créer l'étiquette qui répertorie le numéro de téléphone pour passer un appel lorsque vous cliquez dessus. Comment dois-je procéder?

Heres dans mon 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>

voici mon code derrière:

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;





        }
    }
}

Merci d'avance!

11
Phoneswapshop

Un label n'est pas interactif, vous devez donc utiliser un geste pour le faire répondre aux taps:

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

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

pour passer un appel téléphonique, vous pouvez soit utiliser la méthode intégrée Device.OpenUri () avec un argument "tel: 1234567890", soit utiliser le plug-in Messaging :

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

Un extrait rapide qui est rapide à utiliser l'application de numérotation du téléphone de Xamarin Forms:

    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"));
        }) });
14
noelicus

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.
        }
    }
0