问题
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.One way to do this:
In
WMAppManifest.xml
replace the property ofNavigation Page
with your desire pageAn 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