NeverMorewd / Hosting.Avaloniaui

.NET Generic Host support for Avaloniaui app
MIT License
22 stars 3 forks source link

How to programmatically shut down the app? #1

Open ode2code95 opened 1 day ago

ode2code95 commented 1 day ago

Thanks @NeverMorewd for this handy library! It seems to work well so far and allowed me to port my .NET MAUI application MauiProgram.cs almost verbatim to Program.cs.

I do have one question: How do I shut down the application programmatically? My app needs to shut down after launching the updater. Looking at the source, I see that an AvaloniauiApplicationLifetime` is added to the service pipeline, so I injected that into the viewmodel that controls the update procedure.

I have not tested yet if this works but I did notice that doing this causes the app to no longer shut down when the main window is closed. Perhaps I'll have to shut it down manually?

I suppose what I am really looking for is how does this library play with the application lifetime as described in the official Avalonia docs?

NeverMorewd commented 1 day ago

Similar to WPF, Avalonia's IClassicDesktopStyleApplicationLifetime provides the ShutDownMode property and the ShutDown() method.

You can call ShutDown() directly to terminate the application. Alternatively, you can set the ShutDownMode property to one of the following values, depending on your use case:

For Hosting.Avaloniaui, the application lifetime is accessed via AppBuilder.SetupWithLifetime(). You can find an example here: Hosting.Avaloniaui - ApplicationLifetime Setup

On the other hand, Avalonia provides an alternative approach for managing application lifetime:


    [STAThread]
    public static void Main(string[] args)
    {
        BuildAvaloniaApp().Start(AppMain, args);
    }

    private static void AppMain(Application app, string[] args)
    {
        app.Run(new MainWindow());
    }

    // Avalonia configuration (don't remove; also used by the visual designer)
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .LogToTrace();