Issue
I am building a Xamarin Forms Application and I am currently drawing up my application Resources, mainly my colours.
For example I have the following:
<Color x:Key="Slate">#404040</Color>
<Color x:Key="Blue">#458623</Color>
<Color x:Key="SelectedItemColour">#458623</Color>
As you can see my SelectedItemColour is the same as the Blue.
I have tried the following but it didn't work:
<Color x:Key="Slate">#404040</Color>
<Color x:Key="Blue">#458623</Color>
<Color x:Key="SelectedItemColour" Color="{StaticResource Blue}"/>
I know if WPF you can do the answer stated here
Is it possible to point a Colour Resource to another Colour Resource in Xamarin.Forms?
Solution
You can use x:Static in tandem with a static class in order to directly reference those colors by name. This has the benefits of keeping the colors centralized into one class and minimizing the amount of XAML.
namespace ResourceColors
{
public static class Colors
{
public static Color Slate = Color.FromHex("#404040");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ResourceColors;assembly=ResourceColors" x:Class="ResourceColors.PageOne">
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="Blue">#458623</Color>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="Test" TextColor="{x:Static local:Colors.Slate}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Answered By - Paul
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.