【问题标题】:Loading ResourceDictionary resource at runtime from assemblies with the same name在运行时从同名程序集中加载 ResourceDictionary 资源
【发布时间】:2013-02-26 08:44:01
【问题描述】:

我在尝试从具有同名但位于不同文件夹中的两个单独程序集加载资源时遇到了一些问题:

  • C:\folder1\fcl.dll
  • C:\folder2\fcl.dll

一个名为 Resources.xaml 的ResourceDictionary 嵌入(作为一个页面)在每个这些程序集中。

要加载第一个ResourceDictionary,我使用以下sn-p:

// Load the assembly in memory
var assembly = Assembly.LoadFrom(@"c:\folder1\FCL.Dll");

// Get Dictionnary 
var uri = string.Format("pack://application:,,,/{0};Component/Resources.xaml", assembly.GetName().Name);
var resourceDictionary = new ResourceDictionary { Source = new Uri(uri) };

这正在运行!

但是当我尝试使用相同的 sn-p 代码加载第二个 ResourceDictionary 时(只需将 Assembly.LoadFrom(@"c:\folder1\FCL.Dll") 更改为 Assembly.LoadFrom(@"c:\folder2\FCL.Dll"),它不会从 c:\folder2\fcl.dll 加载资源, 但是那些存储在之前加载的 c:\folder1\fcl.dll :-(

原因:URI的shortAssemblyName字段相同,可能FCL:

 var uri = string.Format("pack://application:,,,/{0};Component/Resources.xaml", assembly.GetName().Name)

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: wpf runtime uri resourcedictionary


    【解决方案1】:

    你不能加载两个同名的程序集,所以我认为第二个 Assembly.LoadFrom 只是被忽略了。您可能必须重命名其中一个程序集。

    【讨论】:

    • 更正:如果您使用强名称并且版本不同,则可以加载多个具有相同名称的程序集。然后,您需要将版本指定为 URI 的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多