Merge pull request #440 from irihitech/window-close
Add CanClose hook for UrsaWindow
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Threading.Tasks;
|
||||
using Ursa.Controls;
|
||||
|
||||
namespace Ursa.Demo.Views;
|
||||
@@ -8,4 +9,10 @@ public partial class MainWindow : UrsaWindow
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override async Task<bool> CanClose()
|
||||
{
|
||||
var result = await MessageBox.ShowOverlayAsync("Are you sure you want to exit?\n您确定要退出吗?", "Exit", button: MessageBoxButton.YesNo);
|
||||
return result == MessageBoxResult.Yes;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.ComponentModel;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
|
||||
@@ -100,5 +101,24 @@ public class UrsaWindow: Window
|
||||
set => SetValue(TitleBarMarginProperty, value);
|
||||
}
|
||||
|
||||
protected virtual async Task<bool> CanClose()
|
||||
{
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
private bool _canClose = false;
|
||||
protected override async void OnClosing(WindowClosingEventArgs e)
|
||||
{
|
||||
VerifyAccess();
|
||||
if (!_canClose)
|
||||
{
|
||||
e.Cancel = true;
|
||||
_canClose = await CanClose();
|
||||
if (_canClose)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
base.OnClosing(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user