c# - 在关闭时使WPF应用程序保持事件状态

标签 c# wpf mvvm mvvm-light

已经对此有疑问,但这有点不同,所以我要问。
这是我在Window内的xaml:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
        <i:InvokeCommandAction Command="{Binding ClosingCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

这将关闭Command:
public virtual ICommand ClosingCommand
{
    get { return new RelayCommand(ClosingExecute); }
}

这是执行:
public virtual void ClosingExecute()
{
   MessageBoxResult result = MessageBox.Show("Close the application?", "Shut Down", MessageBoxButton.YesNo);

    if(result == MessageBoxResult.Yes)
    {
        Application.Current.Shutdown();
    }
    else
    {
       //I don't know what to write
    }

}

在这种情况下如何使我的应用程序保持 Activity 状态?谢谢。

最佳答案

来自Closingdocumentation

Closing can be handled to detect when a window is being closed (for example, when Close is called). Furthermore, Closing can be used to prevent a window from closing. To prevent a window from closing, you can set the Cancel property of the CancelEventArgs argument to true.



您需要将事件处理程序连接到此事件,并将Cancel设置为true

如果要以mvvmy的方式执行此操作,this answer可能会帮助您将eventargs连接到命令参数

关于c# - 在关闭时使WPF应用程序保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25936522/

相关文章:

c# - .NET 版本为 4 或更高时如何编译

c# - 如何在不对 html 进行硬编码的情况下创建 html 报告?

c# - Windows Phone Mvvm Light 在 View 之间导航

wpf - 对动态 DataTable 的编辑未在 DataGrid 中更新

c# - MVVM - 具有多态性的集合

C# 不一致的可访问性 : return type is less accessible than method

c# - ANTLR4 commonTokenStream.GetTokens() 没有得到所有的 token

WPF 功能区按钮鼠标悬停时发光效果

c# - 将 Canvas 大小调整为相邻标签的文本高度

mvvm - 在ComboBox中显示的当前值