Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > Silverlight >

SilverLight中调用自定义用户控件

时间:2010-08-14 08:39来源:未知 作者:admin 点击:

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

 

1.1.       方法一

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

  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页面

<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事件

 

        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;

 

        }

 

修改aspx页面

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

 

myButton.Click += new RoutedEventHandler(myButton_Click);

 

        void myButton_Click(object sender, RoutedEventArgs e)

        {

            WebClient client = new WebClient();

            client.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

 

            //打开打包的xap文件

            client.OpenReadAsync(new Uri("Binglang.ExternalProject.xap", UriKind.Relative));

 

        }

 

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

        {

//通过AppManifest.xaml文件取出动态库的信息

             Assembly asm = LoadAssemblyFromXap(e.Result, "Binglang.ExternalProject.dll");

 

            ////使用反射创建相关的实例,并在页面上加载

            holder.Children.Clear();

            UIElement element = asm.CreateInstance("Binglang.ExternalProject.MainPage") as UIElement;

            this.holder.Children.Add(element);

 

        }

 

        /// <summary>

        /// 通过AppManifest.xaml文件取出动态库的信息

        /// </summary>

        /// <param name="packageStream">OpenReadCompletedEventArgs e.Result</param>

        /// <param name="assemblyName">动态库文件名</param>

        /// <returns></returns>

   Assembly LoadAssemblyFromXap(Stream packageStream, string assemblyName)

        {

            //解包,读取AppManifest.xaml文件信息

            string appManifest = new StreamReader(Application.GetResourceStream(new StreamResourceInfo(packageStream, null), new Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd();

 

            //------------解析AppManifest.xaml信息内容

 

            XElement deploymentRoot = XDocument.Parse(appManifest).Root;

            List<XElement> deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements()

                                              select assemblyParts).ToList();

 

            Assembly asm = null;

            foreach (XElement xElement in deploymentParts)

            {

                string source = xElement.Attribute("Source").Value;

                AssemblyPart asmPart = new AssemblyPart();

                StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(packageStream, "application/binary"), new Uri(source, UriKind.Relative));

                if (source == assemblyName)

                {

                    asm = asmPart.Load(streamInfo.Stream);

                }

                else

                {

                    asmPart.Load(streamInfo.Stream);

                }

            }

            return asm;

        }

(责任编辑:admin)
Tags:Silverlight
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价