创建自己的系统颜色 [英] Create your own system colors

查看:183
本文介绍了创建自己的系统颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我如何在静态类中创建自己的颜色集合,以便我可以这样做:



什么存在

 < Setter ... Value ={DynamicResource {x:Static SystemColors.ControlBrushKey}} /> 

我想要什么

 < Setter ... Value ={DynamicResource {x:Static MyColors.Color1}}/> 


解决方案

资源键可以是任何东西, Color 作为键和值的同时:

  public static class MyColors 
{
static MyColors()
{
App.Current.Resources.Add(MyHighlightColorKey,MyHighlightColorKey);
}

public static readonly Color MyHighlightColorKey = Color.FromArgb(255,0,88,0);
}

静态构造函数使用自身作为应用程序资源的键来添加颜色。



SystemColors 在内部使用 SystemResourceKeys 每一个定义的颜色或画笔,你没有访问那个类(这是有道理的),或者你可以子类 ResourceKey 如果你把这个值作为自己的键



您可以这样使用:

 < TextBox> 
< TextBox.Background>
< SolidColorBrush Color ={DynamicResource {x:Static local:MyColors.MyHighlightColorKey}}/>
< /TextBox.Background>
< / TextBox>

如果你需要覆盖本地关键字,你也可以这样做: p>

 < Window.Resources> 
< Color x:Key ={x:Static local:MyColors.MyHighlightColorKey}A =255R =255G =0B =0
< /Window.Resources>






编辑:你有很多颜色,画笔,你也可以使用反射做资源注册的构造函数(我使用的字段,如果你使用属性来公开你需要稍微调整这些数据):

  static MyColors()
{
FieldInfo [] keyFieldInfoArray = typeof(MyColors).GetFields();
foreach(keyFieldInfoArray中的var keyFieldInfo)
{
对象值= keyFieldInfo.GetValue(null);
App.Current.Resources.Add(value,value);
}
}


Basically, how can I create my own set of Colors in a static class or the such so that I can do something like this:

What exists:

<Setter ... Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

What I want:

<Setter ... Value="{DynamicResource {x:Static MyColors.Color1}}"/>

解决方案

Resource Keys can be anything, so you can use a Color as a key and value at the same time:

public static class MyColors
{
    static MyColors()
    {
        App.Current.Resources.Add(MyHighlightColorKey, MyHighlightColorKey);
    }

    public static readonly Color MyHighlightColorKey = Color.FromArgb(255, 0, 88, 0);
}

The static constructor adds the colour using itself as a key to the application resources.

(SystemColors uses SystemResourceKeys internally for every defined colour or brush, you have no access to that class however (which makes sense), alternatively you could subclass ResourceKey if you take issue with using the value as its own key)

You can use it like this:

<TextBox>
    <TextBox.Background>
        <SolidColorBrush Color="{DynamicResource {x:Static local:MyColors.MyHighlightColorKey}}"/>
    </TextBox.Background>
</TextBox>

And if you need to override the key on a local level you can do so as well:

<Window.Resources>
    <Color x:Key="{x:Static local:MyColors.MyHighlightColorKey}" A="255" R="255" G="0" B="0"/>
</Window.Resources>


Edit: If you have lots of colours, brushes and whatnot you could also use reflection to do the resource registering in the constructor (i used fields, if you use properties to expose the data you need to adjust this slightly):

static MyColors()
{
    FieldInfo[] keyFieldInfoArray = typeof(MyColors).GetFields();
    foreach (var keyFieldInfo in keyFieldInfoArray)
    {
        object value = keyFieldInfo.GetValue(null);
        App.Current.Resources.Add(value, value);
    }
}

这篇关于创建自己的系统颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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