简体   繁体   English

为什么使用 ResourceDictionary 时不显示颜色/画笔?

[英]Why are colors/brushes not shown when using ResourceDictionary?

I have some application wide resources in my programm:我的程序中有一些应用程序范围的资源:

<Application.Resources>
    <ResourceDictionary>

        <!--Colors-->
        <Color x:Key="PaleRed" R="255" G="127" B="127" />
        <Color x:Key="PaleGreen" R="222" G="255" B="209" />
        <Color x:Key="PaleBlue" R="112" G="200" B="255" />
        <Color x:Key="LightGray" R="155" G="155" B="155" />
        <Color x:Key="DarkGray" R="100" G="100" B="100" />

    </ResourceDictionary>
</Application.Resources>

In my ViewModel I get the Background在我的 ViewModel 中,我得到了背景

public Brush Background
    {
        get
        {
            Color paleRed = (Color)Application.Current.Resources["PaleRed"];
            Color paleGreen = (Color)Application.Current.Resources["PaleGreen"];
            Color paleBlue = (Color)Application.Current.Resources["PaleBlue"];
            Color lightGray = (Color)Application.Current.Resources["LightGray"]; 

            #region comment
            paleRed = Color.FromRgb(255, 127, 127);
            paleGreen = Color.FromRgb(222, 255, 209);
            paleBlue = Color.FromRgb(112, 200, 255);
            lightGray= Color.FromRgb(112, 112, 112);
            #endregion

            if (ComponentType == ComponentType.test)
            {
                return new SolidColorBrush(paleBlue);
            }

            var color = Percentage > 0.0 ? paleGreen : paleRed;
            Brush solidBack = new SolidColorBrush(color);
            Brush gradientBack = new LinearGradientBrush(lightGray, color, 0);

            return Amount-AmountSold > 0 ? solidBack : gradientBack;
        }
    }

When setting the Background like it is in the example above (region not commented), it shows the colors in the right way.在上面的示例中设置背景时(区域未注释),它以正确的方式显示 colors。 If I comment the reassignment, the colors are not shown (only white background).如果我评论重新分配,则不会显示 colors(只有白色背景)。 But when I set a breakpoint with the region commented, the colors(and brushes) are not null and have the right ARGB values.但是当我设置一个带有注释区域的断点时,颜色(和画笔)不是 null 并且具有正确的 ARGB 值。

Can anyone tell me why this happens?谁能告诉我为什么会这样?

The way you're defining the colours, you need to also provide A.您定义颜色的方式,您还需要提供 A.

EG:例如:

    <Application.Resources>
        <ResourceDictionary>
            <!--Colors-->
            <Color x:Key="PaleRed" R="255" G="127" B="127" A="255" />
            <Color x:Key="PaleGreen" R="222" G="255" B="209" />
            <Color x:Key="PaleBlue" R="112" G="200" B="255" />
            <Color x:Key="LightGray" R="155" G="155" B="155" />
            <Color x:Key="DarkGray" R="100" G="100" B="100" />
            <SolidColorBrush x:Key="PaleRedBrush" Color="{StaticResource PaleRed}"/>
        </ResourceDictionary>
    </Application.Resources>

Set the background of my mainwindow to PaleRedBrush将我的主窗口的背景设置为 PaleRedBrush

<Grid Background="{StaticResource PaleRedBrush}">

That works这样可行

If I remove that A setting or use a different colour then it's white - the background of my window.如果我删除该 A 设置或使用不同的颜色,那么它就是白色 - 我的 window 的背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM