开发学院图形图像Flash 技巧:在Silverlight中如何访问外部xap文件中User... 阅读

技巧:在Silverlight中如何访问外部xap文件中UserControl

 2008-10-09 11:38:53 来源:WEB开发网   
核心提示: Assembly LoadAssemblyFromXap(Stream packageStream,String assemblyName){Stream stream = Application.GetResourceStream(new StreamResourceInfo(packa

Assembly LoadAssemblyFromXap(Stream packageStream,String assemblyName)
{
  Stream stream = Application.GetResourceStream(
      new StreamResourceInfo(packageStream, null),
      new Uri("AppManifest.xaml", UriKind.Relative)).Stream;
  String appManifestString = new StreamReader(stream).ReadToEnd();
  Deployment deployment = (Deployment)XamlReader.Load(appManifestString);
  Assembly assembly = null;
  foreach (AssemblyPart assemblyPart in deployment.Parts)
  {
    if (assemblyPart.Source == assemblyName)
    {
      String source = assemblyPart.Source;
      StreamResourceInfo streamInfo = Application.GetResourceStream(
        new StreamResourceInfo(packageStream,
        "application/binary"),
        new Uri(source,UriKind.Relative));
      assembly = assemblyPart.Load(streamInfo.Stream);
      break;
    }
  }
  return assembly;
}

得到程序集后,再使用反射创建相关的实例,并在页面上加载,如下代码所示:

Assembly assembly = LoadAssemblyFromXap(e.Result, "ExternalProject.dll");
UIElement element = assembly.CreateInstance("ExternalProject.SubPage") as UIElement;
this.holder.Children.Add(element);

运行后效果如下图所示:

技巧:在Silverlight中如何访问外部xap文件中UserControl

跨域访问

在上面的示例中,不涉及到跨域(我会专门写一篇文章介绍)调用的问题,如果大家想访问的xap文件与当前xap文件不在同一站点中,需要添加跨域访问文件,如下代码所示:

clientaccesspolicy.xml:

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
 <cross-domain-access>
  <policy>
   <allow-from http-request-headers="*" />
   <domain uri="*"/>
   </allow-from>
   <grant-to>
    <resource path="/" include-subpaths="true"/>
   </grant-to>
  </policy>
 </cross-domain-access>
</access-policy>
总结

本文介绍了在Silverlight中如何访问外部xap文件这一技巧,希望对大家有所帮助。

上一页  1 2 3 4 

Tags:技巧 Silverlight 如何

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接