简体   繁体   English

Xamarin.Forms Navigation.PushAsync在ContentView中不起作用

[英]Xamarin.Forms Navigation.PushAsync not working in ContentView

I want to navigate to other page in Xamarin Forms but it is not working in Content View. 我想导航到Xamarin Forms中的其他页面,但它在内容视图中不起作用。 I am using 我在用

await Navigation.PushAsync(new Page2()); 等待Navigation.PushAsync(new Page2());

This code is not giving any error and run successfully but there is no effect on page. 此代码未给出任何错误并成功运行,但对页面没有影响。 I am using this code in content page also and it works properly there but it is not working in case of ContentView. 我也在内容页面中使用此代码,它在那里正常工作,但在ContentView的情况下它不起作用。 Currently I am using 目前我正在使用

await ParentView.Navigation.PushAsync(new Page2()); 等待ParentView.Navigation.PushAsync(new Page2());

and it is working fine in ContentView. 它在ContentView中运行良好。 But I am getting warning that "ParentView is obsolete as of version 2.1.0. Please use Parent instead.". 但是我收到警告“从2.1.0版本开始,ParentView已经过时了。请改用Parent。”。 But there is no function like 但是没有像这样的功能

await Parent.Navigation.PushAsync(new Page2()); 等待Parent.Navigation.PushAsync(new Page2());

and it is giving syntax error. 它给出了语法错误。 So, is there any other way to navigate to other page using ContentView. 那么,有没有其他方法可以使用ContentView导航到其他页面。

If the MainPage of your app is a NavigationPage , then you can always call PushAsync() from inside your app. 如果您的应用程序的MainPageNavigationPage ,那么您始终可以从应用程序内部调用PushAsync() I would recommend checking that the MainPage is of type NavigationPage just to be sure you don't throw a cast exception. 我建议检查MainPage是否为NavigationPage类型,以确保不抛出强制转换异常。 To do this, just use these two lines: 要做到这一点,只需使用以下两行:

if(App.Current.MainPage is NavigationPage)
    (App.Current.Mainpage as NavigationPage).PushAsync(new Page2());

the solution provided by cvanbeek don't work with mine problem but i sort out you have to use this in your c# code the problem is this you have to understand the hierarchy of code how it works use this to move to another page inside of ContentView .content property cvanbeek提供的解决方案不适用于我的问题,但我要解决你必须在你的c#代码中使用它的问题是你必须了解代码的层次结构如何使用它来移动到ContentView内的另一个页面.content属性

 ((App.Current.MainPage as MasterDetailPage).Detail as NavigationPage).Navigation.PushAsync(new page());

Enjoy Folks :) 享受民谣:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM