-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to cancel App Shutdown from Window Closing #14748
Comments
I have the same issue but on windows. I also have another problem. |
@THE-KONDRAT if you want to prevent a Window from closing, why not override |
I'm sorry but where should i override this event? |
In MainWindow.axaml.cs |
And how can i wait another method inside OnClosing? |
something like that should work private bool _canClose;
protected override void OnClosing(WindowClosingEventArgs e)
{
if (!_canClose)
{
// cancel closing
e.Cancel = true;
_canClose = await DoAsyncStuff(); // async stuff should return bool here
if (_canClose) this.Close(); // request close again if above was successful
}
base.OnClosing(e);
} |
Thanks! This also works. Maybe someone find this usefull. protected override async void OnClosing(WindowClosingEventArgs e)
{
var context = Ioc.Default.GetRequiredService<ApplicationContext>();
if (!context.CanClose)
{
e.Cancel = true;
await context.WaitForAsync(nameof(context.CanClose), q => q.CanClose);
if (context.CanClose) this.Close();
}
base.OnClosing(e);
} |
Just to clarify, this is not what this issue is about. The issue I'm having is when the app is being closed by the OS (not when clicking close on the window). If you wire up the OnClosing the same as above, but on macOS you close the application from the menu bar or by right clicking it on the dock. The OnClosing event will fire but the app will close regardless. |
@nixtar I'm aware of that, and this is why I didn't close it. |
From how it looks, this functionality was broken for two years since #7400 |
Finally got a chance to test this with 11.1.0-beta2 and am happy to confirm it now works as expected. 👍 |
Describe the bug
I have an application that prompts the user to confirm when they try and close the apps main window.
I'm currently doing this using Avalonia.Xaml.Behaviors to wire up the Closing event to an async RelayCommand in the viewmodel.
When you close the window with the red X this works as desired.
However, on macOS when you close the app from the menu bar or from the dock this does not work.
The closing event is raised but the app closes when the first await is fired (even if
e.Cancel = true;
is set).To Reproduce
Expected behavior
All methods to gracefully close the app should be cancellable.
Environment
Additional context
You can cancel the app shutdown from the ShutdownRequested event however the remarks for this event reads:
This event provides a first-chance to cancel application shutdown; if shutdown is not canceled at this point the application will try to close each non-owned open window, invoking the Closing event on each and allowing each window to cancel the shutdown of the application.
The text was updated successfully, but these errors were encountered: