Mubeen Khanzada
Mubeen Khanzada

Reputation: 33

.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

Upvotes: 3

Views: 1067

Answers (1)

FreakyAli
FreakyAli

Reputation: 16562

That's easy, first set the MainPage and for that 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

Upvotes: 6

Related Questions