Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a phone call in Xamarin.Forms by clicking on a label?

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?

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>

heres my code behind:

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;





        }
    }
}

Thanks in advance!

like image 747
Phoneswapshop Avatar asked May 31 '16 16:05

Phoneswapshop


People also ask

How do I make a call in Xamarin form?

using Xamarin. Essentials; The Phone Dialer functionality works by calling the Open method with a phone number to open the dialer with. When Open is requested the API will automatically attempt to format the number based on the country code if specified.

What is label in Xamarin forms?

A Label is used to display single-line text elements as well as multi-line blocks of text. The following example, adapted from the default Xamarin.Forms solution, shows a basic use: C# Copy.

How do you underline a label in Xamarin?

Firstly we create a ContentView whose name is under UnderLineLabel. xaml and then set the Label and Boxview. Then we are using the BindableProperty to the code behind. TADAAA!


2 Answers

A quick snippet that is quick to use the phone's dialer app from 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")); // Deprecated
           Launcher.OpenAsync("tel:038773729");
        }) });
like image 162
noelicus Avatar answered Sep 22 '22 22:09

noelicus


Device.OpenUri() is obsolete. Use Xamarin.Essentials:

Launcher.OpenAsync("tel:" + PhoneNumber);
like image 41
Edgaras Avatar answered Sep 22 '22 22:09

Edgaras