使用mvvm取消退出窗口命令

时间:2015-07-19 08:50:19

标签: c# wpf mvvm mvvm-light formclosing

我尝试使用Microsoft Interactivity InvokeCommandAction来关闭我的应用程序。我想询问用户是否希望在退出之前保存,或取消退出。

我知道我可以Command使用EventName="Closing"EventName="Closed"。我也知道在Closed事件的实施结束时会自动调用Closing

我尝试使用MVVM light按照许多帖子中的建议,但无法找到namespace或找不到EventToCommand

这是我的代码:

的Xaml

    <Interactivity:Interaction.Triggers>
       <Interactivity:EventTrigger EventName="Closing">
           <Interactivity:InvokeCommandAction Command="{Binding ClosingCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"  />
       </Interactivity:EventTrigger>
   </Interactivity:Interaction.Triggers>

VM

    private ICommand m_closingCommand;
    private ICommand m_exitCommand;

    public ICommand ExitCommand
    {
        get { return m_exitCommand ?? (m_exitCommand = new RelayCommand(Exit)); }
        set { m_exitCommand = value; }
    }
    public ICommand ClosingCommand
    {
        get { return m_closingCommand ?? (m_closingCommand = new RelayCommand(Closing)); }
        set { m_closingCommand = value; }
    }
    private void Exit(object parameter)
    {
        if (SaveRequired)
            SaveCommand.Execute(null);

        MarkingsManager.DisposeManager();

        Logger.CloseLogger();
        CommonCommands.CloseWindow.Execute(parameter);
    }
    private void Closing(object parameter)
    {
        if (!SaveRequired) return;
        var result = MessageBox.Show("Do You Want To Save Your Changes?", "Markings Clipper", MessageBoxButton.YesNoCancel);

        switch (result)
        {
            case MessageBoxResult.No:
            {
                SaveRequired = false;
                break;
            }
                case MessageBoxResult.Cancel:
                    return;
        }
    }

0 个答案:

没有答案