手机唤醒时Application_Activated中的代码未运行

时间:2013-04-02 07:29:20

标签: c# windows-phone-7 windows-phone-7.1 windows-phone

我已经对Windows Phone应用程序的生命周期进行了一些研究,并且我已经收集到当应用程序仍在运行时手机被锁定,并且您解锁手机时,应用程序中调用了“Application_Activated”功能.xaml.cs文件。

// 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。

真的很感激任何帮助!感谢。

1 个答案:

答案 0 :(得分: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.一种方法:

    WMAppManifest.xml中将Navigation Page的属性替换为您想要的页面

  2. 替代方案:

  3. 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));
            }
    

    这只是一个想法,让我知道它是怎么回事(: