Confirmation when closing window with 'X' button with MVVM light

穿精又带淫゛_ 提交于 2019-12-18 18:04:17

问题


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

I want to do the following:

  1. 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.
  2. If yes, the application closes
  3. If no, nothing happens and he can still use the application as per normal

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

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


回答1:


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



回答2:


The Closing event arguments have a Cancel property that you need to set to true if the user cancels the close. Therefore, your Cleanup() method should return bool and you should assign it to the Cancel property.



来源:https://stackoverflow.com/questions/14395301/confirmation-when-closing-window-with-x-button-with-mvvm-light

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!