Obtain the initparams initialization parameter of Silverlight in the C # code.

Source: Internet
Author: User
Tags silverlight

What we learned today is that we will use a convenient method provided by Silverlight to achieve this: when a Web page is added, the specified parameter (or information) transfer from web page to Silverlight, Which is initparams.
We can use it to pass related information such as the page url to Silverlight (of course, other information can also be passed ).
Initparams information is stored in string/value pairs. We will learn how to set and read them. Next we will start our experiment.
Create a Silverlight application as usualProgramName: slinitparamsfromwbtosl. :

1. Place the initparams information we will pass on the web page (the initparams information sets the format and location ).
The webpage is the host page where the Silverlight control is placed (in this example, the content of the slinitparamsfromwbtosltestpage. ASPX page). The Code is:

<% @ Page Language = "C #" autoeventwireup = "true" %>
<% @ Register Assembly = "system. Web. Silverlight" namespace = "system. Web. UI. silverlightcontrols"
Tagprefix = "asp" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml" style = "height: 100%;">
<Head runat = "server">
<Title> slinitparamsfromwbtosl </title>
</Head>
<Body style = "height: 100%; margin: 0;">
<Form ID = "form1" runat = "server" style = "height: 100%;">
<Asp: scriptmanager id = "scriptmanager1" runat = "server"> </ASP: scriptmanager>
<Div style = "height: 100%;">
<Asp: Silverlight id = "xaml1" runat = "server" Source = "~ /Clientbin/slinitparamsfromwbtosl. xap "minimumversion =" 2.0.31005.0 "width =" 100% "Height =" 100% "initparameters =" Australia = mebourne, China = Chengdu, USA = Washington "/>
</Div>
</Form>
</Body>
</Html>

We can see that there is a siverlight Control <asp: Silverlight id = "xaml1"/>, and our initparams information will be placed in it, because it has an initparams parameter, set the format:

Initparameters = "Australia = mebourne, China = Chengdu, USA = Washington"

This is the information of the initparams key-value pair to be passed to Silverlight.
2. Read the passed initparams information in Silverlight.
The above page (slinitparamsfromwbtosltestpage. when aspx is added, the initparameters information is passed to the Silverlight application, that is, to the Silverlight app, how can we obtain initparameters information in Silverlight application?
We know that when we create a Silverlight application, Visual Studio automatically creates four files for us: app. XAML, app. XAML. CS, page. XAML, and page. XAML. CS. generally, we only pay attention to the following two files, but when we want to obtain initparams, we must pay attention to the app. XAML and app. XAML. CS file.
The content of the app. XAML. CS file is as follows:

Public partial class app: Application
{
Public app ()
{
This. startup + = This. application_startup;
This. Exit + = This. application_exit;
This. unhandledexception + = This. application_unhandledexception;
Initializecomponent ();
}
Private void application_startup (Object sender, startupeventargs E)
{
This. rootvisual = new page ();
}
Private void application_exit (Object sender, eventargs E)
{
}
Private void application_unhandledexception (Object sender, applicationunhandledexceptioneventargs E)
{}
Private void reporterrortodom (applicationunhandledexceptioneventargs E)
{}
}

In this file, we need to pay special attention to the application_startup method, which is executed when the Silverlight application startup event is triggered. Its startupeventargs input parameter contains an attribute named initparams, which contains the information we set in the first step. At present, we have only one way to obtain information in initparams.
There are two ways to implement page. XAML. CS backend.CodeInitparams information.
(1) Method 1: add an attribute in APP. XAML. CS to realize the accessibility of initparameters information ..
1. Add the property: myinitparams. The Code is as follows:

Public System. Collections. Generic. idictionary <string, string> myinitparams {Get; set ;}

2. assign a value to the property myinitparams in application_startup.

Private void application_startup (Object sender, startupeventargs E)
{
This. rootvisual = new page ();
// At this location, we can find E. initparams, which is the initialization parameter of the Silverlight program.
This. myinitparams = E. initparams; // assign a value to the myinitparams attribute we created above.
}

The app. XAML. CS code modified in the preceding two steps is as follows:

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using system. collections;
Namespace slinitparamsfromwbtosl
{
Public partial class app: Application
{
Public System. Collections. Generic. idictionary <string, string> myinitparams {Get; set ;}
Public app ()
{
This. startup + = This. application_startup;
This. Exit + = This. application_exit;
This. unhandledexception + = This. application_unhandledexception;
Initializecomponent ();
}
Private void application_startup (Object sender, startupeventargs E)
{
This. rootvisual = new page ();
// At this location, we can find E. initparams, which is the initialization parameter of the Silverlight program.
This. myinitparams = E. initparams;
}
Private void application_exit (Object sender, eventargs E)
{
}
Private void application_unhandledexception (Object sender, applicationunhandledexceptioneventargs E)
{
// If the application runs outside the debugger, use the browser's
// The exception Mechanism reports this exception. On IE,
// The Yellow alarm icon displays the exception, while Firefox displays a script error.
If (! System. Diagnostics. Debugger. isattached)
{
// Note: This allows the application to handle exceptions that have been thrown.
// Continue running.
// For production applications, this error handling should be replaced with reporting errors to the website
// And stop the application.
E. Handled = true;
Deployment. Current. Dispatcher. begininvoke (delegate {reporterrortodom (e );});
}
}
Private void reporterrortodom (applicationunhandledexceptioneventargs E)
{
Try
{
String errormsg = E. exceptionobject. Message + E. exceptionobject. stacktrace;
Errormsg = errormsg. Replace ('"', '\''). Replace ("\ r \ n", @ "\ n ");
System. Windows. browser. htmlpage. Window. eval ("throw new error (\" unhandled error in Silverlight 2 Application "+ errormsg + "\");");
}
Catch (exception)
{
}
}
}
}

3. Access myinitparams information in page. XAML. CS.
I. Access value by given key

This.txt bxkey. Text = "cities in Australia:" + MyApp. myinitparams ["Australia"];

Ii. Traverse initparams content

Foreach (string key in MyApp. myinitparams. Keys)
{
This.txt bxvalue. Text + = Key + ":" + MyApp. myinitparams [Key] + "\ n ";
}

The page. XAML. CS code is as follows:

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Namespace slinitparamsfromwbtosl
{
Public partial class page: usercontrol
{
Public page ()
{
Initializecomponent ();
Loaded + = new routedeventhandler (page_loaded );
}
Private void page_loaded (Object sender, eventargs E)
{
APP MyApp = application. Current as app;
// Access the corresponding value by key value
This.txt bxkey. Text = "cities in Australia:" + MyApp. myinitparams ["Australia"];
// Traverse the content of initparams
Foreach (string key in MyApp. myinitparams. Keys)
{
This.txt bxvalue. Text + = Key + ":" + MyApp. myinitparams [Key] + "\ n ";
}
}
}
}

(2) Method 2: The initparameters information can be accessed by transforming the page () constructor of page. XAML. CS.
1. In the application_startup method of APP. XAML. CS code, modify the code as follows (pass E. initparams as a parameter to the page constructor ):

This. rootvisual = new page (E. initparams );

2. Modify the page () constructor in page. XAML. CS to include an input parameter.

Public page (idictionary <string, string> initparams)
{
Initializecomponent ();
}

The code for page. XAML. CS is as follows:

Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Namespace slinitparamsfromwbtosl
{
Public partial class page: usercontrol
{
Public page (idictionary <string, string> initparams)
{
Initializecomponent ();
This.txt bxkey. Text = "cities in Australia:" + initparams ["Australia"];
// Traverse the content of initparams
Foreach (string key in initparams. Keys)
{
This.txt bxvalue. Text + = Key + ":" + initparams [Key] + "\ n ";
}
}
}
}

Effect of program execution:

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.