1. Switch to call different user controls in the same Silverlight project on the ASPX page
1.1. method 1
Modify the application_startup event of the Silverlight project Startup File app. xml
private void Application_Startup(object sender, StartupEventArgs e) { if (!e.InitParams.ContainsKey("InitPage")) { this.RootVisual = new MainPage(); return; } switch (e.InitParams["InitPage"]) { case "SilverlightControl1": this.RootVisual = new SilverlightControl1(); break; case "SilverlightControl2": this.RootVisual = new SilverlightControl2(); break; default: this.RootVisual = new MainPage(); break; } }
Modify the ASPX page
<Div id = "silverlightcontrolhost"> <Object Data = "data: Applications/x-silverlight-2, "type =" application/x-silverlight-2 "width =" 100% "Height =" 100% "> <Param name =" Source "value =" clientbin/binglang. silverlightdemo19.xap "/> <Param name =" initparams "value =" initpage = silverlightcontrol1 "/> <Param name =" onerror "value =" onsilverlighterror "/> <Param name =" Background "value =" white "/> <Param name =" minruntime Version "value =" 3.0.40624.0 "/> <Param name =" autoupgrade "value =" true "/> <a href =" http://go.microsoft.com/fwlink? Linkid = 149156 & V = 3.0.40624.0 "style =" text-Decoration: none; "> </a> </Object> <IFRAME id =" _ sl_historyframe "style = 'visibility: hidden; Height: 0; width: 0; Border: 0px '> </iframe> </div>
1.2. method 2
Modify the application_startup event of the Silverlight project Startup File app. xml
private void Application_Startup(object sender, StartupEventArgs e) { if (!e.InitParams.ContainsKey("InitPage")) { this.RootVisual = new MainPage(); return; } Assembly assembly = Assembly.GetExecutingAssembly(); String rootName = String.Format("Binglang.SilverlightDemo19.{0}", e.InitParams["InitPage"]); UIElement rootVisual = assembly.CreateInstance(rootName) as UIElement; this.RootVisual = rootVisual; }
You can use the following reflection code to obtain the required control:
String rootName = String.Format("Binglang.SilverlightDemo19.{0}", e.InitParams["InitPage"]);
Type type = Type.GetType(rootName ); UIElement rootVisual = Activator.CreateInstance(type) as UIElement; this.RootVisual = (UIElement)this._contentPage;
Modify the ASPX page
<Div id = "silverlightcontrolhost"> <Object Data = "data: Applications/x-silverlight-2, "type =" application/x-silverlight-2 "width =" 100% "Height =" 100% "> <Param name =" Source "value =" clientbin/binglang. silverlightdemo19.xap "/> <Param name =" initparams "value =" initpage = silverlightcontrol1 "/> <Param name =" onerror "value =" onsilverlighterror "/> <Param name =" Background "value =" white "/> <Param name =" minruntime Version "value =" 3.0.40624.0 "/> <Param name =" autoupgrade "value =" true "/> <a href =" http://go.microsoft.com/fwlink? Linkid = 149156 & V = 3.0.40624.0 "style =" text-Decoration: none; "> </a> </Object> <IFRAME id =" _ sl_historyframe "style = 'visibility: hidden; Height: 0; width: 0; Border: 0px '> </iframe> </div>
2. Call the specified control in different Silverlight Projects
2. 1. Create a project
(1) binglang. silverlightdemo20
(2) binglang. silverlightdemow.web
(3) binglang. externalproject
Note: using system. xml. LINQ must be referenced in the binglang. silverlightdemo20 project;
Assume that (1) and (3) each have a control named mainpage. XAML (not necessarily the same)