Categories
.NET .NET 8 .NET 9 .NET MAUI Android Blazor C# Code Desktop Developer General Hybrid iOS macOS Mobile Web What's New Windows Xamarin Xamarin.Forms

Transitioning from Application’s MainPage to Window’s Page in .NET MAUI 9

This article analyzes a significant change introduced in .NET MAUI 9, specifically the deprecation of the Application’s MainPage property, which has been replaced by the Window’s Page property.

An article has already been published detailing the comprehensive features of .NET MAUI 9.

The MainPage has existed since the inception of Xamarin.Forms, at which time it functioned as a single-window UI framework.

Nevertheless, the design of .NET MAUI incorporates the concept of a Window to effectively support Desktop form factors, including WinUI and Mac Catalyst.

Until .NET MAUI 8, the support for the MainPage property was consistently maintained. The Window instance returned from the CreateWindow() method will act as the primary window. The MainPage property specifies the app’s starting page.

Now .NET MAUI 9 has evolved into a truly multi-window framework. It deprecates the MainPage property. The framework now associates the Page with the Window.

The Window type includes a Page property to set up the linkage. It provides a constructor that takes a Page as an argument. This allows the value to be set during the Window’s instantiation.

Before:

XAML:

<Application
    x:Class="MauiApp1.App"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiApp1">
    <Application.MainPage>
        <local:AppShell />
    </Application.MainPage>
</Application>

C#:

namespace MauiApp1;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new AppShell();
    }
}

After:

XAML:

Assuming that MainWindow is a class derived from the Window type.

<Window
    x:Class="MauiApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiApp1">
    <!-- Page is the Content Property of the Window -->
    <!-- So this wrapper node is optional -->
    <Window.Page>
        <local:AppShell />
    </Window.Page>
</Window>
namespace MauiApp1;

public class MainWindow : Window
{
    public MainWindow() => InitializeComponent();

    public MainWindow(Page page) : base(page) => InitializeComponent();
}

C#:

Option 1 – Parameterless Constructor:

namespace MauiApp1;

public partial class App : Application
{
    public App() => InitializeComponent();

    protected override Window CreateWindow(IActivationState? activationState)
        => new MainWindow(); // Page is defined within MainWindow definition (in XAML or C#)
}

Option 2 – Parameterized Constructor:

namespace MauiApp1;

public partial class App : Application
{
    public App() => InitializeComponent();

    protected override Window CreateWindow(IActivationState? activationState)
    {
        // The window type can also be used directly.
        return new Window(new AppShell());
        // It can also be defined in an Object Initializer
        //return new Window() { Page = new AppShell() };
    }
}

Unified Option:

To manage Application, Window, and Page in one place, a generic extension method has been published. This method lets you define everything in a single call. No need to override any method. Just one method call with type names. For details, consult this Integrated App Hosting Builder Method article.

var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App, Window, MainPage>();
// To use Shell as the initial page.
//builder.UseMauiApp<App, Window, AppShell>();
// Rest of the configuration ...
return builder.Build();

Further Reading:

Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.

By Vijay Anand E G

A software professional with over a decade long industry experience in developing products that spans across desktop, mobile, and web.

One reply on “Transitioning from Application’s MainPage to Window’s Page in .NET MAUI 9”

Comments are closed.

Discover more from Developer Thoughts

Subscribe now to keep reading and get access to the full archive.

Continue reading