Code in Application_Activated not running when phone wakes up

余生颓废 提交于 2020-01-23 17:29:08

问题


I've done some reseach on the life cycle of Windows Phone apps and I've gathered that when the phone is locked whilst an app is still running, and you unlock the phone the 'Application_Activated' function is called in the App.xaml.cs file.

// 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!");
}

Now in the above example, the simple 'MessageBox' call doesn't get run. Like I said, if you have your app running and you lock the phone, and then unlock the phone the above code is expected to run, in this case display a MessageBox as soon as you unlock the phone.

Any help would really be appreciated! Thanks.


回答1:


You can not do that

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

if you wanna show same message my suggestion is to use OnNavigatedTo event

EDIT

if i understood correctly you wanna change default page navigation

  1. 1.One way to do this:

    In WMAppManifest.xml replace the property of Navigation Page with your desire page

  2. An alternative:

In WMAppManifest.xml remove the property of 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));
}

This way you can "play" with IsolatedStorageSettings for example

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

It just an idea, let me know how it goes (:



来源:https://stackoverflow.com/questions/15758511/code-in-application-activated-not-running-when-phone-wakes-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!