7
votes

I want to navigate to other page in Xamarin Forms but it is not working in Content View. I am using

await 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. Currently I am using

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

and it is working fine in ContentView. But I am getting warning that "ParentView is obsolete as of version 2.1.0. Please use Parent instead.". But there is no function like

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

and it is giving syntax error. So, is there any other way to navigate to other page using ContentView.

2
You could use App.Current.MainPage - cvanbeek
@cvanbeek, It will not show Back button on Page2. - Amar Mathur
Sorry I meant call pushasync() from the MainPage - cvanbeek
@cvanbeek, PushAsync is giving error and Navigation.PushAsync is not working. - Amar Mathur
Is it a syntax error? If your mainpage is a NavigationPage you can call (App.Current.MainPage as NavigationPage).PushAsync() - cvanbeek

2 Answers

7
votes

If the MainPage of your app is a NavigationPage, then you can always call PushAsync() from inside your app. I would recommend checking that the MainPage is of type NavigationPage just to be sure you don't throw a cast exception. To do this, just use these two lines:

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

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

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

Enjoy Folks :)