Silverlight3 加载其他xap

有些时候我们在设计有集成sl系统的时候总会想把xap作为基点来达到持续集成的效果。那么我们应该怎么做呢?
解决方法:

1.首先我们要用一个方法实现从源资中提取出Assembly:

Assembly LoadAssemblyFromXap(Stream packageStream, String assemblyName)
        {
            StreamResourceInfo resouceInfo = new StreamResourceInfo(packageStream, "application/binary");
            Stream mainfestStream = Application.GetResourceStream(resouceInfo, new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
              String appManifestString = new StreamReader(mainfestStream).ReadToEnd();
 
            XElement deploymentRoot = XDocument.Parse(appManifestString).Root;
            List<XElement> deploymentParts = (from assemblyParts in deploymentRoot.Elements().Elements() select assemblyParts).ToList();
            Assembly targetassembly = null;
            foreach (XElement xElement in deploymentParts)
             {
                 String source = xElement.Attribute("Source").Value;
                  if (source == assemblyName)
                  {
                     StreamResourceInfo streamInfo = Application.GetResourceStream(resouceInfo, new Uri(source, UriKind.Relative));
                     AssemblyPart asmPart = new AssemblyPart();
                     targetassembly = asmPart.Load(streamInfo.Stream);
                 }
             }
             return targetassembly;
           }

2.利用一个webClient下载指定的其他xap文件,并在下载完成后,利用上边的方法把加载后的资料还完成一个UIElement,这样我们就可以使用了.

 void MainPage_Loaded(object sender, RoutedEventArgs e)
   {
       //加一个xap文件所在的位置
      Uri address = new Uri("http://localhost:4456/ClientBin/exproject.xap");
       //实例一个webClient
      WebClient webClient = new WebClient();
       //注册一个下载完成事件
      webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
     //开始下载
       webClient.OpenReadAsync(address); 
   }
 

3.下载完成后通过LoadAssemblyFromXap方法还原xpa为UIElement:

  void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
   {
       Stream stream = Application.GetResourceStream(
           new StreamResourceInfo(e.Result, null),
           new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
       String appManifestString = new StreamReader(stream).ReadToEnd();
  
       Assembly assembly = LoadAssemblyFromXap(e.Result, "exproject.dll");//other project name.dll 
       UIElement element = assembly.CreateInstance("exproject.MainPage") as UIElement;//other project name.mainPage 
       this.contaner.Children.Add(element);
  }

原文: http://www.pin5i.com/showtopic-26068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值