1.在aspx页面中切换调用同一个SilverLight项目中的不同用户控件

 

1.1.       方法一

修改SilverLight项目启动文件App.xml的Application_Startup事件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
 
           }
 
 
 
       }

 修改aspx页面

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<div id="silverlightControlHost">
 
           <object data="data:application/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="minRuntimeVersion" 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;">
 
                     <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight" style="border-style: none"/>
 
                </a>
 
           </object><iframe id="_sl_historyFrame" style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>

 

 1.2.      方法二

 

 

修改SilverLight项目启动文件App.xml的Application_Startup事件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
 
 
 
      }

 以上反射取得所需控件,也可以用下面反射代码:

 

 

1
2
String rootName = String.Format("Binglang.SilverlightDemo19.{0}", e.InitParams["InitPage"]);

 

1
2
3
4
Type type = Type.GetType(rootName );
  UIElement rootVisual = Activator.CreateInstance(type)  as UIElement;
 
 this.RootVisual  = (UIElement)this._contentPage;

 

修改aspx页面

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<div id="silverlightControlHost">
 
           <object data="data:application/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="minRuntimeVersion" 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;">
 
                     <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight" style="border-style: none"/>
 
                </a>
 
           </object><iframe id="_sl_historyFrame" style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>

 2.调用不同SilverLight项目中的指定控件 

 

2.1.建立项目

(1)Binglang.SilverlightDemo20

(2)Binglang.SilverlightDemo20.Web

(3) Binglang.ExternalProject

 

注意:项目Binglang.SilverlightDemo20中需要引用using System.Xml.Linq;

 

假设(1)和(3)中各有一个控件,名称都为MainPage.xaml (不一定要相同)