Categories
.NET .NET 8 .NET 9 .NET MAUI .NET MAUI 9 Android Blazor C# Code Developer Getting Started Hybrid iOS macOS Mobile NuGet Toolkit Tools Web What's New Windows Xamarin Xamarin.Forms

Integrated App Hosting Builder Method for .NET MAUI Explained

VijayAnand.MauiToolkit is a toolkit that provides various types and methods to make working with .NET MAUI easier. It’s available as a NuGet package and can be installed using the command below.

dotnet add package VijayAnand.MauiToolkit

The latest addition is the Application, Window, and Page integrated app hosting builder extension method.

Introduction:

From .NET MAUI 9, you need to create your main window to run your app by overriding the CreateWindow() method. Instead of setting the Application’s MainPage, pass the page as a parameter to the constructor of the Window type. Alternatively, set its Page property. This topic has already been covered in detail in an earlier article.

Since the app startup configuration is done in one location, MauiProgram.cs, it’s best to set the main Window while configuring the Application in the UseMauiApp() method. Thus the inherited App type acts as a container for global resources. It also handles app events like Start, Sleep, and Resume.

Integrated App Hosting Builder Method:

This v4.x of the toolkit includes this updated method and is overloaded to cater to different scenarios.

Code snippet showcasing the method signatures of the integrated app hosting builder method for .NET MAUI.
Integrated App Hosting Builder Method for .NET MAUI

Features:

  • When using this method, you don’t need to override the CreateWindow() in App.xaml.cs.
  • The core UseMauiApp<TApp>() is called internally, so there’s no need to repeat it.
  • The factory delegate method overload provides access to the configured services via the IServiceProvider type parameter.
    • If unused, simply put a discard as the parameter placeholder.
  • Use the factory overload with the prebuilt Window type; otherwise, you can’t assign a value to the Page property.
    • This limitation is now resolved in v4.1.0 of the package. Check out the updated sample.
  • When using the inherited Window type, both the Generic and Factory overloads are accessible.
  • No need to register types in the DI container with the Generic overload.
  • ConfigureServices() is a simple extension method for method chaining.

Code Sample:

v4.0.0

using Microsoft.Extensions.Logging;
using VijayAnand.MauiToolkit;

namespace MyApp;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        // Refer to the other code snippet for the enhanced method.
        // Option 1 - Using the Window type as it is
        //builder.UseMauiApp<App, Window>(_ => new Window(new MainPage()) { Title = "MyApp" })

        // Option 2 - Page resolved from DI container
        //builder.UseMauiApp<App, Window>(sp => new Window(sp.GetRequiredService<MainPage>()) { Title = "MyApp" });

        // Option 3 - Using Shell as the initial page
        //builder.UseMauiApp<App, Window>(_ => new Window(new AppShell()))

        // Option 4 - Extending the Window type
        // Note: There's no need to register the MainWindow in the DI container explicitly.
        builder.UseMauiApp<App, MainWindow>()
               .ConfigureFonts(fonts =>
               {
                   fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                   fonts.AddFont("OpenSans-SemiBold.ttf", "OpenSansSemiBold");
               })
               // Another extension method to configure services from this toolkit
               .ConfigureServices(services =>
               {
                   services.AddSingleton(SemanticScreenReader.Default);
                   services.AddSingleton<MainViewModel>();
                   services.AddSingleton<MainPage>();
               });

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

v4.1.0:

  • Use the 3-parameter overload to use the prebuilt Window type directly.
  • Since the types are resolved, any injectable dependency can be added to their constructor.
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();

AppServices:

Another notable feature is the static wrapper type over the IPlatformApplication.Current property, which gives access to configured services throughout the application. This is represented by the AppService type from the VijayAnand.MauiToolkit.Services namespace.

using VijayAnand.MauiToolkit.Services;

public partial class MainPage : ContentPage
{
    public ContentPage()
    {
        InitializeComponent();
        BindingContext = AppService.GetService<MainViewModel>();
    }
}

Note: v3.2.0 and v3.3.0 of the toolkit have the same API for .NET MAUI 8, allowing the app to work without setting the MainPage. Manage everything in one place.

Update: The 3-type parameter generic method overload with Application, Window, and Page is now released in v4.1.0. It will work without needing a factory delegate for simple scenarios.

The app project template in the All-in-One .NET MAUI Templates Pack has been updated to include support for the unified startup. Consult this article for more info.

Support:

If you have any issues or features to suggest, please raise an issue with appropriate details here. Ideas are most welcome.

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

2 replies on “Integrated App Hosting Builder Method for .NET MAUI Explained”

Comments are closed.

Discover more from Developer Thoughts

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

Continue reading