如果你有一个没有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(); }; }