c# – 未使用UWP Windows 10触发的OnSuspending事件
作者:互联网
我的UWP应用程序未触发OnSuspending事件,但此问题仅发生在运行Windows 10的Windows Phone上.在我的本地计算机或模拟器上将其作为Windows应用商店应用程序运行时,它可以正常工作.
我正在使用此事件在应用程序关闭时保存我的应用程序设置,但这显然会导致Windows手机出现重大问题,因为此事件未触发.
如您所见,OnSuspending事件在应用程序启动时初始化
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
}
下面是应该调用的OnSuspending代码,但在Windows Phone 10中运行时则不行.
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仍然运行.当我按F5再次运行应用程序时,它会触发OnSuspending事件.我的应用程序启动但代码停止在IDE中运行.
解决方法:
从官方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事件不会被触发.如果要调试它,请通过选择相应的生命周期事件手动发送事件.
标签:c,windows-store-apps,windows-phone,win-universal-app,uwp 来源: https://codeday.me/bug/20190716/1475563.html