Xamarin PhoneDialer.Open with firebase realtime database

Viewed 72

When I try to use PhoneDialer.Open, the application crashes. I am trying to pull the phone number from firebase realtimedatabase.

                                <Image Source="phone.png"
                                   HeightRequest="20"
                                   WidthRequest="20" Margin="10,0,0,0">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="CallTap_Tapped"
                                                          CommandParameter="{Binding Tel}"/>
                                </Image.GestureRecognizers>

                            </Image>

and class

            public void CallTap_Tapped(object sender, EventArgs e)
        {
                string telTaxi = new TaxiSlovenskoModel().Tel;

                PhoneDialer.Open(telTaxi);

        }

UPDATE

Example: TaxiSlovenskoModel:

public class TaxiSlovenskoModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Kraj { get; set; }
    public string Mesto { get; set; }
    public string Tel { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
}

TaxiListPage.xaml:

 <Image Source="phone.png"
                                   HeightRequest="20"
                                   WidthRequest="20" Margin="10,0,0,0">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="CallTap_Tapped"
                                                          CommandParameter="{Binding Tel}"/>
                                </Image.GestureRecognizers>

                            </Image>

and TaxiListPage.xaml.cs:

        TaxiRepository taxiRepository = new TaxiRepository();
    public TaxiListPage()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
       var taxies = await taxiRepository.GetAll();
        TaxiListView.ItemsSource = taxies;
    }

    private void AddToolBarItem_Clicked(object sender, EventArgs e)
    {
        Navigation.PushModalAsync(new TaxiEntry());
    }

    private void TaxiListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        if (e.Item == null)
        {
            return;
        }
        var taxi = e.Item as TaxiSlovenskoModel;
        Navigation.PushModalAsync(new TaxiDetail(taxi));
        ((ListView)sender).SelectedItem = null;
       
    }

        public void CallTap_Tapped(object sender, EventArgs e)
        {
                string telTaxi = new TaxiSlovenskoModel().Tel;

                PhoneDialer.Open(telTaxi);

        }

Database: enter image description here

In APP: enter image description here

1 Answers

The reason this is happening is probably that you are missing your Manifest query that you need if you Target Android 11:

  <queries>
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel"/>
  </intent>
</queries>

More information here: https://docs.microsoft.com/en-us/xamarin/essentials/phone-dialer?tabs=android

UPDATE:

So the issue is that you are creating a new object instead of actually using your existing object. your call method should look as follows:

public void CallTap_Tapped(object sender, EventArgs e)
    {
       var tappEvent = (TappedEventArgs)e;
       var telTaxi = tappEvent?.Parameter.ToString();              
       if(!string.IsNullOrWhiteSpace(telTaxi))           
        PhoneDialer.Open(telTaxi);
    }

Good luck

Related