-2

How to make a phone call in Xamarin Forms by clicking on a label? I know this question is cliche because someone already asked it here,

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

I can't ask in that post because it requires 50 reputation to comment. But I still need to know the answer because that post didn't show the final code. So here's my code,

RequestorPage.xaml

<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Margin="10,0,10,0">
      <StackLayout Orientation="Vertical" HorizontalOptions="Start">
          <Image Source="detail_mobile" HeightRequest="20" WidthRequest="20" />
      </StackLayout>
      <StackLayout Orientation="Vertical" VerticalOptions="Center">
          <Label FontSize="Small" Text="{Binding RequestorHp}" x:Name="reqHp" />
      </StackLayout>
</StackLayout>

RequestorPage.xaml.cs

public RequestorPage()
        {
            InitializeComponent();

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) =>
            {
                // handle the tap
                var phoneDialer = CrossMessaging.Current.PhoneDialer;
                if (phoneDialer.CanMakePhoneCall)
                {
                    phoneDialer.MakePhoneCall(reqHp.Text);
                }
            };

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

When I run in the simulator, error appears when I click RequestorHp label. Here's the error,

System.NotImplementedException has been thrown

This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.

Which part did I miss? Please help me. Thank you in advanced.

nuyuljana
  • 59
  • 10

1 Answers1

0

You need to use a dependency service to run the MakePhoneCall() method inside the iOS and Android Projects from your shared project.

In your Xamarin.Forms Project, you can add the following code to make the call to the dependency service:

switch (Device.RuntimePlatform)
{
    case Device.Android:
        DependencyService.Get<IAndroidMethods>().startPhoneCall();
        break;
    case Device.iOS:
        DependencyService.Get<IAppleMethods>().startPhoneCall();
        break;
}

Next, in your Xamarin.Forms project, create two interfaces: IAppleMethods and IAndroidMethods that contain the startPhoneCall() method.

I'll show the implementation for IAndroidMethods:

public interface IAndroidMethods
{
    void startPhoneCall(string number);
}

Then, in your iOS and Android projects, create classes that implement the interfaces you just created. This would be your android class:

//add appropriate using declarations
[assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))]
namespace <YourApp>.Droid
{
    public class AndroidMethods : IAndroidMethods
    {
        public void startPhoneCall(string number)
        {
            var phoneDialer = CrossMessaging.Current.PhoneDialer;
            if (phoneDialer.CanMakePhoneCall)
            {
                phoneDialer.MakePhoneCall(number);
            }
        }
    }
}
cvanbeek
  • 1,796
  • 21
  • 30