c# – 未使用UWP Windows 10触发的OnSuspending事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 未使用UWP Windows 10触发的OnSuspending事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1734字,纯文字阅读大概需要3分钟。
内容图文
![c# – 未使用UWP Windows 10触发的OnSuspending事件](/upload/InfoBanner/zyjiaocheng/962/46cc44261bae4f6eb881276e415e5393.jpg)
我的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# – 未使用UWP Windows 10触发的OnSuspending事件全部内容,希望文章能够帮你解决c# – 未使用UWP Windows 10触发的OnSuspending事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。