Search code examples
xamarinxamarin.formsmaui

.NET MAUI Application class MainPage is Obsolete


I just migrated a small project to .NET 9 and it seems Application classes MainPage is now obselete, cannot find a direct solution for this

My current code:

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

Also how would I access the MainPage now Globally like i used to with the Application.Current.MainPage

Any documentation links are appreciated as well

I tried Google but did not find anything relevant quickly


Solution

  • That's easy, first to set the MainPage now you need to create a new Window something like this

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    
        protected override Window CreateWindow(IActivationState? activationState)
        {
            return new Window(new AppShell());
        }
    }
    
    

    Now to access this MainPage globally I would do something like:

    var MainPage = Application.Current.Windows.FirstOrDefault()?.Page;
    

    More information here : https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0