2013-04-02 13 views
1

我已經對Windows Phone應用程序的生命週期進行了一些研究,並且我已經瞭解到,當應用程序仍在運行時手機被鎖定,並且您將手機解鎖'Application_Activated'函數在App.xaml.cs文件中被調用。在手機醒來時Application_Activated中的代碼不運行

// Code to execute when the application is activated (brought to foreground) 
// This code will not execute when the application is first launched 
private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
    //Code to run 
    MessageBox.Show("Hello there!"); 
} 

現在在上面的例子中,簡單的'MessageBox'調用不會運行。正如我所說,如果您的應用程序正在運行,並且您鎖定了手機,然後解鎖手機,則上述代碼有望運行,在此情況下,只要您解鎖手機,就會顯示一個MessageBox。

任何幫助真的很感謝!謝謝。

回答

2

你不能這樣做,

If you call Show(String) method from the app Activated and Launching event 
handlers an InvalidOperationException is thrown with the message Error 
Displaying MessageBox. 

it is in msdn

,如果你想顯示相同的消息我的建議是使用OnNavigatedTo事件

編輯

如果我理解正確你要更改默認頁面導航

  1. 1.One方式做到這一點:

    WMAppManifest.xml與你的願望頁面替換Navigation Page財產

  2. 一種替代方案:

WMAppManifest.xml刪除屬性的Navigation Page

private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
    RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative)); 
} 
private void Application_Activated(object sender, ActivatedEventArgs e) 
{    
    RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative)); 
} 

這樣你就可以 「玩」 IsolatedStorageSettings例如

 if (boolvariable) 
     { 
      RootFrame.Navigate(new Uri("YourPage.xaml", UriKind.Relative)); 
      boolvariable = false; 
     } 
    else 
     { 
      RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
     } 

這只是一個想法,讓我知道如何去(:

+0

我明白了,謝謝指出這一點。 MessageBox.Show()實際上只是一種測試它的方法。我實際想要做的是導航到我已經創建的.xaml頁面。 I.e.當您鎖定手機並將其解鎖時,該應用會自動導航至「main_screen.xaml」。無論您鎖定屏幕時的頁面位置如何。我如何得到這個工作? – Tiwaz89

+0

請現在看看我的答案 –

相關問題