动态载入xap文件

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;

        }



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/manimanihome/archive/2009/08/29/4498244.aspx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值