Silverlight navigation (Multi-page navigation and value passing) 3

Source: Internet
Author: User
This is mainly followed by frame, mainly about the page control and the use of templates.
In the previous example, usercontrol is used as the page, but usually we use the page control or inherit the page class to replace usercontrol. Because page provides more convenient navigation and status management. In fact, the page class inherits from usercontrol, and then some members are added, some rewritable methods and four attributes: title, navigationservice, navigationcontext, and navigationcachemode. Among them, the titel attribute is relatively simple and not much to be said. Other attributes will be mentioned later.
Using the page control is simple, just like adding usercontrol to a project.
1. Page control attributes
Each page control has a navigationservice attribute, which is equivalent to accessing the Silverlight navigation system because it provides the same method and attribute as the frame object (navigate (), Goback (), goforward (), attributes include cangoback, cangoforward, currentsource, and so on ). This means you can navigate the page ..

  • This. navigationservice. navigate (New uri ("/page2.xaml", urikind. Relative ));

The page class also contains a navigationcontext attribute used to access the navigationcontext object. You can use this attribute to obtain the current URL and querystring to obtain parameters in the URL. That is to say, you can use the address bar parameters to pass values when you jump to the page. As follows:

  • String uritext = string. Format ("/product. XAML? Id = {0} & type = {1} ", productid, producttype );
  • Mainframe. navigate (New uri (uritext), urikind. Relative );

In this way, you can upload two values to the target page ..
Then you can get the value on the priduct. XAML page:

  • Int productid, type;
  • If (this. navigationcontext. querystring. containskey ("productid "))
  • Productid = int32.parse (this. navigationcontext. querystring ["productid"]);
  • If (this. navigationcontext. querystring. containskey ("type "))
  • Type = int32.parse (this. navigationcontext. querystring ["type"]);

You can also use other methods to pass values, such as storing values in Appliaction objects or using independent storage, because URL parameters are easily tampered ..

Save Page Status
Generally, when a user enters the page for the first time or switches to the page using the forward and backward buttons, an object is created again. When the user leaves, the object is released. In this case, if the user inputs information and returns to the page, the default value is programmed, and other members of the page are initialized to the default value. This is not the case if the page status can be stored.
Silverlight allows you to use the page. navigationcachemode attribute to set the storage policy. The default value of this attribute is disabled, so the page is not stored by default. If you set the attribute to required, the page will be saved to the memory. When the user leaves the page and returns the result, the modified content still exists. However, returning to the page again does not trigger the page construction method, so pay attention to the logic you write in the constructor. However, this will trigger the page loaded event.
Another value of navigationcachemode is enabled. If this value is set, the page is similar to frame. the cachesize attribute is associated with the number of pages to be saved. The cachesize attribute is set to 10. When 11th pages are stored, the first page is released. When the navigationcachemode attribute is set to required, the page is not calculated in cachesize. This can be selected based on your own needs.
Page Control Method
The page class contains several methods for you to manage navigation more flexibly.

  • Onnavigatedto (): called when a page is no longer an active page in the framework.
  • Onnavigatingfrom (): called when the page becomes an activity page in the framework.
  • Onnavigatedfrom (): called when a page is no longer an active page in the framework.

You can use these methods to add some logic when you leave the page or access the page, such as initializing some parameters or managing other states.
For more information about the page class, see msdn for detailed usage and instructions ..

Navigation templates
Now I have learned how to use the Frome/Page Control to create applications with navigation functions. However, there are still many differences in visual effects. However, you can simulate others' instances to improve your performance. Another method is to use the existing template.
If you use the Silverlight navigation Appliaction template that comes with vs to create a program, a set of default styles will be automatically created for you. The running effect is as follows:


This template can meet some basic requirements: the top of the page is the navigation button, and the bottom is the content panel. This is the default template, and the Silverlight team also provides several other templates that have been shown in the previous article. You can download them from the attachment in this article. The specific usage is to replace the style. XAML file and it will be OK .. more:










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.