Introduction to Windows Phone 7-Navigation Framework principles

Source: Internet
Author: User
Tags silverlight

If you know something about Silverlight or WPF development when learning about Windows Phone 7 development, I believe the Navigation Framework must be yours.

. Therefore, when I was learning Windows Phone 7 Application Development, I felt that it would take some time to understand the underlying layer.

Principles.

 

First, the Application of Windows Phone 7 is mainly based on the Silverlight Page Model, providing users with the ability to direct the specified Page and change different content,

Therefore, you can create many pages to browse within the same Frame. InFrameAndPageThe two operational relationships are described in detail below.

 

Here, I have to thank the author who has always supported me for writing such an article with interest. I would like to thank the author again for a very good wp7 Development Forum, I will also post several high-quality articles to you later. Please contact me on the noodles.

Enter the subject:

 

> PhoneApplicationFrame and PhoneApplicationPage:

You can understand the relationship between the two. They are all important components in Microsoft. Phone. Controls Namespace:

()PhoneApplicationFrame(PhoneApplicationFrame Class ):

A WP7 Application has a Frame, which is used to Control pages, controls, and other elements.

Controls the rendering of the entire WP Application, such as Orientation Property and Render range, and includes Status Bar and ApplicationBar.

The above is the description on MSDN. The PhoneApplicationFrame functions include:

Exchange of data between A-1. PhoneApplicationPage;

A-2. control content and controls throughout the ApplicationPage with App. xaml

A-3. control switching of PhoneApplicationPage

Pay special attention to the following Events: Navigated, Navigating, NavigationFailed, and NavigationStopped,

After Navigate (), GoBack (), or GoForward () is proposed in ApplicationPage, control is transferred to Frame for processing through four events.

 

(B)PhoneApplicationPage(PhoneApplicationPage Class ):

It is a Page class. The Page class provides events and services for running the Navigation Framework. Therefore, in some Sample Code, the Navigate (),

In fact, they all operate through the Page class and are transferred to PhoneApplicationFrame for orientation tasks. PhoneApplicationPage generates a copy

When a Xaml file is created, it is assigned a corresponding parameter. The content of this parameter can include all the information in the User Control, and of course, includes the metadata, chart, and so on. In addition, for Orientation

OrientationChanged the Event. If your code requires a special direction, this Event will help you deal with a lot of things.

 

 

This section describes the basic elements for running the entire Windows Phone Application. Next, it describes how elements are mutually oriented and moved.

In the process, I think we need to understand:

 

> Important elements of Page Navigation-Navigation Framework:

Windows Phone Applications are composed of Silverlight Pages. In order to allow Pages to be browsed and connected to each other,

"Navigation Stack 」. The Stack stores pages of all browsing processes through the First in first out concept, helping to return to the target Page when the Back key is pressed.

However, MSDN also mentioned that "over-using Page may cause the Application to store excessive residual Page information in the navigation stack.

Performance and reliability to a certain extent 」.

 

For Page-class navigation tasks, you can use NavigationService. Navigate (), GoBack (), or GoForward ().

The Page impact and operation process can be divided into five necessary Event Handler:

> Loaded(Events triggered after each page is loaded .)

> Unloaded(Events triggered when the page is directed to another page .)

> OnNavigatingFrom(This event is triggered when you exit the current page through NavigationService .)

> OnNavigatedFrom(Via NavigationService, this event is triggered after you exit the current page .)

The relationship between OnNavigatingFrom and OnNavigatedFrom is sequential. Generally, events are handled in OnNavigatedFrom. This does not mean

You cannot add tasks to OnNavigatingFrom for processing, but the OnNavigatingFrom parameter is triggered when the current page is not

Before using it, we can use it to complete some tasks, such as disabling the animation currently being played.

> OnNavigatedTo(The OnNavigatedTo event of the new page is automatically triggered when you go to the new page through NavigationService .)

 

The sequence in which each XAML file loads pages can be described as follows:

(A) When you want to go from MainPage. xaml to SecondPage. xaml, OnNavgatingFrom and OnNavigatedFrom are triggered to end this page;

(B) then, SecondPage. xaml triggers the OnNavigationTo event and forwards the information to be used on this page through the previous page;

(C) When MainPage. xaml completes NavigateService-oriented SecondPage. xaml, the Unloaded event is triggered;

(D) At last, SecondPage. xaml triggers the Loaded event to start loading the required components and content of the page;

 

For the method for uploading the second Page of documents, refer to the introduction in this article: <How to: Perform Page Navigation on Windows Phone>,

It passes parameters in the URL through NavigateService. Navigate (), and uses the OnNavigatedTo () page to process the obtained data.

The above operation sequence will affect the exchange and storage of data during Page exchange.

 

Of course, you can also directly convert the information to be shared between pages into an example:

By using App. xaml to control the information exchanged between pages, there isBenefits:You only need to process Application access through App. xaml

DeActive and Actived tasks for storing and obtaining information. Every Page is not required to handle the impact caused by Tombstoning.

 

In addition, when using Navigate, I see in other files that the Sample will perform some processing and instructions on NavigateContext. Then let's take a look at what it is:

> Page. NavigationContext:

The content that specifically handles the Navigation Request. the method mentioned above that transmits parameters through the Url is to place the content to be passed in the NavigationContext category.

Obtain the parameter value through the QueryString attribute.

 

====

The above section describes how WP7 works with the use of the Navigation Framework. In fact, this article has many basic principles to help you

They are more aware of the operating principles of the rules, so that they will not always encounter some fundamental problems in development, and spend a lot of time on the road.

I have been learning WP7 for some time. In fact, throughout the development process, I encountered many principles. Because I have no solid foundation for the Silverlight principle,

Therefore, it is often difficult to understand why the screen cannot appear normally or fail to guide, or even how to exit the App developed by yourself through Back.

This kind of development experience is actually deeply touched by me. In particular, I will learn the experiences of Silverlight and WP7 and write some necessary concepts through this article.

 

[Supplement]

> Comparison of WP7 page change practices-PhoneApplicationPage. Content and PhoneApplicationPage. NavigationService:

(A) Use Content for Page feed. In fact, the original Page still exists, but the Content is converted into a new screen.

The content in the Page will be overwritten. When you press the Back key, the screen will not return to the previous one, but there will always be the same page.

(B) Use NavigationService to change pages. In fact, the current Page of the Frame control is specified as another new Page. The original Page content will also be

Retained. When you press the Back key, you can return to the previous Page. Note that the Page returned by the Back key is the generated instance,

It is not a brand new Page.

 

> PhoneApplicationFrame and RootVisual:

Open App. xaml. cs and you may find this Code:

// Do not add any additional code to this method
private void CompleteInitializePhoneApplication( object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;

// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}

I don't know if you are as curious as me, What is RootVisual.

In fact, at the beginning of the entire Application operation, when the Page or Frame is not actually loaded (that is, when the program displays the splash screen), the Application is

There is no main screen, so you can see that when App. xaml is in the InitializePhoneApplication () method, the following code is written:

 // 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 ;
}

A set of rootframes (namely, PhoneApplicationFrame) are initialized at the beginning of the Code. When the RootFrame completes the Navigated event processing, it is called

CompleteInitializePhoneApplication () event handler specifies the RootFrame to the RootVisual to let the application know who the main screen is.

However, Frame becomes the main image, while PhoneApplicationPage is only the Content in the Frame.

In other words,RootVisual is used to obtain or set the UI of the main application.. So we can control all pages through Frame. However,

It has an inherent condition: "You can set the RootVisual attribute value only once from the code, but you can obtain the value without limit .」 Pay special attention to this.

 

> Exit WP7 Program:

Although this title is not a very new topic, if you want to leave your program, you can directly handle it by throwing an Exception,

You can leave your program, but there are other ways. Through the Namespace Microsoft. XNA. Game

To Exit. You can refer to it.

 

 

 

> Application Page Model of Windows Phone:

Application Page Model is the key to the entire operation of WP7. It is associated with the Navigation Framework described in this article. Therefore, if,

If there is not enough detail, you can search for the keyword "Application Page Model" directly. In addition, we can see it in the MSDN file.

The difference between Page and Screen is:

 

  Page Screen
  A user-recognizable collection of persistent state. Not a user-recognizable collection of persistent state
  As a Silverlight page that contains information, memorable content, or links to other pages. Such as a pop-up window, dialog box, or splash screen.

> Select the recommendations for Navigation on MSDN:

A. Screens and Non-Navigational Transitions

For transient UI (short-lived UI), we recommend that you use Pop-Up Control to render the content. Learn the BackKeyPress event to hide the content of PopUp (or Dialog box.

B. Multiple Content Views

We recommend that you use multiple DataContext components to interact and present information with your operations. Of course, you can use

It is easier to use multiple pages, but you must note that the Back key should be processed to avoid storing excessive stack information.

C. Saving State and Tombstoning

Note the processing of the current state of code storage. Because the program may be converted to tombstone, You can retain the current state or knowledge through the NavigationContext API.

Do not direct the status and information of the two sides to further control the information content.


I hope you like my article! If you have more ideas, please contact me in the Q & a area of the wp7 Development Forum (codewp7.com). I will be glad to know what you are thinking. At the same time, I can also find my figure in wp7 chat QQ Group 172765887. Thank you and welcome everyone to pay attention to my meager content (www.weibo.com/codewp7)




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.