2016-05-12 84 views
0

我正在开发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 
    } 
+0

如果您尝试调试它,通常你会不会触发这些事件 - 你将不得不使用*生命周期选项卡* - [看到这个问题](http://stackoverflow.com/q/24103101/2681948)获得更多帮助。 – Romasz

回答

0

您订阅挂起事件两次

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

相关问题