UWP Windows 10 未触发 OnSuspending 事件
OnSuspending event not triggered with UWP Windows 10
我的 UWP 应用程序未触发 OnSuspending 事件,但此问题仅发生在 Windows Phone 运行ning Windows 10。当 运行将其作为 Windows 在我的本地计算机或模拟器上存储应用程序。
我正在使用此事件在应用程序关闭时保存我的应用程序设置,但这显然对 windows phone 造成了重大问题,因为未触发此事件。
如您所见,应用启动时会初始化 OnSuspending 事件
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
}
下面是应该调用的 OnSuspending 代码,但在 Windows Phone 10.
中 运行ning 时不会调用
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
await Locator.MainPageViewModel.SaveSettings();
deferral.Complete();
}
关于如何解决这个问题的任何想法或者是否有潜在的解决方法?
谢谢。
更新-1:
当我通过按住标志键并单击叉号来终止我的应用程序时,关闭了应用程序,但它仍然没有触发 OnSuspending 事件,但 .net IDE 仍然 运行s。当我再次按 F5 到 运行 应用程序时,它会触发 OnSuspending 事件。我的应用程序启动但代码在 IDE.
中停止 运行ning
来自官方App lifecycle documentation:
A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.
这意味着当您附加到 Visual Studio 调试器时,不会触发 OnSuspending
事件。如果要调试它,请通过选择相应的生命周期事件手动发送事件。
我的 UWP 应用程序未触发 OnSuspending 事件,但此问题仅发生在 Windows Phone 运行ning Windows 10。当 运行将其作为 Windows 在我的本地计算机或模拟器上存储应用程序。
我正在使用此事件在应用程序关闭时保存我的应用程序设置,但这显然对 windows phone 造成了重大问题,因为未触发此事件。
如您所见,应用启动时会初始化 OnSuspending 事件
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
}
下面是应该调用的 OnSuspending 代码,但在 Windows Phone 10.
中 运行ning 时不会调用private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
await Locator.MainPageViewModel.SaveSettings();
deferral.Complete();
}
关于如何解决这个问题的任何想法或者是否有潜在的解决方法?
谢谢。
更新-1:
当我通过按住标志键并单击叉号来终止我的应用程序时,关闭了应用程序,但它仍然没有触发 OnSuspending 事件,但 .net IDE 仍然 运行s。当我再次按 F5 到 运行 应用程序时,它会触发 OnSuspending 事件。我的应用程序启动但代码在 IDE.
中停止 运行ning来自官方App lifecycle documentation:
A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.
这意味着当您附加到 Visual Studio 调试器时,不会触发 OnSuspending
事件。如果要调试它,请通过选择相应的生命周期事件手动发送事件。