我有一个棱镜应用程序和各种模块.我想知道找到资源的最佳位置在哪里,如样式,画笔,控制模板,数据模板?
我应该制作一个单一的资源字典并将所有内容放在那里 每个模块应该有自己的资源吗?还是每个视图?我想遵循Prism的目标,即保持所有模块化,但我也没有看到在每个模块中重新声明相同资源的重点......
cho*_*dze 31
我用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>
Run Code Online (Sandbox Code Playgroud)现在你可以在任何模块中添加这些资源(你有单独的项目,对吗?)通过将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>
Run Code Online (Sandbox Code Playgroud)我不知道,也许这种方式有一些问题,但它有效,对我来说效果很好.如果有人可以这种方式评论(优点/缺点) - 我会很高兴听到它!
我通常放在应用程序范围内的资源ResourceDictionary
,它被添加到App.xaml
或者StartupWindow.xaml
特定View的资源通常位于View中.例如,用于CalendarView的UserControl将包含日历的任何自定义资源,例如特定于日历的画笔,样式,模板等.
我通常没有看到制作模块范围资源的理由,但是如果我曾经做过的话,我ResourceDictionary
可以在运行时将该模块加载到应用程序的合并字典中,或者包含在模块中的各个视图中.