我試圖將我的Silverlight控件的顏色設置爲由我的應用程序使用的ResourceDictionary中定義的顏色的稍微透明的版本。如何在XAML中通過顏色組件定義Silverlight顏色?
要做到這一點,我的策略爲打破顏色爲組件,這樣我可以抓住的RGB值,然後設置最重要的是我自己的Alpha值讓我的半透明色。
的資源字典看起來像:
<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顯然不支持本地的字節型:在ResourceDictionary中定義這樣我的膚色部分不載入,拋出一個「類型‘字節’未找到」錯誤。
那麼,除非不使用字節,我該如何實現將A,R,G,B顏色值分解爲字節的等價操作? (使用字符串&類型轉換?)或者也許是定義顏色,繼承/使用它,然後重寫其alpha值的更好方法?我需要通過XAML完全實現這一目標。
任何想法?
你可以只使用純色和透明度是必要的元素設置不透明度? – 2011-03-09 19:03:19
不幸的是,沒有。並非我可能需要半透明顏色的每個元素都具有不透明度,並且Alpha存儲在顏色級別。看到我上面的LinearColorKeyFrame示例:它的Color值爲alpha,但在Color或LinearColorKeyFrame級別沒有Opacity屬性。 – Dave 2011-03-09 19:11:47