Windows Phone 8.1 new Features page navigation

Source: Internet
Author: User

This article describes how to implement page navigation in Windows Phone 8.1.

The implementation of page navigation in Windows Phone 8 is certainly not unfamiliar, we use navigationservice to achieve. The specific wording is this:

Navigationservice.navigate (New Uri ("XAML Relative Path", Urikind.relativeorabsolute));

In the navigation target page, we receive the parameters of the page passing by navigationcontext.querystring["key" in the onnavigatedto method.

When the back button is pressed, the navigation source page is taken out of the fallback stack, so we complete a complete page navigation process.

In Windows Phone 8.1, navigation is different, first we need to do the following on the navigation source page:

Frame.navigate (typeof (Navigationdemopage), null);

We used the Navigate method of page content control frame, which has two parameters, the first parameter is the type of the navigation target page, and the second parameter (optional) is the parameter passed between pages.

On the target page, we use navigationhelper to complete the accept parameters and other processing. When we create a new basic page (Basicpage) in the project, the Navigationhelper class and some other auxiliary classes appear in the Common folder.

Navigationhelper has two very important event loadstate and savestate, which are used to load the saved state and save the status of the current page, respectively. The receive navigation parameter uses the LoadState event to see how we handle it in its registration method:

private void Navigationhelper_loadstate (object sender, Loadstateeventargs e)
{
    var param = e. Navigationparameter;
}

Unlike the number of string parameters that can be passed in Windows Phone 8, there is only one navigation parameter in 8.1 and the object type, which means that we can pass many custom types, including strings, which are much more convenient for parameter passing and use.

When you press the back key, the page returns to the navigation source. And if we do not use Navigationhelper, the page is no way to return to normal, this is how it, we look at the Navigationhelper constructor:

Public navigationhelper (Page page) {this.
    
            page = page;
            When this page is part of a visual tree, make two changes://1 to map application view state to the visible state of the page//2) Handle hardware navigation requests This. page.loaded + = (sender, E) => {#if Windows_phone_app Windows.Phone.UI.Input.HardwareButton
s.backpressed + = hardwarebuttons_backpressed; #else///Only when the entire window is occupied, the keyboard and mouse navigation apply if (this. Page.actualheight = = Window.Current.Bounds.Height && this.
                    Page.actualwidth = = Window.Current.Bounds.Width) {//Direct listening window, so no focus Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated + + coredispatcher_acceleratorkeyact
                    ivated; Window.Current.CoreWindow.PointerPressed = this.
                corewindow_pointerpressed;
    
            } #endif};
 Undo the same change when the page is no longer visible           This. page.unloaded + = (sender, E) => {#if Windows_phone_app Windows.Phone.UI.Input.HardwareButt Ons.
backpressed-= hardwarebuttons_backpressed; #else Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated-= coredispatcher_a
                cceleratorkeyactivated; Window.Current.CoreWindow.PointerPressed = this.
corewindow_pointerpressed;
        #endif}; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

We see that there is a conditional compiler windows_phone_app. In the page with Navigationhelper, Windows.Phone.UI.Input.HardwareButtons.BackPressed is added to the Loaded and unloaded event handling code, This is the response to the Hardware rollback button click, and the response event is as follows:

private void Hardwarebuttons_backpressed (object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            if (This. Gobackcommand.canexecute (NULL))
            {
                e.handled = true;
                This. Gobackcommand.execute (null);
            }
        
Public Relaycommand Gobackcommand
        {
            get
            {
                if (_gobackcommand = null)
                {
                    _gobackcommand = new Relaycommand (
                        () => this. GoBack (),
                        () => this. CanGoBack ());
                }
                return _gobackcommand;
            }
            Set
            {
                _gobackcommand = value;
            }
        }

We saw that the event was handled with Gobackcommand to navigate to the newest item in the navigation history.

The most important thing to note is that in Windows Phone 8.1, when the page navigation is rolled back, the navigation source page will reload, and for pages that need to load data, the result of this reload is not what we want to see.

Here we can use Navigationcachemode to avoid this phenomenon, we set it in the page construction method, it is an enumeration type, there are three kinds of enumeration values:

Disabled pages are not cached and the page is reloaded when the navigation returns

Required cache pages without regard to cache size

Enabled caching of pages, but discards caching when the cache size limit is exceeded.

All right, here we have the knowledge about the page navigation in Windows Phone 8.1, I hope to help you, 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.