我尝试使用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;
}
}