Customize and call the Silverlight Control

Source: Internet
Author: User

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)




















              Contact Us

              The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

              If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

              A Free Trial That Lets You Build Big!

              Start building with 50+ products and up to 12 months usage for Elastic Compute Service

              • Sales Support

                1 on 1 presale consultation

              • After-Sales Support

                24/7 Technical Support 6 Free Tickets per Quarter Faster Response

              • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.