2017-07-13 30 views
1

我已经做了一些研究,但我没有发现太多对UWP有用的东西。当窗口上点击'X'时,我需要在应用程序实际关闭之前调用一个函数。我似乎无法找到任何涉及MVVM Cross和UWP的资源。看起来可能有一种方法,但网上只是缺乏资源。我真的只是希望指出正确的方向。访问窗口上的关闭按钮UWP C#

+0

听起来像你试图抢劫回调? – TGarrett

回答

4

当你创建一个空白的应用程序,并转到App.xaml.cs你应该在文件的结尾看到事件OnSuspending

看到下面

/// <summary> 
/// Invoked when application execution is being suspended. Application state is saved 
/// without knowing whether the application will be terminated or resumed with the contents 
/// of memory still intact. 
/// </summary> 
/// <param name="sender">The source of the suspend request.</param> 
/// <param name="e">Details about the suspend request.</param> 
private void OnSuspending(object sender, SuspendingEventArgs e) 
{ 
    var deferral = e.SuspendingOperation.GetDeferral(); 
    //TODO: Save application state and stop any background activity 
    deferral.Complete(); 
} 

这是你在找什么。

+2

只需要添加一下,在系统结束之前先做好自己的工作。 –

+1

这很好,谢谢。 –

0

我不确定你的意思是与MVVM Cross交易。为了处理或致电之前应用程序关闭功能/停用您可以拨打下列事件:

Window.Current.Activated += (s, e) => 
{ 
    //closed, activated, deactivate 
}; 
Window.Current.Closed += (ss, ee) => 
{ 
    //closed 
}; 
+0

这不会起作用。我不认为你可以监视** main **窗口上的Closed事件。 –

+0

我测试过它,它适用于我,不知道你的意思是无法监测封闭事件? –

+0

'Activated'应该可以工作,但'Closed'不会启动......而这个问题特别要求在点击** x **按钮时如何处理 –