You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Coming from WPF's App.Current.Shutdown() it is not immediately obvious what the analog is for Avalonia. After reading about Lifetimes, it makes sense why things are not as simple as WPF but still left me wondering "okay, so how do I quit the desktop app?"
Going the classic MVVM route I was essentially trying to map a "Quit" MenuItem to shutdown the application and do any user prompting ceremony.
The first step was learning that the IClassicDesktopStyleApplicationLifetime interface (try saying that 5 times fast) is where the expected Shutdown() (or TryShutdown()) method lives. But how would I access this from my MainWindowViewModel?
The solution I came up with was to inject that lifetime into the VM and rework the startup code a bit:
publicsealedclassMainWindowViewModel:ReactiveObject{// Maybe store an Action<int> requestQuit instead?privatereadonlyIClassicDesktopStyleApplicationLifetime_lifetime;privatestring_windowTitle=App.AppName;publicstringWindowTitle{get=>_windowTitle;set=>this.RaiseAndSetIfChanged(ref_windowTitle,value);}publicMainWindowViewModel(IClassicDesktopStyleApplicationLifetimelifetime){_lifetime=lifetime;}publicvoidOnQuitRequested()=>_lifetime.Shutdown();}
This works great and "feels" the most clean approach. I didn't love needing the entire lifetime interface stored by the VM, so maybe passing an Action<int> requestQuit that calls lifetime.Shutdown(exitCode) instead could be more de-coupled approach.
Anyways, it would be nice if something like this could be documented as it seems fairly elementary in the context of desktop applications.
The text was updated successfully, but these errors were encountered:
Coming from WPF's
App.Current.Shutdown()
it is not immediately obvious what the analog is for Avalonia. After reading about Lifetimes, it makes sense why things are not as simple as WPF but still left me wondering "okay, so how do I quit the desktop app?"Going the classic MVVM route I was essentially trying to map a "Quit"
MenuItem
to shutdown the application and do any user prompting ceremony.The first step was learning that the
IClassicDesktopStyleApplicationLifetime
interface (try saying that 5 times fast) is where the expectedShutdown()
(orTryShutdown()
) method lives. But how would I access this from myMainWindowViewModel
?The solution I came up with was to inject that lifetime into the VM and rework the startup code a bit:
App.axaml.cs:
MainWindowViewModel.cs:
MainWindow.axaml:
This works great and "feels" the most clean approach. I didn't love needing the entire lifetime interface stored by the VM, so maybe passing an
Action<int> requestQuit
that callslifetime.Shutdown(exitCode)
instead could be more de-coupled approach.Anyways, it would be nice if something like this could be documented as it seems fairly elementary in the context of desktop applications.
The text was updated successfully, but these errors were encountered: