(Windows phone 10)Handle应用程序状态
问题描述:
我正在开发Windows Phone 10中的应用程序(Windows phone 10)Handle应用程序状态
由于某些原因,我必须处理应用程序状态(转到后台,输入前台)。我有处理事件暂停和继续在App.xaml.cs但它不起作用,没有达到OnSuspending和OnResuming。请帮我查看我的源代码并告诉我如何处理这些事件。
这里是我的代码:
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
Application.Current.Resuming += new EventHandler<Object>(OnResuming);
}
private void OnSuspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}
private void OnResuming(object sender, object e)
{
// do some thing
}
答
你应该使用Visual Studio 2015年的生命周期事件下拉它可以让你之间选择暂停,恢复或暂停和关闭状态。
每当您在调试中运行应用程序时,它都不会自动进入暂停状态。
一些文档在这里:https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/
答
您订阅挂起事件两次
this.Suspending += OnSuspending;
Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
最好不要经典
this.Suspending += OnSuspending;
this.Resuming += App_Resuming;
而且也是同样的方法添加恢复事件
private void App_Resuming(object sender, object e)
{
// TODO
}
你调试是暂停/喜欢这篇文章中定律描述恢复事件的工作原理:
How to trigger suspend, resume, and background events for Windows Store apps in Visual Studio
如果您尝试调试它,通常你会不会触发这些事件 - 你将不得不使用*生命周期选项卡* - [看到这个问题](http://stackoverflow.com/q/24103101/2681948)获得更多帮助。 – Romasz