wp8.1 Development Tutorial One: page navigation and value transfer between pages

Source: Internet
Author: User
Tags tostring visibility

1, page navigation

Using the Frame.navigate () method, the C # statement is as follows:

The code is as follows Copy Code

1 Frame.navigate (typeof (Page2));//page2 as the name of a page



The note here is that this method can be overloaded, that is, the method of value transfer between pages described later.

2. Value transfer between pages

This uses the Frame.navigate () overload method to deliver a single value and multiple value passes

1 A single value delivery: can pass characters, numbers and other data, such as

In the Page1.xaml.cs

The code is as follows Copy Code

1 Na=mytextbox.text;
2 Frame.navigate (typeof (Page2), NA);



And in Page2.xaml.cs, receiving data

The code is as follows Copy Code

Receive the passed data in the Onnavigate () method

protected override void Onnavigatedto (NavigationEventArgs e)
{

Mytextblock.text = E.parameter.tostring ();
}


2) Multi-value transfer: The basic method is the same as the idea of a single value transfer method.

The reference method is as follows:

First create a new class

  code is as follows copy code

namespace Navigatesample
{
    public class Navigatecontext
    {
         public string name {get ; Set
         public int id {get; set;}
        public Navigatecontext (string name, int id)
          {
             This.id = ID;
             this.name = name;
        }
   }
}



And then using the Navigatecontext class to carry out multiple value transfer

The code is as follows Copy Code

Navigatecontext na = new Navigatecontext (mytextbox.text, (int) myslider.value);
Frame.navigate (typeof (Page2), NA)//mytextbox is textbox.name in page page, Myslider is in page page Slider.name


And in Page2.xaml.cs, receiving data

  code is as follows copy code

         protected override void Onnavigatedto (NavigationEventArgs e)
         {
            var na = ( Navigatecontext) E.parameter;
            Mytextblock1.text =na.name;
            mytextblock2.text = na.id.ToString ();
       }



Summary: The above knowledge is learned in Bob's video, if there is anything wrong welcome to point out!


Theoretical knowledge

First, page navigation

1, Windows, frames, page relationships (Windows, Frames and pages)



Windows contains a single frame, Size in the area of 100%. The
frame contains pages that are also typically 100% of the size of Windows.
applications can have multiple windows in a Windows Store application's flat/PC. In WP, the application has only one window. The

 2, frame, and navigation return stack (the frame and the navigation Backstack)

Frame is created when the application is started, and the frame acts as a container for page pages. And when you navigate to other pages, it saves a navigation history.

Example: 1 Navigating through the page, C # code is as follows:

The code is as follows Copy Code
private void Itemlistview_itemclick (object sender, Itemclickeventargs e)
{
Navigate to the appropriate destination page, configuring the new page
By passing required information as a navigation parameter
var itemId = ((Mylistviewitem) e.clickeditem). UniqueId;
Frame.navigate (typeof (Mydetailpage), itemId);
}



2 navigation back, C # code:

  code is as follows copy code
private void Btngoback_click (         object sender, RoutedEventArgs e)
 {
   if (this. Frame.cangoback)
      this. Frame.goback ();
 }



3, Return key note item

1 standard Windows Phone UX (user experience) is to use the return key to navigate the back or to close the instant UI. By default, the return key causes navigation back to the previous application, not the page!
2 in Windows Phone Store application, you must include code rewriting which causes the application to navigate backwards.
3 Unlike the Windows Phone's Silverlight (ie WP8 design): In an application, backward page navigation is the default frame
4 Rewrite Return key: The blank template is a processing code that does not contain the return key, and the Navigationhelper class is included in the hub template to navigate the page correctly. If you need to rewrite the return key to replace your own code custom navigation processing, the following is the rewrite return key C # code in a page:

The code is as follows Copy Code
Public sealed partial class Secondpage:page
{
...

protected override void Onnavigatedto (NavigationEventArgs e)
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed + = hardwarebuttons_backpressed;
}

protected override void Onnavigatedfrom (NavigationEventArgs e)
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed-= hardwarebuttons_backpressed;
}

Async void Hardwarebuttons_backpressed (object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
E.handled = true; We ' ve handled this button press

If the secondary UI is visible, hide it
if (secondaryui.visibility = = visibility.visible)
{
Fadeoutsecondarystoryboard.begin ();
}
Else
{
Standard Page Backward Navigation
if (Frame.cangoback)
Frame.goback ();
}
}


If the entire app requires a return key rewrite, you can write similar code into the APP.XAML.CS file.

4. Page Cache mode

When you first navigate to a page type, a new instance (page) is created. When you leave this page, the instance is cached or corrupted by the Navigationcachemode property of the page.

Navigationcachemode.disabled: Whether you navigate the page forward or backwards, you get a new instance of the page
Navigationcachemode.enabled: The page can be cached, but when the cache exceeds a certain value (the default in WP8.1 is 1, determined by the Frame.cachesize property), the cache instance is discarded

Navigationcachemode.required: The page can be cached, and regardless of the size of the frame (frame.cachesize), the cached instance can be reapply for each access, i.e. the content of the page is still the same.

5, Navigationhelper class

You can use the Navigationhelper class to save and overload the state of the page. In the underlying page template, the Navigationhelper class is loaded automatically, which we can use to load this class.

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.