In the previous custom Tile system, the Uri of the Tile system is fixed and written to the program. If you modify the Uri, the program needs to be regenerated. How can we change the Uri dynamically? This document describes how to pass parameters to a webpage before Silverlight initialization.
1. Prepare Parameters
Because the Silverlight program uses web pages as the carrier, it is necessary to pass parameters to Silverlight on the webpage. Here we use WebForm as an example to use web. config to store parameters (changing to a database is also a truth ).
First, add the server address and request string format in the appSettings node of the web. config file:
<configuration> <appSettings> <add key="server" value="localhost:58591"/> <add key="format" value="http://{0}/GetTile.ashx?type=china&format=png&quadkey={quadkey}"/>
</appSettings></configuration>
2. Read Parameters
In the background, use WebConfigurationManager to read configurations.
public string Server{ get { return WebConfigurationManager.AppSettings["server"]; }}public string Format{ get { return WebConfigurationManager.AppSettings["format"]; }}
3. PASS Parameters
Here is the core step, that is, using InitParams to pass parameters to the Silverlight program, the following is the main front-end code (for other content, refer to copy the content of the test page automatically generated by ):
<Object data = "data: application/x-silverlight-2, "type =" application/x-silverlight-2 "width =" 100% "height =" 100% "> <param name =" source "value =" ClientBin/MapOnBing_Silverlight.xap "/> <param name =" onError "value =" onSilverlightError "/> <param name =" background "value =" white "/> <param name =" minRuntimeVersion "value =" 5.0.61118.0 "/> <param name = "autoUpgrade" value = "true"/> <param name = "InitParams" value = "Server = <% = Server %>, Format = <% = Format %>"/> <a href = "http://go.microsoft.com/fwlink? LinkID = 149156 & v = 5.0.61118.0 "style =" text-decoration: none "> </a> </object>
Basically, the content of the test page is directly copied. The key modification is:
<param name="InitParams" value="Server=<%=Server %>,Format=<%=Format %>" />
Here we can pass parameters to the Silverlight program. Multiple parameters can be separated by commas.
4. Receive Parameters
In the Silverlight program, find the App. open the background code App of The xaml file. xaml. cs: Find the Application_Startup event processing method. You can use the InitParams attribute of StartupEventArgs to receive parameters from the foreground. The following is a sample code:
Public partial class App: Application {private void Application_Startup (object sender, StartupEventArgs e) {this. rootVisual = new MainPage (); // read the dynamically set address if (e. initParams. count> 0) {// Save the read configuration to the MainPage in the static variable. server = e. initParams ["Server"]. toString (); MainPage. format = e. initParams ["Format"]. toString ();} else {// when the configuration is not read, you can set a default value MainPage. server = "localhost"; MainPage. format = "http: // {0 }/GetTile. ashx? Type = china & format = png & quadkey = {quadkey }";}}}
In this way, the parameters of the page are read from the Silverlight program.
5. Use Parameters
public partial class MainPage : UserControl{ public static string Server; public static string Format;}
The rest is how to use it. Let's make full use of it ~