wpf - 一个程序集中的多个 XAML 资源文件,在另一个程序集中引用
问题描述
晚上好。请提出 WPF XAML 资源问题。
我有一个名为 Resources 的用户控制项目。在这个项目中,我有一个包含多个 XAML 文件的目录。我已将这些资源合并到项目根目录的 Main.xaml 文件中。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources;component/xaml/Buttons.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Images.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Styles.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
Resource 项目编译没有问题,并且您在上面看到的代码中没有视觉错误。
我有一个名为 Buttons 的第二个项目。在 App.xaml 中,我引用了这个 Resource 项目。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我在 Button 项目中引用了 Resources.dll。Visual Studio 在 ResourceDictionary 语句下显示错误行;项目如何编译。
当我运行 Button.exe 时,我收到错误“无法找到资源 'resources;component/xaml/buttons.xaml'”。
我的 ResourceDictionary 语句与我在其他项目中成功完成的相匹配。假设我在 Button 项目中的 App.xaml 中编写的内容是正确的,我应该在 Resources 的 Main.xaml 文件中做些什么不同的事情?
提前致谢。
解决方案
好吧,我想通了。我将记录如何执行此操作,以防其他人遇到此问题。
在我的一个名为 Resources 的程序集中,我在项目根目录中有一个 Main.xaml 文件。我有一个名为“xaml”的子目录,其中有几个 xaml 文件。我的错误是在目录“xaml”的前导反斜杠中。
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="xaml/Buttons.xaml" />
<ResourceDictionary Source="xaml/Images.xaml" />
<ResourceDictionary Source="xaml/Styles.xaml" />
<ResourceDictionary Source="xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
然后,在另一个名为 Buttons 的项目中,在 App.xaml 文件中,我在 MergedDictionaries 部分引用该 Main.xaml 文件。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
一旦我更正了主文件中的引用,一切正常。
因此,这允许我将我的资源拆分为单独的 xaml 文件并在其他程序集中引用它们。哇!
推荐阅读
- python - nbconvert into PDF fails to process exclamation mark in Latex
- c++ - “/usr/bin/ld: 在 Ubuntu 20.04 中编译 opencv 项目时找不到 -llibopencv_calib3d”
- python - 读取整数,直到它提供空输入并打印出最长的递增序列
- spring-boot - 我的 vue 3 前端和 Java spring boot 日期差异问题
- dask - 为什么 dask 返回子图而不是 read_csv().compute() 上的计算?
- c# - 是否可以在堆栈跟踪中获取 NuGet 包的行号?
- performance - perf 记录(或其他分析器)如何选择将哪条指令计为成本时间?
- python - 在 Django 中,如何获取通过请求调用的基于类的视图的特定方法?
- c++ - macOS 上的 SDL2 和 KISS_SDL 链接问题
- php - 我如何制作一个简单的 RewriteRule?