问题
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