简述;
在VS2015中,屏幕可以在运行时正确引用外部ResourceDictionary
文件,但在设计时无法正确显示。为什么会这样?
在工作中,我们有一个WinForms产品,其中包含许多WinForms屏幕,其中一个开发人员正在不断添加新的屏幕,还有一些WPF屏幕,我负责添加新的屏幕。注意到现有WPF屏幕中有很多重复的代码/样式,我创建了一个单独的项目来存储这些内容 - 可以被所有现有/未来的WPF屏幕引用。 项目:WpfHelper
- 平台目标:任何CPU
- 目标框架:.NET Framework 4.6
WpfHelper.dll
部署到...\Trunk\Project\Externals
...\Trunk\Utilities\WpfHelper\WpfHelper\Resources\GlobalResources.xaml
- 构建操作:页面
- 包含通用样式的ResourceDictionary
我在六个项目中引用了...\Trunk\Project\Externals\WpfHelper.dll
,并在每个资源文件中添加了以下代码:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfHelper;Component/Resources/GlobalResources.xaml" />
</ResourceDictionary.MergedDictionaries>
所有的屏幕都位于
...\Trunk\Project\Plugins
中。╔══════════╦═════════════════╦═════════════════╦══════════════════╦════════════════════════════════════════════════╗
║ ║ Resource Works? ║ Platform Target ║ Target Framework ║ Reference File Path ║
╠══════════╬═════════════════╬═════════════════╬══════════════════╬════════════════════════════════════════════════╣
║ Project1 ║ Succeeded ║ Any CPU ║ .NET 4.6 ║ ...\Project1\Project1\Resources\Resources.xaml ║
║ Project2 ║ Succeeded ║ x86 ║ .NET 4.6 ║ ...\Project2\Project2\Resources\Resources.xaml ║
║ Project3 ║ Succeeded ║ Any CPU ║ .NET 4.6 ║ ...\Project3\Project3\Resources\Resources.xaml ║
║ Project4 ║ Failed ║ x86 ║ .NET 4.6 ║ ...\Project4\Project4\Resources\Resources.xaml ║
║ Project5 ║ Failed ║ x86 ║ .NET 4.6 ║ ...\Project5\Project5\Resources\Resources.xaml ║
║ Project6 ║ Failed ║ Any CPU ║ .NET 4.6 ║ ...\Project6\Project6\Resources\Resources.xaml ║
╚══════════╩═════════════════╩═════════════════╩══════════════════╩════════════════════════════════════════════════╝
最近的变化
我最近将Visual Studios 2013升级到了2015版本。在同一时间,其他屏幕开发人员将所有现有屏幕项目的目标框架从.NET Framework 3.5 / 4.0升级到了.NET Framework 4.6。
成功的项目
- 在最近的变化之前,我引用了
WpfHelper.dll
。 - 样式在设计时和运行时都正确应用。
失败的项目
- 在最近的变化之后,我引用了
WpfHelper.dll
。 - 样式只在运行时正确应用。
- 在设计时,会抛出以下错误:
查找资源字典“pack://application,,,/WpfHelper;component/Resources/GlobalResources.xaml”时发生错误。
- 当使用本地
Resources.xaml
时,会抛出以下错误:值不能为 null。 参数名: item
我尝试过的方法:
阅读了大量的文章和问答后:
- 设计时 WPF XAML 资源字典
- 如何在 WPF 中在设计时加载 ResourceDictionary
- 在单独的程序集中查找资源字典出错
- .NET 4.0 中合并资源字典时的问题
- Windows Presentation Foundation 中的 Pack URIs
- VS2013:查找资源字典时发生错误
- 查找资源字典时发生错误
- 在另一个程序集中查找合并的资源字典出错
- 有问题的引用包含合并字典的资源字典
- WPF VS2013:查找资源字典时发生错误
- 应用程序在正常运行但设计时出现“名称空间不存在”和“查找资源字典时出错”的“错误”
- 链接文件作为资源字典
- Visual Studio 2015 XAML 资源字典错误
我尝试了以下所有方法,但都无济于事:
- Change project Platform target to "Any CPU"
- Change project Target framework from/to .NET Framework 4.6
- Ensure all Resource.xaml files had the same Build Action (Page), etc.
Checked each project's
AssemblyInfo.cs
file. Failing projects included (and I removed) the following:[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
Manually cleared the temp folder in AppsData, Cleaned & Rebuilt projects.
- Re-opened projects.
- Rebooted.
- Right-click copied
WpfHelper.dll
references from working projects and pasted into references of failing projects. - Added the
WpfHelper.Resources.GlobalResources.xaml
as a Linked file to the failed projects. - Nagged every co-worker.
- Added the full pack URI for sub-dictionaries used in
GlobalResources.xaml
. - Killed the designer and rebuilt the project.
我已经没有任何想法,也迷失在研究中。我已经模板化了一个可行的解决方案,并将其用于创建新屏幕 - 这样可以在设计时成功显示新屏幕。问题出在现有屏幕上。如何使失败的项目在设计时正确地显示样式资源?