在带有MVVM指示灯的'X'按钮关闭窗口时进行确认 [英] Confirmation when closing window with 'X' button with MVVM light

查看:122
本文介绍了在带有MVVM指示灯的'X'按钮关闭窗口时进行确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF和MVVM Light框架(我是新手).

I am using WPF and MVVM Light framework (I am new in using them).

我要执行以下操作:

  1. 当用户单击"X"关闭按钮时,是否要退出该应用程序,我想显示一个确认窗口.
  2. 如果是,则应用程序关闭
  3. 如果没有,则什么也没有发生,他仍然可以照常使用该应用程序

到目前为止,我有这个:

So far, I have this:

  • 在MainWindow.xaml.cs中:

  • In MainWindow.xaml.cs:

public MainWindow()
{
    InitializeComponent();
    Closing += (s, e) => ViewModelLocator.Cleanup();
}

  • 在ViewModelLocator.cs中:

  • In ViewModelLocator.cs:

    public static void Cleanup()
    {
        ServiceLocator.Current.GetInstance<MainViewModel>().Cleanup();
    }
    

  • 在MainViewModel.cs中:

  • 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
        {
          // ????
        }
    

  • 实际上,如果用户回答是"或否",则两种情况下应用程序都将退出.

    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...

    任何帮助都会很棒!

    谢谢

    推荐答案

    您可以在EventTrigger中使用EventToCommand来捕获关闭事件,并在以下情况下将传递的CancelEventArgsCancel属性设置为true:您要取消关闭:

    You can use an EventToCommand in an EventTrigger to catch the closing event and set the Cancel property of the passed CancelEventArgs to true if you want to cancel the closing:

    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;
          } 
       }
    }
    

    这篇关于在带有MVVM指示灯的'X'按钮关闭窗口时进行确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆