我正在为 Windows 10 Store 应用程序开发一个应用程序,但我似乎无法找到/了解如何检查用户是否按下了红色关闭按钮(在右上角)或按 Alt + F4。基本上我想要的是这样的:
private void app_Close(object sender, CloseEventArgs e)
{
//saves some data in the app :D
}
最佳答案
。: 编辑 :。
如果您有一个没有 MainWindow 对象的通用应用程序,您可能想要进入“暂停”事件:
appObject.Suspending += (s, a) =>
{
SaveTheData(); // I really like my data and want it for later too
};
或者
public App()
{
/*
stuff
*/
Suspending += (s, a) =>
{
SaveTheData(); // I really like my data and want it for later too
};
}
。: 原来的 :。
向 MainWindow“Closing”事件添加处理程序以保存数据。一旦“关闭”完成,“关闭”应该正常触发。
theMainWindowObject.Closing += (s,a) =>
{
SaveTheData(); // It's precious!
};
我在我的一个较小的应用程序中有类似的东西,在 MainWindow 的构造函数中,我将上面的代码片段用“theMainWindowObject”替换为“this”,以便它引用自己
所以我有:
public MainWindow()
{
// Note: "this." isn't necessary here but it helps me with mental accounting
this.Closing += (s, a) =>
{
Save();
};
}
如果你只是保存一两个属性并且没有任何疯狂的逻辑,你可以将它放在处理程序中:
public MainWindow()
{
Closing += (s, a) =>
{
Properties.Settings.Default.SettingsPopupX = mySettingsPopupObject.GetX();
Properties.Settings.Default.SettingsPopupY = mySettingsPopupObject.GetY();
Properties.Settings.Default.Save();
};
}
关于c# - 通用 Windows 应用商店应用程序关闭时如何执行代码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34361603/