Code in Application_Activated not running when pho

2019-06-05 08:56发布

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条回答
ら.Afraid
2楼-- · 2019-06-05 09:34

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 (:

查看更多
登录 后发表回答