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.
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 of Navigation Page with your desire page
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 (:
Related
The title is the question right here, but to be more specific, i'll show the example. Exit icon as i want to do.
Execute the following lines of code in your icon's OnClickListener,
MainActivity.this.finish();
System.exit(0);
You can use the following code to terminate an application via a click event in Xamarin Forms.
For more details, you can refer to this thread.
private void Button_Clicked(object sender, EventArgs e)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
I need to re-run the code contained within dela method OnNavigatedTo () when the app resumes from background.
To do this I need the event VisibilityChanged:
Link MSDN
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
//My code
}
With this event, each time the app is opened again from the background, the code contained within dell'OnNavigatedTo runs again. How can I use that event? I can not.
Visibility changed is only relevant if the page isn't in the background. Every time the app opens it goes to OnNavigatedTo(), you can make a bool or counter to check if it is the first time you entered the page and then decide what to do based on that inside OnNavigatedTo. for Example:
private override void OnNavigatedTo(NavigationEventArgs e)
{
if(hasBeenHere) Repeat_Visit(args);
else First_Visit(args);
}
The Application_Activated Event gets fired when the application is resumed from the background.
From the App.xaml.cs of the WP8.1 Template:
// 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)
{
// your code
}
Edit : to call methods on MainPage from app.xaml.cs has already been answered here : How to use a method in MainPage from app.xaml.cs with variable appbar pivot
I am developing a Windows Phone 8.1 Silverlight app. In my app, I need to override the back button code.
So I tried this,
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
DisplayPage mynewPage = new DisplayPage();
this.Content = mynewPage;
e.Cancel = true;
}
But this code is not working. What am I doing wrong? Please help me out.
EDIT:
When I place this code on MainPage, it works! But I don't want to place it there. I want to place it on other pages.
Remove "e.Cancel = true" (it cancels the navigation). Simply just navigate to the new page.
EDIT:
For navigating to another page, I would rather suggest using NavigationService. Check out this page for examples.
EDIT:
Code snippet:
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
NavigationService.Navigate(new Uri("/DisplayPage.xaml", UriKind.Relative));
}
I have created a button in C# for Windows phone 7 development. It contains an animation and when clicked I want the animation to be shown, and then for the button to navigate to the next page.
Using Thread.Sleep(4000) just crashes the application and I was wondering how I go about this.
Is there a way to delay the navigate code?
private void batnballBtn_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
}
Presumably the animation is a Storyboard. You can navigate within the Storyboard's Completed event.
myStoryboard.Completed += (s, e) =>
{
myStoryboard.Stop();
NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
};
This way you don't need to predict how long the animation will take, making the code more reusable, and you don't have to worry about dealing with multiple threads manually.
Use a thread for that:
http://techkn0w.wordpress.com/2012/04/18/using-a-background-thread-in-windows-phone-7-wp7/
You're stopping the UI thread that's why the button is freeze.
There is only one thread responsible for managing the UI, so, if you block it when doing lengthy operations or making it sleep the UI process management is freezed until that work has finished.
Something like this:
private void batnballBtn_Click(object sender, RoutedEventArgs e)
{
ThreadPool.QueueUserWorkItem((WaitCallback)delegate(object state)
{
Thread.Sleep(4000);
this.Dispatcher.BeginInvoke((Action)delegate
{
NavigationService.Navigate(new Uri("/PictureGame.xaml", UriKind.Relative));
});
});
}
I have 2 pages.(MainPage.xaml,second.xaml)
MainPage.xaml is the Login page. In this page I send login and password, and receive result. I save them(result) in Isolate Storage and navigate to the second.xaml page;
When i start this application in the next time, i extract data from Isolate Storage and I want to navigate the second.xaml immidiately, but i don't know how
I try write
public MainPage()
{
InitializeComponent();
//function for Isolate storage
InitializeSettings();
NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
}
But it isn't work)
I understood that I could not use the navigation code associated with the MainPage() constructor. Of course, i may will do simple button, but i wish fast navigation
I think may be it connected with App.xaml method
private void Application_Launching(object sender, LaunchingEventArgs e)
for example, write my method
//function for Isolate storage
InitializeSettings();
with navigation there?(navigation not work in this example)
private void Application_Launching(object sender, LaunchingEventArgs e)
{
InitializeSettings();
NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
}
Where I can use navigation, so go straight to the second.xaml page, without fully loading the MainPage.xaml(may be without MainPage.xaml)
You can do as Rana Tallal said.
Or you can write it in code:
public MainPage()
{
InitializeComponent();
Loaded += (s, e) =>
{
InitializeSettings();
// Some login-password check condition
if (_login && _password)
NavigationService.Navigate(new Uri("/Conversation.xaml",
UriKind.Relative));
}
}
Well create a new function... and in that perform the checks at which you want it to be navigated and if the checks are ok right in it than call the navigation service navigationservice.navigate(....) code.
Now you need to tell the program to call this function when the mainpage is completely loaded. To do so in the xml of mainpage inside tags at the end of it write loaded="function_name"
Now when ever the page will be loaded this function will be called. If the login information is present in the isolated storage than the navigation sevices will be called otherwise the mainpage will be shown.
Make sure to put (object sender, RoutedEventArgs e) in the functions parameters(as it is a event handler).