我想导航到 Xamarin Forms 中的其他页面,但它在内容 View 中不起作用。我正在使用
await Navigation.PushAsync(new Page2());
此代码没有给出任何错误并成功运行,但对页面没有影响。我也在内容页面中使用此代码,它在那里正常工作,但在 ContentView 的情况下不起作用。 目前我正在使用
await ParentView.Navigation.PushAsync(new Page2());
它在 ContentView 中运行良好。但我收到警告“ParentView 从 2.1.0 版开始已过时。请改用 Parent。”。但是没有类似的功能
await Parent.Navigation.PushAsync(new Page2());
它给出了语法错误。那么,有没有其他方法可以使用 ContentView 导航到其他页面。
最佳答案
如果您应用的 MainPage
是一个 NavigationPage
,那么您始终可以从应用内部调用 PushAsync()
。我建议检查 MainPage
是否属于 NavigationPage
类型,以确保您不会抛出强制转换异常。为此,只需使用这两行:
if(App.Current.MainPage is NavigationPage)
(App.Current.Mainpage as NavigationPage).PushAsync(new Page2());
关于c# - Xamarin.Forms Navigation.PushAsync 在 ContentView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341128/