Calling Naviation.PushAsync from CustomMapRenderer when clicked on a marker

妖精的绣舞 提交于 2020-01-05 04:13:10

问题


I am working on a xamarin project for ios aswel as android where we are showing custom markers on the Xamarin.Forms maps, we want the user to navigate to another view when he clicks 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 platformspecific code. Wich is where the customMapRenderers are and where the onlclick 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();
    }

回答1:


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");


来源:https://stackoverflow.com/questions/44406120/calling-naviation-pushasync-from-custommaprenderer-when-clicked-on-a-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!