【问题标题】:WPF Prism - Where to put Resources?WPF Prism - 在哪里放置资源?
【发布时间】:2012-01-25 12:31:56
【问题描述】:

我有一个 prism 应用程序和各种模块。我想知道哪里是查找样式、画笔、控件模板、数据模板等资源的最佳位置?

我应该制作一个单一的资源字典并将所有内容都放在那里吗?每个模块都应该有自己的资源吗?还是每个视图?我想遵循 Prism 保持一切模块化的目标,但我也没有看到在每个模块中重新声明相同资源的意义......

【问题讨论】:

    标签: c# wpf prism


    【解决方案1】:

    我想分享一些新知识。我正在使用@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.xamlApp.xaml 中声明&lt;ResourceDictionary/&gt;

    <Application.Resources>        
        <ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
    </Application.Resources>
    

    【讨论】:

      【解决方案2】:

      应用程序范围的资源我通常放在ResourceDictionary 中,添加到App.xamlStartupWindow.xaml

      特定视图的资源通常位于视图中。例如,用于 CalendarView 的 UserControl 将包含日历的任何自定义资源,例如特定于日历的画笔、样式、模板等。

      我通常看不到制作模块范围资源的理由,但如果我这样做的话,我会为模块提供ResourceDictionary,它可以在运行时加载到应用程序的合并字典中,或者包含在单个模块中的视图。

      【讨论】:

        【解决方案3】:

        我使用 Prism 开发应用程序,我使用的技术与 Prism 手册中描述的非常接近。有 YourApplication.Infrastructure 项目,您通常会在其中放置所有共享接口等。所以:

        1. 我只是添加项目 YourApplication.Resources
        2. 在那里创建文件夹 Themes
        3. 在 Themes 文件夹中为每组资源创建单独的 xaml 文件(例如 Generic.WPF.xaml 用于标准 WPF 控件的样式,Generic.Brushes.xaml 用于画笔等)
        4. 创建文件 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>
          
        5. 现在您可以在任何模块中添加这些资源(您有单独的项目,对吗?),方法是向该项目添加对 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>
          

        我不知道,也许这种方式有一些问题,但它很有效,对我来说效果很好。如果有人能以这种方式发表评论(优点/缺点) - 我会很高兴听到它!

        【讨论】:

        • 嗨,知道这有点老了,但需要评论它并询问与此相关的建议。我做了类似的事情并尝试了不同的简单或高级名称。名称和地址似乎是正确的。我们应该在 Container 中注册任何东西还是做任何初始化技术来使用这种方法。对我来说,似乎在运行时找不到资源程序集? Prism 4.1,谢谢。
        • @Sypress 实际上这种方法不使用 Prism 的任何东西,它只是类似于 Prism。它仅使用嵌入式 WPF 和 XAML 功能。它究竟如何说它找不到?它向您显示错误或只是不使用程序集中的样式(可能 - 默认样式)?
        • 找不到程序集,我通过 ms-build 传递了程序集,问题消失了:)
        • 2020年仍然有效
        猜你喜欢
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多