我有一个棱镜应用程序和各种模块。我想知道哪里是找到样式、画笔、控件模板、数据模板等资源的最佳位置?
我应该制作一个资源字典并将所有内容都放在那里吗?每个模块应该有自己的资源吗?还是每个视图?我想遵循 Prism 保持一切模块化的目标,但我也不认为在每个模块中重新声明相同的资源有什么意义......
我使用 Prism 开发应用程序,并且使用与 Prism 手册中描述的技术非常接近的技术。有一个 YourApplication.Infrastruct 项目,您通常在其中放置所有共享接口等。所以:
- 我只是添加项目 YourApplication.Resources
- 在那里创建文件夹主题
- 在 Themes 文件夹中为每组资源创建单独的 xaml 文件(例如用于标准 WPF 控件样式的 Generic.WPF.xaml、用于画笔的 Generic.Brushes.xaml 等)
-
创建文件 Themes\Generic.xaml (正是使用这个名称,它将在未来带来巨大的好处),其内容如下
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic.Brushes.xaml"/>
<ResourceDictionary Source="Generic.WPF.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
-
现在,您可以通过向该项目添加对 YourApplication.Resources 的引用并添加到视图的 xaml 中,在任何模块中添加这些资源(您有单独的项目,对吧?):
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Put your not shared resource here -->
</ResourceDictionary>
</UserControl.Resources>
我不知道,也许这种方式有一些问题,但它有效,而且对我来说效果很好。如果有人能以这种方式发表评论(优点/缺点) - 我会很高兴听到它!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)