As we all know, the application in Silverlight 2ProgramEach application generates an xap file. Only one initial user control can be set in each xap file. If we have multiple user controls, we need. NET page load, the simplest way is to create a corresponding Silverlight project for multiple user controls, but this method has many disadvantages, for example, we need to copy the Style File in multiple projects.
This article describes how to use initialization parameters to switch between user controls.
Preparation
Shows how to create a project structure. In the Silverlight project, we have three User Controls: contentpage, defaultpage, and masterpage, which must be in different ASP. different user controls are displayed when the. NET page is loaded.
Ideas
It is not difficult to implement this function. We can use the initparams attribute, as shown in:
Alternatively, use Param in HTML to specify initparameters:
This attribute is of the <string, string> type. You can set a series of key-Value Pair initialization parameters, separated by commas. Therefore, our idea is very simple: Specify the starting user control through initparameters on the ASP. NET page or HTML, then obtain the parameters in the application_startup event, and set rootvisual.
Implementation
As shown in the following sectionCode, We set an initialization parameter initpage to contentpage:
<ASP:SilverlightID= "Xaml1"Runat= "Server"Source= "~ /Clientbin/switchusercontrol. xap"Minimumversion= "2.0.30523"Width= "100%"Height= "100%"Initparameters= "Initpage = contentpage"/>
Then, in application_startup, set different rootvisual based on different parameters:
Private void Application_startup (Object Sender, Startupeventargs E ){ If (! E. initparams. containskey ( "Initpage" )){ This . Rootvisual = New Defaultpage (); Return ;} Switch (E. initparams [ "Initpage" ]) { Case "Masterpage" : This . Rootvisual =New Masterpage (); Break ; Case "Contentpage" : This . Rootvisual = New Contentpage (); Break ; Default : This . Rootvisual = New Defaultpage (); Break ;}}
After running the program, you can see that the initial user control is contentpage, as shown in:
Improvement
Although the above method achieves our goal, the switch statement code is not very elegant. If there are dozens of user controls, there must be dozens of branches. Since we have set the initial user control name in the initial parameter, why not use reflection directly? In Silverlight 2, launch is well supported, so our code can be modified as follows:
Private void Application_startup ( Object Sender, Startupeventargs E ){ If (! E. initparams. containskey ( "Initpage" )){ This . Rootvisual = New Defaultpage (); Return ;}Assembly Assembly = Assembly . Getexecutingassembly (); String Rootname = String . Format ( "Switchusercontrol. {0 }" , E. initparams [ "Initpage" ]); Uielement Rootvisual = assembly. createinstance (rootname) As Uielement ; This . Rootvisual = rootvisual ;}
Now the code looks much better. Even if there are more user controls, you don't have to modify the code here. However, you must pay attention to the following problem: You must divide the Silverlight project reasonably and do not place all user controls in one project to avoid the xap file being too large. I will write an article laterArticleLet's talk about how to reasonably divide the Silverlight project structure and how to call user controls in other xap files.
Summary
This article is actually very simple. I wrote this article because many of my friends have recently asked this question, so I would like to explain it here, the initparameters attribute is used in combination with reflection to implement switching of user controls. Hope to help you.