【发布时间】:2012-01-25 12:31:56
【问题描述】:
我有一个 prism 应用程序和各种模块。我想知道哪里是查找样式、画笔、控件模板、数据模板等资源的最佳位置?
我应该制作一个单一的资源字典并将所有内容都放在那里吗?每个模块都应该有自己的资源吗?还是每个视图?我想遵循 Prism 保持一切模块化的目标,但我也没有看到在每个模块中重新声明相同资源的意义......
【问题讨论】:
我有一个 prism 应用程序和各种模块。我想知道哪里是查找样式、画笔、控件模板、数据模板等资源的最佳位置?
我应该制作一个单一的资源字典并将所有内容都放在那里吗?每个模块都应该有自己的资源吗?还是每个视图?我想遵循 Prism 保持一切模块化的目标,但我也没有看到在每个模块中重新声明相同资源的意义......
【问题讨论】:
我想分享一些新知识。我正在使用@chopikadze 方法。这是非常酷的方法。谢谢你!
但是,如果您不想每次都为每个控件编写这些代码:
<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>
然后你可以像这样在App.xaml 的App.xaml 中声明<ResourceDictionary/>:
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</Application.Resources>
【讨论】:
应用程序范围的资源我通常放在ResourceDictionary 中,添加到App.xaml 或StartupWindow.xaml 中
特定视图的资源通常位于视图中。例如,用于 CalendarView 的 UserControl 将包含日历的任何自定义资源,例如特定于日历的画笔、样式、模板等。
我通常看不到制作模块范围资源的理由,但如果我这样做的话,我会为模块提供ResourceDictionary,它可以在运行时加载到应用程序的合并字典中,或者包含在单个模块中的视图。
【讨论】:
我使用 Prism 开发应用程序,我使用的技术与 Prism 手册中描述的非常接近。有 YourApplication.Infrastructure 项目,您通常会在其中放置所有共享接口等。所以:
创建文件 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>
我不知道,也许这种方式有一些问题,但它很有效,对我来说效果很好。如果有人能以这种方式发表评论(优点/缺点) - 我会很高兴听到它!
【讨论】: