在复杂的WPF场景中如何组织资源(样式,...)? [英] How to organize resources (styles, ...) in a complex WPF scenario?

查看:70
本文介绍了在复杂的WPF场景中如何组织资源(样式,...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何组织WPF资源(包括样式,模板等),以便我可以在Windows,Pages甚至Projects中使用它们。为了获得资源的最大可重用性和可维护的结构(例如,每个模板一个文件),我必须采取哪些选择?

How can WPF resources - including styles, templates, etc. - be organized, so that I can use them across Windows, Pages or even Projects. What options do I have to achieve maximum re-usability of my resources and a maintainable structure (for example one file per Template)?

例如:我正在创建一个WPF应用程序,并且我想使用TabControl,但是我想对其进行重大更改。因此,我可以在其中创建样​​式并将其应用于TabControl和TabItem。没关系,但是我可以在哪里放置资源以保持Window XAML的清晰,并可以从其他Windows或项目访问样式?

For example: I am creating a WPF application and I want to use a TabControl, but I want to make major changes to it. So I could create a style in and apply it to the TabControl and TabItem. That's ok, but where can I place my resources to keep my Window XAML clear and have the style accessible from other Windows or projects as well?

我发现我可以添加它到App.xaml,但这仅是一个项目的解决方案,并且允许在该项目的各个项目之间共享。另外,我认为最好将这些模板与其他代码稍微分开,而不是将其全部放在某个页面或app.xaml中。

I found that I can add it to App.xaml but that is only a solution for one project and allows sharing just between items of this project. Also, I think it would be better to have these templates a little separate from other code, than placing it all in some page or app.xaml?

推荐答案

我通常创建一个单独的样式项目,该项目从我要样式的项目中引用。样式项目具有如下固定结构:

I usually create a seperate styling project, which I reference from the projects, which I want to style. The styling project has a fixed structure like this:

对于每个控件,我都创建一个样式 ResourceDictionary 。例如,对于我的按钮:

For every control, I create a styling ResourceDictionary. For example for my buttons:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="PrimaryButtonStyle" TargetType="Button">
    </Style>

    <Style x:Key="ToolbarButton" TargetType="Button">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Margin" Value="3"/>
        <Setter Property="Background" Value="Transparent"></Setter>
    </Style>
</ResourceDictionary>

在一个主要的 ResourceDictionary 中,我合并了所有其他字典,在本例中为IncaDesign.xaml文件,您可以在上图中看到:

In one main ResourceDictionary, I merge all the other dictionaries, in this case in the file IncaDesign.xaml, which you can see in the picture above:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:Commons.Controls;assembly=Commons">

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="Converter/Converter.xaml" />
        <ResourceDictionary Source="Styles/Button.xaml" />
        <ResourceDictionary Source="BitmapGraphics/Icons.xaml" />

    </ResourceDictionary.MergedDictionaries>

    <!-- Default Styles -->
    <Style TargetType="Button" BasedOn="{StaticResource PrimaryButtonStyle}"></Style>
</ResourceDictionary>

请注意我是如何定义默认样式的,除非您另外指定,否则它们将自动应用。在要设置样式的每个窗口或控件中,只需引用一个 ResourceDictionary 。请注意源的定义,它是对程序集的引用( /Commons.Styling;component ...

Notice how I defined the default styles, which are applied automatically, unless you specify otherwise. In every window or control, that you want to style, you only need to reference this one ResourceDictionary. Note the definition of the source, which is a reference to the assembly (/Commons.Styling;component...)

<UserControl.Resources>        
    <ResourceDictionary>            
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Commons.Styling;component/IncaDesign.xaml" />
        </ResourceDictionary.MergedDictionaries>   
    </ResourceDictionary>        
</UserControl.Resources>

现在将自动设置默认样式,如果要显式访问资源,则可以执行使用 StaticResource

Default styles will be set automatically now, and if you want to access a resource explicitly, you can do this, using StaticResource.

<Viewbox Height="16" Width="16" Margin="0,0,10,0">
    <ContentControl Content="{StaticResource FileIcon32}" />
</Viewbox>

我认为这是一个非常好的解决方案,适用于非常复杂的解决方案,包括模块化解决方案,使用PRISM构建的示例。

This is very nice solution in my opinion, which works for very complex solutions, including modular solutions, for example built with PRISM.

这篇关于在复杂的WPF场景中如何组织资源(样式,...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆