How to make a phone call in Xamarin.Forms by clicking on a label?
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.
}
}
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);
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");
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");
}) });
Device.OpenUri()
is obsolete. Use Xamarin.Essentials
:
Launcher.OpenAsync("tel:" + PhoneNumber);