动态加载xap

dotkit 2009-12-07 10:56:22
我在做一个Silverlight项目的时候,想调用另外一个XAP文件里的内容,
使用的代码如下,(HomeDrid是一个Grid控件)
void HomePage_Show(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("DeepZoomProject.xap", UriKind.Relative);

// 用 WebClient 下载指定的 XAP
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(uri);
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
/*
* StreamResourceInfo - 提供对通过 Silverlight 应用程序模型检索资源的支持
* AssemblyPart - 包含在 Silverlight 程序内的程序集
* AssemblyPart.Load() - 加载指定的程序集到当前应用程序域中
* Application.GetResourceStream() - 对 zip 类型的文件自动解压缩
*/

// .xap 的 AppManifest.xaml 的信息如下
/*
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="YYTetris" EntryPointType=".App" RuntimeVersion="2.0.31005.0">
<Deployment.Parts>
<AssemblyPart x:Name="" Source=".dll" />
</Deployment.Parts>
</Deployment>
*/

// 可以通过 Deployment.Current 检索 AppManifest.xaml 中的 Deployment 对象

// 取得 XAP 的 AppManifest.xaml 中的 Deployment.Parts 内的全部 AssemblyPart 节点信息
StreamResourceInfo resource = App.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative));
string resourceManifest = new StreamReader(resource.Stream).ReadToEnd();
List<XElement> assemblyParts = XDocument.Parse(resourceManifest).Root.Elements().Elements().ToList();

Assembly assembly = null;

foreach (XElement element in assemblyParts)
{
// 取出 AssemblyPart 的 Source 指定的 dll
string source = element.Attribute("Source").Value;
AssemblyPart assemblyPart = new AssemblyPart();
StreamResourceInfo streamInfo = App.GetResourceStream(
new StreamResourceInfo(e.Result, "application/binary"),
new Uri(source, UriKind.Relative));

if (source == "DeepZoomProject.dll")
assembly = assemblyPart.Load(streamInfo.Stream);
else
assemblyPart.Load(streamInfo.Stream);
}

// 实例化 .xap 的主函数
var type = assembly.GetType("DeepZoomProject.Page");
var yyTetris = Activator.CreateInstance(type) as UIElement;

// 添加 .xap 到指定的容器内,并更新 UI
HomeGrid.Children.Add(yyTetris);
HomeGrid.UpdateLayout();
}

问题:
如果我是用Blend 或 Visual Studio 进行调试的话,能够加载且正常显示DeepZoomProject.xap里的内容,但如果我找到客户端的网页(default.html)双击直接在浏览器里打开的话,则不能正常显示, 且浏览器的状态栏提示“网页上有错误”。
请问该怎么解决???
...全文
318 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
abis 2010-06-03
  • 打赏
  • 举报
回复
继续来问,呵呵
abis 2010-04-19
  • 打赏
  • 举报
回复
多谢版主,但我遇到了一个小问题,还请你指点一下,我新建了一个项目,生成的XAP包为:MyTest,但我调用的时候出错。
调用格式:
loader.LoadXap(new Uri("MyTest.xap", UriKind.Relative), "MyTest.PageMain");
出错提示为:

行: 1
错误: Unhandled Error in Silverlight 2 Application Could not create instance of requested type. 位于 XapPreloader.XapLoader.wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
位于 System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
位于 System.Net.WebClient.OpenReadOperationCompleted(Object arg)



还有一个小问题,我想想这个包放在我的FRAME中来执行,应该怎么做呢?

多谢了
abis 2010-04-19
  • 打赏
  • 举报
回复
已经解决了,呵呵。但在FRAME中执行还没解决,还请版主指点一下
jv9 2009-12-09
  • 打赏
  • 举报
回复
网址打错了,不让编辑。。。

http://www.silverlightchina.net/resource/code/XapPreloader.zip
jv9 2009-12-09
  • 打赏
  • 举报
回复
到这里下载吧。

http://www.silverlightchina.net/resource/XapPreloader.zip
jv9 2009-12-08
  • 打赏
  • 举报
回复
OK,我做个demo,随后给你。
dotkit 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jv9 的回复:]
试试这个,xaploader

C# codepublicclass XapLoader
{publicevent EventHandler<XapLoaderEventArgs> XapLoaded;privatestring m_rootAssembly;privatestring m_typeName;publicvoid LoadXap(Uri xapUri,string typeName)
{
m_rootAssembly= Path.GetFileNameWithoutExtension(xapUri.ToString());
m_typeName= typeName;

WebClient wc=new WebClient();
wc.OpenReadCompleted+= wc_OpenReadCompleted;
wc.OpenReadAsync(xapUri);
}privatevoid wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{var manifestStream= Application.GetResourceStream(new StreamResourceInfo(e.Result,null),new Uri("AppManifest.xaml", UriKind.Relative));string appManifest=new StreamReader(manifestStream.Stream).ReadToEnd();string assemblyName= m_rootAssembly+".dll";
XmlReader reader= XmlReader.Create(new StringReader(appManifest));
Assembly asm=null;while (reader.Read())
{if (reader.IsStartElement("AssemblyPart"))
{
reader.MoveToAttribute("Source");
reader.ReadAttributeValue();if (reader.Value== assemblyName)
{var assemblyStream=new StreamResourceInfo(e.Result,"application/binary");var si= Application.GetResourceStream(assemblyStream,new Uri(reader.Value, UriKind.Relative));
AssemblyPart p=new AssemblyPart();
asm= p.Load(si.Stream);break;
}
}
}if (asm==null)thrownew InvalidOperationException("Could not find specified assembly.");var o= asm.CreateInstance(m_typeName);if (o==null)thrownew InvalidOperationException("Could not create instance of requested type.");

RaiseXapLoadedEvent(o);
}privatevoid RaiseXapLoadedEvent(object instance)
{if (XapLoaded!=null)
{
XapLoaded(this,new XapLoaderEventArgs(instance));
}
}
}publicclass XapLoaderEventArgs : EventArgs
{publicobject Instance {get;set; }public XapLoaderEventArgs(object instance)
{this.Instance= instance;
}
}
[/Quote]
能给个Demo吗?谢谢!
dotkit 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jv9 的回复:]
OK,我做个demo,随后给你。
[/Quote]
好的, 谢谢您的帮助,如果可以的话, 请发到我的邮箱dot.kit@msn.com,真的十分感谢
jv9 2009-12-08
  • 打赏
  • 举报
回复
试试这个,xaploader


public class XapLoader
{
public event EventHandler<XapLoaderEventArgs> XapLoaded;

private string m_rootAssembly;
private string m_typeName;

public void LoadXap(Uri xapUri, string typeName)
{
m_rootAssembly = Path.GetFileNameWithoutExtension(xapUri.ToString());
m_typeName = typeName;

WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(xapUri);
}

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var manifestStream = Application.GetResourceStream(
new StreamResourceInfo(e.Result, null),
new Uri("AppManifest.xaml", UriKind.Relative));

string appManifest = new StreamReader(manifestStream.Stream).ReadToEnd();
string assemblyName = m_rootAssembly + ".dll";
XmlReader reader = XmlReader.Create(new StringReader(appManifest));
Assembly asm = null;
while (reader.Read())
{
if (reader.IsStartElement("AssemblyPart"))
{
reader.MoveToAttribute("Source");
reader.ReadAttributeValue();
if (reader.Value == assemblyName)
{
var assemblyStream = new StreamResourceInfo(e.Result, "application/binary");
var si = Application.GetResourceStream(assemblyStream, new Uri(reader.Value, UriKind.Relative));
AssemblyPart p = new AssemblyPart();
asm = p.Load(si.Stream);
break;
}
}
}

if (asm == null)
throw new InvalidOperationException("Could not find specified assembly.");

var o = asm.CreateInstance(m_typeName);
if (o == null)
throw new InvalidOperationException("Could not create instance of requested type.");

RaiseXapLoadedEvent(o);
}

private void RaiseXapLoadedEvent(object instance)
{
if (XapLoaded != null)
{
XapLoaded(this, new XapLoaderEventArgs(instance));
}
}
}

public class XapLoaderEventArgs : EventArgs
{
public object Instance { get; set; }

public XapLoaderEventArgs(object instance)
{
this.Instance = instance;
}
}

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧