Call method on application exit in Avalonia
Asked Answered
V

1

5

I need to call method when application is closing in Avalonia. Honestly on startup too, but this can be handled by view model constructor.

The obstacle is that Avalonia documentation is empty and barren, so I basically have zero information how to do it. I've already tried doing it the same as in WPF but it doesn't work. Example. Avalonia doesn't even have Exit or ExitEventArgs objects.

So the only thing left is to bash my head against the wall hoping that one of the random methods work, but this metod is yet to bear fruit.

Villainous answered 26/1, 2023 at 14:33 Comment(1)
You could use Closing and Activated events on your main windowCyanohydrin
C
8

You can access the application's lifetime events by overriding the OnFrameworkInitializationCompleted method of your application class in App.axaml.cs:

public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
        desktop.Startup += OnStartup;
        desktop.Exit += OnExit;
    }

    base.OnFrameworkInitializationCompleted();
}

private void OnStartup(object s, ControlledApplicationLifetimeStartupEventArgs e)
{
    [...]
}

private void OnExit(object sender, ControlledApplicationLifetimeExitEventArgs e)
{
    [...]
} 
Costive answered 23/6, 2023 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.