Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 341
Amar Mathur Avatar asked Nov 16 '17 23:11

Amar Mathur


2 Answers

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());
like image 85
cvanbeek Avatar answered Oct 14 '22 20:10

cvanbeek


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 :)

like image 3
Mr Talha Avatar answered Oct 14 '22 20:10

Mr Talha