Silverlight instance tutorial

Source: Internet
Author: User

650) this. width = 650; "border =" 0 "alt =" "width =" 340 "height =" 153 "src =" http://www.bkjia.com/uploads/allimg/131228/1T2343956-0.jpg "/>

In the previous article, I learned about the URI ing MECHANISM OF THE Silverlight Navigation framework, and discussed the problem of passing parameters in the Silverlight Navigation framework. Passing parameters between pages in the navigation framework is one of the most common development skills. This article describes how to pass parameters in the Silverlight Navigation.


 

In traditional Web applications, because common Web pages are stateless pages, Cookies, sessions, or ViewState are often used to transmit parameters between pages. In addition, the QueryString parameter is often used to append the parameter to be passed to the current URI link. The shared parameter is between the server side and the client side. In contrast, as a rich client technology, Silverlight's application page belongs to a stateful page. For a simple understanding, the content of the variables defined on the Silverlight client will be stored in the client memory, the current page status will also be saved. In other words, parameters passed between Silverlight pages can define global application-level variables for parameter transfer. However, this method increases the application memory usage and reduces the application running efficiency. Therefore, it is not recommended. The Silverlight application uses another method to pass parameters, which is similar to the traditional method for passing parameters in Web applications. The "QueryString parameter" method is used to transmit parameters between pages.

 

"QueryStringParameter "transmission parameter basic format

 

Parameters are transmitted using the QueryString parameter method, which is often used in traditional Web applications. Silverlight still uses the traditional format for URI parameter transfer. The format is as follows:

 

/Views/Page. xaml? Parameter = Value

In the above link, Page. xaml is a Silverlight client application Page, and the Parameter name to be passed is "Parameter", and its Parameter Value is "Value ". Use "? "Connection Page name and included parameters. You can use the specified API to obtain the included parameter name and value on the target page.
 

The format of multi-parameter transfer between pages is as follows:

 

/Views/Page. xaml? Parameter1 = Value1 & Parameter2 = Value2

The above link uses the "&" symbol to add multiple parameters to the URI for the purpose of transferring multiple parameters.

 

It can be seen from the above that the "QueryString parameter" method mainly transmits parameters through URI, And the transfer method is closely related to the URI address ing of the Silverlight navigation framework.

 

 

 

" QueryString Parameter Method

In the previous URI ing mechanism, we once introduced how to set MapperUri in the App. xaml resource file and transfer a single parameter from the Home page to the About page. The ing rule is:
 

 

<UriMapper: UriMapping Uri = "/About/{parameter}" MappedUri = "/Views/About. xaml? Parameter = {parameter} "/>


 

On the Source Page, use the NavigationService. Navigate method to Navigate the page, with parameters to be passed,

Private void btSend_Click (object sender, RoutedEventArgs e)
{
This. NavigationService. Navigate (new Uri (String. Format ("/About/" + txtQueryString. Text. Trim (), UriKind. Relative ));
}

 

When the Navigation framework is running, the URI address format in the browser is: http: // localhost: 49750/SilverlightNavigationDemoTestPage. aspx #/About/SilverlightChina

Through URI address ing resolution, the actual address will point to "/Views/About. xaml? Parameter = SilverlightChina ",

650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1T234IR-1.png "width =" 306 "height =" 154 "/>
 

 

 

 

Based on the above introduction, if you need to pass multiple parameters to the target page, you need to add a new address ing rule with multiple parameters. For example, add a rule:
 

<UriMapper: UriMapping Uri = "/About/{name} & amp; {url}" MappedUri = "/Views/About. xaml? Name = {name} & amp; url = {url} "/>

Use the NavigationService. Navigate method to Navigate the page, with multiple parameters to be passed:
 

Private void btSendMultiple_Click (object sender, RoutedEventArgs e)
{
This. navigationService. navigate (new Uri (String. format ("/About/" + txtNameString. text. trim () + "&" + txtURLString. text. trim (), UriKind. relative ));
}

 

In the ing rule, {name} and {url} are used as URI generic characters, and "& amp" is the HTML representation of the "&" symbol.

When the Navigation framework is running, the URI address format in the browser is:

Http: // localhost: 49750/SilverlightNavigationDemoTestPage. aspx #/About/% E9 % 93% B6 % E5 % 85% 89% E4 % B8 % AD % E5 % 9B % BD % E7 % BD % 91 & SilverlightChina. net through URI address ing resolution, the actual address will point to "/Views/About. xaml? Name = % E9 % 93% B6 % E5 % 85% 89% E4 % B8 % AD % E5 % 9B % BD % E7 % BD % 91 & url = SilverlightChina. Net"

650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1T234IR-2.png "width =" 381 "height =" 147 "/>

 

 

Read "QueryString parameter" and pass the parameter value

After the Silverlight Navigation framework Passes parameters, you need to use the QueryString attribute in the NavigationContext object to read the name of the passing parameter contained in the current URI to obtain its parameter value. The basic format is as follows:
 

Object parameter = this. NavigationContext. QueryString ["parameter"];
 

For the preceding two routines, the read parameter format is as follows:

Protected override void OnNavigatedTo (NavigationEventArgs e)
{
If (this. NavigationContext. QueryString. ContainsKey ("parameter "))
{
TbReceiveParameter. Text = "received transfer parameter value:" + this. NavigationContext. QueryString ["parameter"];
}
Else
{
TbReceiveParameter. Text = "receiving parameter 1:" + this. NavigationContext. QueryString ["name"] + "receiving parameter 2:" + this. NavigationContext. QueryString ["url"];
}
}


 

When using NavigationContext to read parameters, there are two points:Note:

 

  • Before reading a parameter, check whether the parameter name exists. if no parameter name needs to be obtained in QueryString, an exception occurs in the application. The parameter value can be Null. If the parameter value is Null, Null is returned to the page. The common check code is as follows: NavigationContext. QueryString. ContainsKey (paramName)
  • Parameter type conversion is required before parameter value assignment, because parameter transmission is passed in the form of a string, while page value assignment requires value type conversion as required; otherwise, an exception occurs in the application; the common type conversion code is as follows: int. tryParse (NavigationContext. queryString [paramName], out paramValue)

 

Finally, a URI address ing resolution and matching table is converted from MSDN, including the parameter instances attached to the URI:
 

650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1T2345145-3.png "width =" 719 "height =" 311 "/>
 

 

If you have any questions, please leave a message.

Source code download

 

 

Silverlight instance tutorial series-Silverlight Validation instance

Silverlight instance tutorial series-Silverlight Out-of-Browser instance

Silverlight instance tutorial series-Expression Blend instance Chinese tutorial

 

You are welcome to join the "Focus on Silverlight" QQ technology group. You are welcome to join in to learn and discuss Silverlight & WPF & Widnows Phone development technology.
22308706 (group 1) super group of 500 people
37891947 (group 2) super group: 500 people
100844510 (group 3) Senior Group: 200 persons
32679922 (group 4) super group: 500 people
23413513 (group 5) Senior Group: 200 persons
32679955 (Group 6) super group: 500 people
61267622 (group 7) super group: 500 people
88585140 (group 8) super group: 500 people
128043302 (9 Group Enterprise Application Development Recommendation group) Senior Group: 200 people
101364438 (10 groups) super group: 500 people
68435160 (11 Group Enterprise Application Development Recommendation group) super group of 500 people

This article is from the "Kevin Fan" blog, please be sure to keep this source http://kevinfan.blog.51cto.com/1037293/635792

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.