- Back to Home »
- Windows Phone apps »
- Windows Phone 8 apps development Part-5 Navigation
Posted by :
Sudhir Chekuri
Thursday, 3 October 2013
Windows Phone 8 Page Navigation
XAML apps on windows phone use page based navigation model which is similar to web page model. Pages are stateless and they are identified by URI.
Navigation to other pages in Windows Phone
Create a windows phone basic application which comes with a default page named as MainPage.xaml.
Create another page by adding new item (Pivot page) to your solution explorer named as Page1.xaml.
In MainPage.xaml add a xaml hyperlink button control and generate click event to write the below code using which user can navigate from MainPage.xaml to Page1.xaml.
Example to Navigate - XAML code for hyperlink button
<HyperlinkButton Content="HyperlinkButton" Click="HyperlinkButton_Click_1"/>
C# code under hyperling button click event
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri ("/Page1.xaml",UriKind.Relative ));
}
Navigation to before page
Application provides controls to navigate to preceding page and other pages.
The hardware back button is also used to navigate to preceding page.
Sometimes we have to override the hardware back button .If there is a panel opened then user expects the panel to be closed with a click on back button. But, if it redirect to the before page that creates a problem so in those cases we have to override the hardware button.
Example to navigate to before page
Create a new phone application with two pages MainPage.xaml and Page1.xaml.
Add HyperlinkButton controls in each page.
In the first page Hyperlink button click event write the code to navigate to Page1.xaml as shown in the before example.
Xaml code and C# code in Page1.xaml to navigate to MainPage.xaml with a click on hyperlinkbutton
<HyperlinkButton Content="Back" Click="HyperlinkButton_Click_1"/>
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
Exit or Close an app
Don't Use close or exit buttons in your app because users know that they can use back button or start button to leave your app. When user use back button to exit your app your app will be closed. If user launches other app your app will be deactivated. Windows phone automatically handles these events and manages the app life cycle and resources.
When user click on back button when he is on the first page of the app then closing event is raised and the app will be closed.
When the user click on start button deactivated event will be raised and the app will be deactivated.
If the app is deactivated the app will be on back state. It may be launched again from the start screen.
The app in back state can be removed because of lack of resources or uninstallation of app or reboot of device.
The app can be terminated because of unhandled exception or calling terminate method.
It an app is terminated it will be removed from the back state.
XAML apps on windows phone use page based navigation model which is similar to web page model. Pages are stateless and they are identified by URI.
Navigation to other pages in Windows Phone
Create a windows phone basic application which comes with a default page named as MainPage.xaml.
Create another page by adding new item (Pivot page) to your solution explorer named as Page1.xaml.
In MainPage.xaml add a xaml hyperlink button control and generate click event to write the below code using which user can navigate from MainPage.xaml to Page1.xaml.
Example to Navigate - XAML code for hyperlink button
<HyperlinkButton Content="HyperlinkButton" Click="HyperlinkButton_Click_1"/>
C# code under hyperling button click event
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri ("/Page1.xaml",UriKind.Relative ));
}
Navigation to before page
Application provides controls to navigate to preceding page and other pages.
The hardware back button is also used to navigate to preceding page.
Sometimes we have to override the hardware back button .If there is a panel opened then user expects the panel to be closed with a click on back button. But, if it redirect to the before page that creates a problem so in those cases we have to override the hardware button.
Example to navigate to before page
Create a new phone application with two pages MainPage.xaml and Page1.xaml.
Add HyperlinkButton controls in each page.
In the first page Hyperlink button click event write the code to navigate to Page1.xaml as shown in the before example.
Xaml code and C# code in Page1.xaml to navigate to MainPage.xaml with a click on hyperlinkbutton
<HyperlinkButton Content="Back" Click="HyperlinkButton_Click_1"/>
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
Exit or Close an app
Don't Use close or exit buttons in your app because users know that they can use back button or start button to leave your app. When user use back button to exit your app your app will be closed. If user launches other app your app will be deactivated. Windows phone automatically handles these events and manages the app life cycle and resources.
When user click on back button when he is on the first page of the app then closing event is raised and the app will be closed.
When the user click on start button deactivated event will be raised and the app will be deactivated.
If the app is deactivated the app will be on back state. It may be launched again from the start screen.
The app in back state can be removed because of lack of resources or uninstallation of app or reboot of device.
The app can be terminated because of unhandled exception or calling terminate method.
It an app is terminated it will be removed from the back state.