Windows Phone 7 page navigation

Source: Internet
Author: User

First, let's talk about the presentation of the subject and relationship of the WP7 program. The page that is directly presented to the user is page, and each page is a class inherited from phoneapplicationpage, A program can contain any number of pages. These pages are placed under a common frame, and the frame inherits from phoneapplicationframe. An application can only have one frame. The code for program frame Initialization is contained in the app. XAML. CS file automatically generated when the program is created. In the initializephoneapplication () method

// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;

// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;

// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;

// Ensure we don't initialize again
phoneApplicationInitialized = true;
}

As you can see, the Comment mentioned, do not add the code to this method, so we should not do the same.
When we change the attributes of a page, it does not affect other pages. However, once the frame is changed, all pages will be affected, we should think that the frame is read-only (though not ).

The app class is the main program class. I didn't deliberately use it to operate on global attributes. For example, obtaining a frame is the rootframe attribute.

Page navigation method

Page navigation is generally divided into two methods: Code navigation, and XAML declared navigation

1. Code Implementation

The navigationservice class provides the navigation function, navigationservice. navigate (New uri ("/Newpage. XAML ", urikind. (relative); Put this code in the button click event, click the button to jump to Newpage. in the XAML page, naviagte accepts a URI-type parameter. Here, the string path and path type urikind are input. urikind is an enumeration type, and the navigation on one side is a relative path. WP7 features starting from the root directory, "/" indicates the root directory, input the file path in sequence, Newpage. the XAML file is stored in the root directory, so the path is written as "/Newpage. XAML ", if Newpage. in the View folder of the root directory, the path of XAML is "/View/Newpage. XAML ".

2. XAML implementation

You can use a navigation control such as hyperlinkbutton to write

The above two methods achieve the same purpose. clicking a button or hyperlinkbutton will jump to Newpage. XAML

 

The path alias can be used in the WP7 page navigation, which is also inherited from Silverlight.

First, register the windows. Navigation namespace on the app. XAML file. Code: mlns: navigate = "CLR-namespace: system. Windows. Navigation; Assembly = Microsoft. Phone ",

Then, register the resource Code as follows:

<Application.Resources>
<navigate:UriMapper x:Key="UriMapper">
<navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/>
</navigate:UriMapper>
</Application.Resources>

Finally, add urimapper: This. rootframe. urimapper = resources ["urimapper"] As urimapper to the frame. Write the code to the app class constructor.

Usage

Navigationservice. navigate (New uri ("Newpage", urikind. Relative ));

<Hyperlinkbutton navigateuri = "Newpage" content = "Goto new page" Height = "30" horizontalalignment = "Left" margin =, 200 "verticalalignment =" TOP "width =" "/>

You can directly enter an alias in the path, and the running effect is the same.

Passing parameters between pages

The method for passing parameters on a page is the same as that for passing parameters on a webpage. Add "? Parameter Name = parameter value"

Navigationservice. navigate (New uri ("/Newpage. XAML? Id = 10 ", urikind. Relative ));

<Hyperlinkbutton navigateuri = "/Newpage. XAML? Id = 10 "content =" Goto new page "Height =" 30 "horizontalalignment =" Left "margin =" 200, "verticalalignment =" TOP "width =" "/>

In this way, we navigate to Newpage. XAML and pass the ID and its value to Newpage. XAML.

Method for accepting parameters in Newpage. XAML

The querystring attribute of navigationcontext stores all the parameters passed on the previous page. The code below gets the ID and displays its value with MessageBox.

if (NavigationContext.QueryString.ContainsKey("ID"))
{
MessageBox.Show(NavigationContext.QueryString["ID"]);
}

If you use an alias in the navigation bar, you can easily pass the parameter and change the resource

<Application.Resources>
<navigate:UriMapper x:Key="UriMapper">
<navigate:UriMapping Uri="NewPage" MappedUri="/NewPage.xaml"/>
<navigate:UriMapping Uri="NewPage/{param}" MappedUri="/NewPage.xaml?ID={param}"/>
</navigate:UriMapper>
</Application.Resources>

In this way

Navigationservice. navigate (New uri ("Newpage/10", urikind. Relative ));

<Hyperlinkbutton navigateuri = "Newpage/10" content = "Goto new page" Height = "30" horizontalalignment = "Left" margin = ", 92, 200 "verticalalignment =" TOP "width =" "/>

You can pass id = 10 to Newpage. XAML,

Because two navigation paths to urimapping are defined in different ways, the parameter passing method is different. By default, parameters are passed to Newpage. XAML without aliases, and the path can be written in three ways.

"/Newpage. XAML? Id = 10 "," Newpage? Id = 10 "," Newpage/id = 10 ", the same effect.

 

Page passing parameters are a way to share data between pages. There are also two methods to compare and create data sharing, one is global variables, such as using static classes, this method is relatively simple for static members of the app class and will be used by C # Kids shoes. Another method is to use the isolatedstorage class, which is also integrated from Silverlight and called independent storage. Independent storage plays an important role in the declaration period and data sharing of WP7 programs, I will explain it in detail when introducing the Declaration CYCLE OF THE WP7 program. Here, I only want to write about his simplest usage. Don't worry about kids shoes that you don't understand.

Write in mainpage. XAML. CS

private void btnNavigate_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
iss["ID"] = 10;
NavigationService.Navigate(new Uri("NewPage", UriKind.Relative));
}

 

Write in Newpage. XAML. CS

IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
if (iss.Contains("ID"))
{
MessageBox.Show(iss["ID"].ToString());
}

The final running is the same as that of navigationservice jump.

 

My nickname on Sina Weibo is "@ Ma vegetables". Please pay more attention to it. Thank you.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.