2016-05-12 117 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

相關問題