xamarinxamarin.iosxamarin.androidxamarin.formscustom-renderer

Calling Naviation.PushAsync from CustomMapRenderer when clicked on a marker


I am working on a xamarin project for iOS as well as Android where we are showing custom markers on the Xamarin.Forms maps, we want the user to navigate to another view when they click on a custom marker.

We are using Navigation.PushAsync to navigate through the views. this is done from the viewmodels from the non platform specific code, and navigation.PushAsync can only be used there and not from the platform specific code. Which is where the customMapRenderers are and where the onclick for the markers is handled.

So my question is how can I navigate to another view from these onClick Events? underneath are the methods that catch the onclick.

Android:

private void OnMarkerClick(object sender, GoogleMap.MarkerClickEventArgs e)
        {
            Toast.MakeText(MainApplication.Context, "Button Pressed", ToastLength.Long).Show();
        }

iOS:

private void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
    {
        UIAlertView alert = new UIAlertView()
        {
            Title = "Event",
            Message = "Button Clicked"
        };
        alert.AddButton("Oke");
        alert.Show();
    }

Solution

  • As Yuri said, you can implement this by using Messenger, here is a sample about how to use Messenger. You can use it in custom renderer onClick Events, When send a message in renderer, the Page will received Message. So you can navigate to another view in your Page's MessagingCenter.Subscribe method.

    Page:

    MessagingCenter.Subscribe<MainPage>(this, "Navigation", async (sender) =>
    {
         var page1 = new Page1();
         await Navigation.PushModalAsync(page1);
    });
    

    Custom Renderer:

    MessagingCenter.Send<MainPage>(MainPage.getInstance(), "Navigation");