问题
EDIT: Looks like this was related to the way I declared the color - using hex value seems to bring it through fine.
EDIT: This about referencing from the App.xaml file. The color itself works fine if declared as a local resource.
I've created a colour:
<SolidColorBrush x:Key="TestBlue">
<SolidColorBrush.Color>
<Color R="0" G="86" B="45"/>
</SolidColorBrush.Color>
</SolidColorBrush>
and placed it in a ResourceDictionary. I've then referenced it in a MergedDictionary:
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colours.xaml"/>
<ResourceDictionary Source="View\Item\ItemResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
When I apply it to the background of a Button as a StaticResource it doesn't apply:
Background="{StaticResource TestBlue}"
When I Snoop the control it declares the Background as some other value:
It also doesn't work if I make it an entry in the itself (like the ViewModelLocator). It does work if I place it in the Window.Resources. Any ideas what's going on here?
回答1:
You need to provide value for Alpha (transparency) channel, because Color is structure and it's default value for that value is zero (transparent).
<Color A="255" R="0" G="86" B="45"/>
回答2:
To use it as a StaticResource, you must include your ResourceDictionnary where you want to use your Resource. Otherwise, you'll have to use it as a DynamicResource.
So if your button is in its own xaml file, you'll have to include your ResourceDictionnary in the button's xaml file, as you did in App.xaml.
来源:https://stackoverflow.com/questions/30224232/defining-a-color-in-app-xaml-and-using-as-a-static-resource