UWP 应用程序退出时如何做作业?
How to do job while the application is exiting in UWP?
我必须在我的应用程序退出时做一些清理工作。
我的 windows 版本。
package.appxmanifest
中的能力
但事件 Exiting and Closed 从未触发。
我不知道这个问题。请帮助我!
在 UWP 中,window关闭有一个特殊事件:SystemNavigationManagerPreview.CloseRequested
如果你想使用这个事件,你需要添加一个特殊的能力:
package.appxmanifest
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
<Capabilities>
...
<rescap:Capability Name="confirmAppClose"/>
...
</Capabilities>
</Package>
之后,您可以注册事件回调:
public MainPage()
{
this.InitializeComponent();
Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested +=
async (sender, args) =>
{
args.Handled = true;
// Do something
App.Current.Exit();
};
}
当您的应用程序window关闭时,将触发此事件,您可以执行一些保存操作。 (在此之前,您必须将 args.Handled
设置为 true 以防止应用程序 window 关闭)。
我必须在我的应用程序退出时做一些清理工作。
我的 windows 版本。
在 UWP 中,window关闭有一个特殊事件:SystemNavigationManagerPreview.CloseRequested
如果你想使用这个事件,你需要添加一个特殊的能力:
package.appxmanifest
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
<Capabilities>
...
<rescap:Capability Name="confirmAppClose"/>
...
</Capabilities>
</Package>
之后,您可以注册事件回调:
public MainPage()
{
this.InitializeComponent();
Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested +=
async (sender, args) =>
{
args.Handled = true;
// Do something
App.Current.Exit();
};
}
当您的应用程序window关闭时,将触发此事件,您可以执行一些保存操作。 (在此之前,您必须将 args.Handled
设置为 true 以防止应用程序 window 关闭)。