查看 Mahapps 项目,有一个资源字典 (Controls.xaml),它在解决方案资源管理器中显示为其他资源字典的“根”。如何才能实现这一目标?
下图显示了 Controls.xaml 项目的缩回和扩展,以及一些合并到 Contorls.xaml 文件中的资源字典。
最佳答案
如果您查看 MahApps.Metro.NET45.csproj 项目文件,您会发现:
<Page Include="Styles\Controls.AnimatedSingleRowTabControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<DependentUpon>Controls.xaml</DependentUpon>
</Page>
对于其他嵌套的 xaml 资源也类似。
如果您查看 Controls.xaml,您会发现它们使用 ResourceDictionary.MergedDictionaries将一些嵌套的 xaml 资源合并到 Controls.xaml 中,如下所示:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Sizes.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Page.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/ValidationErrorTemplate.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Scrollbars.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ListView.xaml" />
等等...
我不确定他们为什么不将所有嵌套的 xaml 控件合并到 Controls.xaml 中。
我认为没有办法在 Visual Studio IDE 中添加 DependentUpon xml 节点,我手动添加并得到以下结果。有addins声称要在 IDE 中添加此功能(我没有尝试过)。
请注意MSDN将 DependentUpon 节点描述为:可选字符串。 指定该文件正确编译所依赖的文件。我不确定这是否会对编译过程产生重大影响。
总之,如果您希望在 IDE 中直观地表示哪些文件相互依赖,请在 csproj 文件中添加 DependentUpon Xml 节点。如果您还想合并资源字典,请在 xaml 中使用 ResourceDictionary.MergedDictionaries。
关于c# - 将资源字典文件分组到一个资源字典文件下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26746042/