动态加载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)双击直接在浏览器里打开的话,则不能正常显示, 且浏览器的状态栏提示“网页上有错误”。
请问该怎么解决???
...全文
317 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;
}
}

内容概要:本文提出了一种针对两级电力市场环境下省间交易商的最优购电模型,重点考虑了现货市场与中长期市场的联动机制及市场风险因素,旨在帮助交易商在复杂多变的电力市场环境中制定科学合理的购电策略。模型以Matlab为工具实现,通过构建包含市场电价波动、负荷需求不确定性等风险源的优化框架,运用先进的数学规划方法求解最小化购电成本或风险调整后成本的决策方案。文中系统阐述了模型的目标函数设计、约束条件设定、风险量化机制(如VaR、CVaR)以及求解算法流程,并通过实际算例验证了模型在提升购电经济性、增强风险抵御能力方面的有效性与实用性。; 适合人群:适用于具备一定电力系统基础知识、运筹学理论背景及Matlab编程能力的高校学生、科研人员以及电力市场运营、能源交易等领域的专业从业者。; 使用场景及目标:①用于全国大学生数学建模竞赛A题等相关赛事中电力市场类问题的研究与求解;②为省级及以上电力交易机构和售电公司在跨区购电决策中提供计及风险的量化分析工具,实现成本控制与风险规避的平衡;③作为高等院校电力市场、能源经济等课程的教学案例,深化对现代电力市场机制与风险管理方法的理解。; 阅读建议:读者在学习过程中应重点关注模型对风险的建模方式与优化算法的具体实现细节,建议结合所提供的Matlab代码进行调试与仿真,通过改变市场参数、风险偏好和约束条件等方式开展敏感性分析,以深入掌握模型的适应性与鲁棒性。

8,757

社区成员

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

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