Silverlight从动态xap加载导入App.xaml

时间:2011-09-14 18:18:31

标签: silverlight dynamic xap app.xaml

有没有办法访问导入的XAP文件的app.xaml资源(来自主机silverlight应用程序)???

或者更好的是,将访客app.xaml导入主机app.xaml

问题是在导入的Silverlight应用程序中,我丢失了所有app.xaml资源,我只看到了主机资源......我想合并它们......

有可能吗?

我以这种方式加载XAP

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);
}

1 个答案:

答案 0 :(得分:0)

如果您知道资源文件的uri,例如:  “/COMPONENTNAME;component/resourcepath.xaml”,

您可以通过编写以下代码简单地将此资源字典添加到您的应用程序: Application.Current.Resources.MergedDictionaries.Add("*resourceuri*");

“Application.Current”始终指向运行时应用程序实例(主机应用程序),无论它在何处使用。

相关问题