wpf 使用带有 MVVM 灯的“X”按钮关闭窗口时的确认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14395301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confirmation when closing window with 'X' button with MVVM light
提问by Romain
I am using WPF and MVVM Light framework (I am new in using them).
我正在使用 WPF 和 MVVM Light 框架(我是新手)。
I want to do the following:
我想做以下事情:
- When the user click on the 'X' close button, I want to display a confirmation window if whether he wants to exit the application or not.
- If yes, the application closes
- If no, nothing happens and he can still use the application as per normal
- 当用户单击“X”关闭按钮时,如果他是否要退出应用程序,我想显示一个确认窗口。
- 如果是,应用程序关闭
- 如果没有,则什么也没有发生,他仍然可以正常使用该应用程序
So far, I have this:
到目前为止,我有这个:
In MainWindow.xaml.cs:
public MainWindow() { InitializeComponent(); Closing += (s, e) => ViewModelLocator.Cleanup(); }In ViewModelLocator.cs:
public static void Cleanup() { ServiceLocator.Current.GetInstance<MainViewModel>().Cleanup(); }In MainViewModel.cs:
public override void Cleanup() { MessageBoxResult result = MessageBox.Show( "Unsaved data will be lost, would you like to exit?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // clean-up resources and exit } else { // ???? }
在 MainWindow.xaml.cs 中:
public MainWindow() { InitializeComponent(); Closing += (s, e) => ViewModelLocator.Cleanup(); }在 ViewModelLocator.cs 中:
public static void Cleanup() { ServiceLocator.Current.GetInstance<MainViewModel>().Cleanup(); }在 MainViewModel.cs 中:
public override void Cleanup() { MessageBoxResult result = MessageBox.Show( "Unsaved data will be lost, would you like to exit?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { // clean-up resources and exit } else { // ???? }
Actually, if the user answers 'Yes' or 'No', in both cases the application will exit.
实际上,如果用户回答“是”或“否”,在这两种情况下,应用程序都会退出。
I am not too sure how to proceed from here...
我不太确定如何从这里开始......
Any help would be great!
任何帮助都会很棒!
Thanks
谢谢
回答by Jürgen Bayer
You can use an EventToCommandin an EventTriggerto catch the closing event and set the Cancelproperty of the passed CancelEventArgsto true if you want to cancel the closing:
如果要取消关闭,您可以使用EventToCommandin anEventTrigger来捕获关闭事件并将Cancel传递的属性设置CancelEventArgs为 true:
XAML:
XAML:
<Window ...
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<cmd:EventToCommand Command="{Binding OnClosingCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
...
</Grid>
</Window>
ViewModel:
视图模型:
public class MainViewModel : ViewModelBase
{
public RelayCommand<CancelEventArgs> OnClosingCommand { get; set; }
public MainViewModel()
{
this.OnClosingCommand =
new RelayCommand<CancelEventArgs>(this.OnClosingCommandExecuted);
}
private void OnClosingCommandExecuted(CancelEventArgs cancelEventArgs)
{
...
if (mustCancelClosing)
{
cancelEventArgs.Cancel = true;
}
}
}
回答by Kent Boogaart
The Closingevent arguments have a Cancelproperty that you need to set to trueif the user cancels the close. Therefore, your Cleanup()method should return booland you should assign it to the Cancelproperty.
该Closing事件参数有一个Cancel,你需要设置属性true,如果用户取消关闭。因此,您的Cleanup()方法应该返回bool并且您应该将其分配给Cancel属性。

