自定义SilverLight控件并调用

8/18/2010来源:Silverlight人气:8617

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

 

1.1.       方法一

修改SilverLight项目启动文件App.xmlapplication_Startup事件

 

view sourcePRint?01 private void Application_Startup(object sender, StartupEventArgs e) 

02  

03        { 

04  

05            if (!e.InitParams.ContainsKey("InitPage")) 

06  

07            { 

08  

09                this.RootVisual = new MainPage(); 

10  

11                return; 

12  

13            } 

14  

15            switch (e.InitParams["InitPage"]) 

16  

17            { 

18  

19                case "SilverlightControl1": 

20  

21                    this.RootVisual = new SilverlightControl1(); 

22  

23                    break; 

24  

25                case "SilverlightControl2": 

26  

27                    this.RootVisual = new SilverlightControl2(); 

28  

29                    break; 

30  

31                default: 

32  

33                    this.RootVisual = new MainPage(); 

34  

35                    break; 

36  

37            } 

38  

39   

40  

41        }

 修改aspx页面

 

 

view sourceprint?01 <div id="silverlightControlHost"> 

02   

03            <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" > 

04   

05                 <param name="source" value="ClientBin/Binglang.SilverlightDemo19.xap"/> 

06   

07                 <param name="InitParams" value="InitPage=SilverlightControl1" /> 

08   

09                 <param name="onerror" value="onSilverlightError" /> 

10   

11                 <param name="background" value="white" /> 

12   

13                 <param name="minRuntimeVersion" value="3.0.40624.0" /> 

14   

15                 <param name="autoUpgrade" value="true" /> 

16   

17                 <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none;"> 

18   

19                      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight" style="border-style: none"/> 

20   

21                 </a> 

22   

23            </object><iframe id="_sl_historyFrame" style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>

 


 1.2.      方法二

 

 

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

 

view sourceprint?01 private void Application_Startup(object sender, StartupEventArgs e) 

02  

03       { 

04  

05           if (!e.InitParams.ContainsKey("InitPage")) 

06  

07           { 

08  

09               this.RootVisual = new MainPage(); 

10  

11               return; 

12  

13           } 

14  

15  

16  

17           Assembly assembly = Assembly.GetExecutingAssembly(); 

18  

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

20  

21           UIElement rootVisual = assembly.CreateInstance(rootName) as UIElement; 

22  

23           this.RootVisual = rootVisual; 

24  

25  

26  

27       }

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

 

 

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

 

view sourceprint?1 Type type = Type.GetType(rootName ); 

2   UIElement rootVisual = Activator.CreateInstance(type)  as UIElement; 

3   

4  this.RootVisual  = (UIElement)this._contentPage;

 

修改aspx页面

 

view sourceprint?01 <div id="silverlightControlHost"> 

02   

03            <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" > 

04   

05                 <param name="source" value="ClientBin/Binglang.SilverlightDemo19.xap"/> 

06   

07                 <param name="InitParams" value="InitPage=SilverlightControl1" /> 

08   

09                 <param name="onerror" value="onSilverlightError" /> 

10   

11                 <param name="background" value="white" /> 

12   

13                 <param name="minRuntimeVersion" value="3.0.40624.0" /> 

14   

15                 <param name="autoUpgrade" value="true" /> 

16   

17                 <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none;"> 

18   

19                      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight" style="border-style: none"/> 

20   

21                 </a> 

22   

23            </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 (不一定要相同)