在另一个程序集中找不到合并的资源字典错误。

4

我在这里真的很苦恼。首先,一些背景信息。该应用程序是一个使用WPF的PRISM 4/MVVM应用程序,我的解决方案结构如下:

MyCompany.MyProduct (MyCompany.MyProduct.exe)
+-- Shell.xaml
       Shell.xaml.cs
    app.config
    (etc)
MyCompany.MyProduct.Infrastructure (MyCompany.MyProduct.Infrastructure.dll)
+-- Resources
|   +-- MyApplicationStyles.xaml
|       SuperCoolImage.png
|       (etc)
+-- BaseClasses
    +-- ViewModelBase.cs
        (etc)
MyCompany.MyProduct.Modules.ModuleA (MyCompany.MyProduct.Modules.ModuleA.dll)
+-- ViewModels
|   +-- StuffViewModel.cs
|       (etc)
+-- Views
+-- StuffView.xaml
       StuffView.xaml.cs
    (etc)

项目引用:

  • MyCompany.MyProduct 引用 MyCompany.MyProduct.Infrastructure
  • MyCompany.MyProduct.Modules.ModuleA 引用 MyCompany.MyProduct.Infrastructure

现在,在Shell.xaml和StuffView.xaml中,我都包含一个合并的字典,如下所示:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="pack://application:,,,/MyCompany.MyProduct.Infrastructure;component/Resources/MyApplicationStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

然而,在两种情况下,我都遇到了以下错误:
Could not load file or assembly 'MyCompany.MyProduct.Infrastructure, Culture=neutral' or one of its dependencies. The system cannot find the file specified.

(叹气)我还注意到XAML编辑器在这一行上显示了两个波浪线错误:
<ResourceDictionary Source="pack://application:,,,/MyCompany.MyProduct.Infrastructure;component/Resources/MyApplicationStyles.xaml"/>

第一个波浪线错误是...
An error occurred while finding the resource dictionary "pack://application:,,,/MyCompany.MyProduct.Infrastrucuture;component/Resources/MyApplicationStyles.xaml".

第二个(更加奇怪的)波浪线错误是...
"Assembly name expected instead of project name."

以下是我尝试过的方法:

  • 我三次确认了项目引用是否存在(并且已经删除并重新添加)
  • 我检查了MyCompany.MyProduct.Infrastructure是否成功构建
  • 我通过AssemblyInfo.cs双重检查了程序集是否正确
  • 我一次又一次地更改了MyApplicationStyles.xaml上的Build Action(目前设置为“Page”)
  • 我手动清理了解决方案并重新构建了它
  • 我重新启动了Visual Studio几次
  • 我献祭了一只山羊(没关系...它是一只非常不友好的山羊)

我已经没有更多的想法,搜索也没有得到很多结果。有什么建议吗?


请仔细检查 MyCompany.MyProduct.Infrastructure 中的命名空间和程序集名称。 - Alex Wiese
2个回答

1
请确保所引用的程序集是针对相同版本的 .Net Framework 进行编译的。

0

我遇到了同样的问题,但有所不同。一个项目的包URI正常工作,而在另一个项目中使用相同的URI却失败了,这真是太棒了!

我解决这个问题的方法是将相关资源作为链接文件从打包URI指定的目标程序集添加到失败项目的根目录中。一旦将链接文件添加到项目中,打包问题就神奇地消失了。

步骤(VS2012) 1.右键单击项目,选择“添加”。然后选择“添加现有项”。 2.浏览其他项目中的资源文件。 3.突出显示XAML文件。然后选择“添加”按钮旁边的箭头下拉菜单。下拉菜单会显示“添加为链接”,选择该选项并将其添加到文件中。

这样,对此链接文件所做的任何更改都会传递到所有项目,并修复与打包相关的奇怪问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接