【发布时间】:2011-03-09 18:45:33
【问题描述】:
我正在尝试将 Silverlight 控件的颜色设置为我的应用程序使用的 ResourceDictionary 中定义的颜色的略微透明版本。
为此,我的策略 将颜色分解为组件,这样我就可以获取 RGB 值,然后在其上设置我自己的 alpha 值得到我的半透明颜色。
ResourceDictionary 类似于:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:Byte x:Key="PrimaryLightColorAlphaValue">#FF</system:Byte>
<system:Byte x:Key="PrimaryLightColorRedValue">#DB</system:Byte>
<system:Byte x:Key="PrimaryLightColorGreenValue">#E5</system:Byte>
<system:Byte x:Key="PrimaryLightColorBlueValue">#F1</system:Byte>
<Color x:Name="PrimaryLightColor" A="{StaticResource PrimaryLightColorAlphaValue}"
R="{StaticResource PrimaryLightColorRedValue}"
G="{StaticResource PrimaryLightColorGreenValue}"
B="{StaticResource PrimaryLightColorBlueValue}" />
<SolidColorBrush x:Name="PrimaryLightColorBrush" Color="{StaticResource PrimaryLightColor}" />
....
然后我的颜色将通过引用颜色或其组件在我的应用程序中使用。
....
<Border Background="{StaticResource PrimaryLightColorBrush}" />
....
<LinearColorKeyFrame KeyTime="00:00:00">
<LinearColorKeyFrame.Value>
<Color A="#CC"
R="{StaticResource PrimaryLightColorBrushRedValue}"
G="{StaticResource PrimaryLightColorBrushGreenValue}"
B="{StaticResource PrimaryLightColorBrushBlueValue}" />
</LinearColorKeyFrame.Value>
</LinearColorKeyFrame>
....
我的问题:
Silverlight XAML 显然本身不支持 Byte 类型:因此我在 ResourceDictionary 中定义的颜色部分永远不会加载并引发“未找到类型 'Byte'”错误。
那么,除了不使用字节之外,我怎样才能实现将这些 A、R、G、B 颜色值分解为字节的等价物? (使用字符串和类型转换?)或者也许有更好的方法来定义颜色,继承/使用它,然后覆盖它的 alpha 值?问题是我需要专门通过 XAML 来实现这一点。
有什么想法吗?
【问题讨论】:
-
您可以只使用纯色并在需要透明的元素上设置不透明度吗?
-
不幸的是,没有。并非我可能需要半透明颜色的每个元素都具有不透明度,并且 alpha 存储在颜色级别。请参阅上面的 LinearColorKeyFrame 示例:它的 Color 值具有 alpha,但在 Color 或 LinearColorKeyFrame 级别没有 Opacity 属性。
标签: wpf silverlight xaml colors