xamarin 单击标记时从CustomMapRenderer调用Naviation.PushAsync

egmofgnx  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(99)

我正在为iOS和Android开发一个xamarin项目,我们在Xamarin上显示自定义标记。表单Map,我们希望用户在点击自定义标记时导航到另一个视图。
我们使用Navigation.PushAsync在视图中导航。这是从viewmodels从非平台特定的代码和导航完成的。PushAsync只能在那里使用,而不能从平台特定的代码使用。这是customMapRenderers所在的位置,也是处理标记onclick的位置。
所以我的问题是我如何从这些onClick事件导航到另一个视图?下面是捕获onclick的方法。
安卓系统:

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();
    }
wfsdck30

wfsdck301#

正如Yuri所说,你可以通过使用Messenger来实现这一点,这里有一个关于如何使用Messengersample。您可以在custom renderer onClick事件中使用它,当在renderer中发送message时,Page将收到Message。因此,您可以在Page的MessagingCenter.Subscribe方法中导航到另一个视图。
页码:

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

自定义渲染器:

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

相关问题