晚上好。关于WPF XAML资源的问题。
我有一个名为资源的用户控制项目。在这个项目中,我有一个包含多个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>
资源项目的编译没有问题,并且在上面看到的代码中没有视觉错误。
我有第二个项目叫做Buttons。在App.xaml中,我引用了这个资源项目。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我在按钮项目中引用了Resources.dll。Visual Studio在ResourceDictionary语句下显示一条错误行;
当我运行Button.exe时,我得到错误"无法定位资源"resources;component/xaml/buttons.xaml'.">
我的ResourceDictionary语句与我在其他项目中成功完成的语句匹配。假设我在App.xaml中在Button项目中编写的内容是正确的,那么我应该在参考资料的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文件中,我引用了Main。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
一旦我纠正了主文件中的引用,一切都正常工作了。
所以这允许我将我的资源分割成单独的xaml文件,并在其他程序集中引用它们。Wohoo !
另外,为了完整起见,对于每个xaml文件(包括main.xaml),将Build Action设置为Page(右键单击xaml文件并选择Properties)。