Set MainPage in AppShell

Jassim Al Rahma 1,616 Reputation points
2022-10-24T21:50:50.123+00:00

Hi,

How can I set different MainPage in AppShell like this:

public App()  
{  
    InitializeComponent();  
  
    if (condition_1)  
    {  
        MainPage = new AppShell();  
    }  
    else  
    {  
        MainPage = AnotherPage;  
    }  
}  

Thanks,
Jassim

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

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2022-10-25T09:36:44.593+00:00

    Hello @Jassim Al Rahma ,

    You could add a new MAUI ContentPage named AnotherPage firstly, then set different page, please refer to the following code:

     if (condition_1)  
            {  
                Application.Current.MainPage = new AppShell();  
            }  
            else  
            {  
                Application.Current.MainPage = new AnotherPage();// or new NavigationPage(new AnotherPage()) if you would like to use NavigationPage, see https://learn.microsoft.com/en-us/dotnet/maui/user-interface/pages/navigationpage  
            }  
    

    And I check another thread you create, the condition_1 is requesting the Android permissions which is an async method. So, you can add a default page, then judge permissions and set different page in the OnAppearing method.
    App.xaml.cs

     public App() {  
         InitializeComponent();  
          
         MainPage = new DefaultPage();  
          
     }  
    

    OnAppearing method in DefaultPage

    protected override async void OnAppearing()  
        {  
            base.OnAppearing();  
      
            if (...){  
                Application.Current.MainPage = new AppShell();  
            }  
            else{  
                Application.Current.MainPage = new NavigationPage(new MauiAwaitSample.MainPage());  
            }  
        }  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    1 person found this answer helpful.
    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.