gpt4 book ai didi

silverlight 4,动态加载 xap 模块

转载 作者:行者123 更新时间:2023-12-04 15:48:50 26 4
gpt4 key购买 nike

我知道可以使用 Prism 或 MEF 框架动态加载 xap 模块。但是,我不想使用这些框架;而是手动加载我的 xap 文件。所以,我创建了以下类(改编自互联网):

public class XapLoader
{
public event XapLoadedEventHandler Completed;

private string _xapName;

public XapLoader(string xapName)
{
if (string.IsNullOrWhiteSpace(xapName))
throw new ArgumentException("Invalid module name!");
else
_xapName = xapName;
}

public void Begin()
{
Uri uri = new Uri(_xapName, UriKind.Relative);
if (uri != null)
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += onXapLoadingResponse;
wc.OpenReadAsync(uri);
}
}

private void onXapLoadingResponse(object sender, OpenReadCompletedEventArgs e)
{
if ((e.Error == null) && (e.Cancelled == false))
initXap(e.Result);

if (Completed != null)
{
XapLoadedEventArgs args = new XapLoadedEventArgs();
args.Error = e.Error;
args.Cancelled = e.Cancelled;
Completed(this, args);
}
}

private void initXap(Stream stream)
{
string appManifest = new StreamReader(Application.GetResourceStream(
new StreamResourceInfo(stream, null), new Uri("AppManifest.xaml",
UriKind.Relative)).Stream).ReadToEnd();

XElement deploy = XDocument.Parse(appManifest).Root;

List<XElement> parts = (from assemblyParts in deploy.Elements().Elements()
select assemblyParts).ToList();

foreach (XElement xe in parts)
{
string source = xe.Attribute("Source").Value;
AssemblyPart asmPart = new AssemblyPart();
StreamResourceInfo streamInfo = Application.GetResourceStream(
new StreamResourceInfo(stream, "application/binary"),
new Uri(source, UriKind.Relative));
asmPart.Load(streamInfo.Stream);
}
}
}

public delegate void XapLoadedEventHandler(object sender, XapLoadedEventArgs e);

public class XapLoadedEventArgs : EventArgs
{
public Exception Error { get; set; }

public bool Cancelled { get; set; }
}

上面的代码工作正常;我可以通过以下方式加载任何 xap:
XapLoader xapLoader = new XapLoader("Sales.xap");
xapLoader.Completed += new XapLoadedEventHandler(xapLoader_Completed);
xapLoader.Begin();

现在,我在 Sales.xap 项目中有一个名为 InvoiceView 的 UserControl,所以我想实例化该类。在当前项目(Main.xap)中,我添加了对 Sales.xap 项目的引用,但是,由于我手动加载它,我设置了“Copy Local = False”。但是在执行时,下面的代码会抛出 TypeLoadException:
Sales.InvoiceView view = new Sales.InvoiceView();

似乎代码找不到 InvoiceView 类。但是我检查了 XapLoader 的 initXap() 方法是否成功执行。那么为什么代码找不到 InvoiceView 类呢?有人可以帮我解决这个问题吗?

最佳答案

这是基于提问者在下面的自我回答,而不是问题。

如果您删除一个项目/模块,输出 DLLs/XAP 文件确实会挂起。如果单击“显示所有文件”按钮,您将在相关项目的 clientbin、bin 和 obj 文件夹中看到这些剩余的输出文件。

alt text

您可以从项目中单独删除它们,或者,如有疑问,搜索所有 BIN 和 OBJ(例如使用桌面资源管理器)并删除所有这些文件夹。 BIN/CLIENTBIN/OBJ 文件夹将在需要时重新创建(这是 Visual Studio 中的“干净”选项应该完成的工作!)

希望这可以帮助。

关于silverlight 4,动态加载 xap 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3673718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com