silverlight 4,动态加载xap模块

syn*_*tic 6 silverlight assembly-resolution xap

我知道可以使用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; }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码工作正常; 我可以通过以下方式加载任何xap:

XapLoader xapLoader = new XapLoader("Sales.xap");
xapLoader.Completed += new XapLoadedEventHandler(xapLoader_Completed);
xapLoader.Begin();
Run Code Online (Sandbox Code Playgroud)

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

Sales.InvoiceView view = new Sales.InvoiceView();
Run Code Online (Sandbox Code Playgroud)

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

Gon*_*ing 1

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

如果删除项目/模块,输出 DLL/XAP 文件会继续存在。如果单击“显示所有文件”按钮,您将在相关项目的clientbinbinobj文件夹中看到一些剩余的输出文件。

替代文本

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

希望这可以帮助。