Reputation: 3573
In App.xaml.cs i have following code:
MainPage = new LoginPage(); //LoginPage is ContentPage
When user click a Button
on that page there is call to MasterDetailPage which is of MasterDetailPage
type. This will be my real main page from where users will navigate to other pages. I treat it as central place for my application with navigation bar
.
Application.Current.MainPage = new MasterDetailPage();
being in MasterDetailPage in ctor
i set default page which open:
Detail = new NavigationPage(new ClientsPage());
Now, from here i have a Button
to navigate to another ContentPage
so when i click a Button
this line is executed:
await Application.Current.MainPage.Navigation.PushAsync(new ClientModifyPage(_selectedClient));
Nevertheless i am getting following error:
PushAsync is not supported globally on Android, please use a NavigationPage
What's wrong?
Upvotes: 0
Views: 3102
Reputation: 11
If you've added the Page using Singleton change the Page to transit, as transit creates a new page and would not self-reference the current Page as the Parent page
i.e change AddSingleton to AddTransient for the Page you want to navigate to
builder.Services.AddTransient<ListVehiclePage>();
builder.Services.AddSingleton<ListVehicleViewModel>();
The change must be done on MauiProgram File
public static class MauiProgram
Upvotes: 0
Reputation: 158
place below the line in app.cs page
MainPage = new NavigationPage(new LoginPage());
instead of
MainPage = new LoginPage();
And no need to use NavigationPage in other pages.NavigationPage should be used when we are redirecting the page first time in our application. Here MainPage is the root page. We have to assign a page(LoginPage) to MainPage by using NavigationPage.
Upvotes: 2
Reputation: 18861
await Application.Current.MainPage.Navigation.PushAsync(new ClientModifyPage(_selectedClient));
As SushiHangover said , it will be an expected result because Application.Current.MainPage is a MasterDetailPage , which is not in a NavigationPage .
You should call the method PushAsync
in the detail page .
await((MasterDetailPage)App.Current.MainPage).Detail.Navigation.PushAsync(new ClientModifyPage(_selectedClient));
Upvotes: 2