How to access PushAsync in cs file not the xmal.cs?

mc 5,491 Reputation points
2021-04-26T00:56:20.387+00:00

I can use Navigation.PushAsync in xaml.cs

but I can not use App.Current.MainPage.Navigation.PushAsync in other cs file.

I am using simple xamarin.forms not shell. add a tabbed page to the mainpage.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-04-26T03:28:07.247+00:00

    Hello,

    Welcome to Microsoft Q&A!

    How to access PushAsync in cs file not the xmal.cs?

    Definitely we can call App.Current.MainPage.Navigation.PushAsync anywhere in project .

    But first of all , we should put the App.Current.MainPage into navigation stack , in other word , the MainPage should be wrapped into NavigationPage .

    Normally the solution is to set NavigationPage in App.cs , MainPage = new NavigationPage(new TabbedPage1()); .

    However , if you just want TabbedPage to be the root page , you can use (App.Current.MainPage as TabbedPage).CurrentPage.Navigation.PushAsync(xxx); (CurrentPage should be placed in naviagtion stack as well).

    Best Regards,
    Cole Xia


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.