Color resource in XAML?

TMainTMain USMember ✭✭
edited May 2014 in Xamarin.Forms

Is there anyway to use a <Color/> resource in XAML the way you would use it in other versions in XAML as a StaticResource?

For example:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestUI.Xaml;assembly=TestUI.Xaml" x:Class="TestUI.MainView" > <ContentPage.Resources> <ResourceDictionary> <Color x:Key="AlmostSilver">#FFCCCCCC</Color> </ResourceDictionary> </ContentPage.Resources> <Grid x:Name="Container"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Grid.Row="0" Text="Works just fine" VerticalOptions="Center" TextColor="#FFCCCCCC"/> <Label Grid.Row="1" Text="Doesn't work" VerticalOptions="Center" TextColor="{StaticResource AlmostSilver}"/> </Grid> </ContentPage>

The second label just silently fails, no error message kicked. I've tried lower case, binding, and any other crazy thing I can think of - it just doesn't work. Is there anyway to actually use a <Color/> resource defined in XAML?

Posts

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    Not seeing the problem immediately, let me load this into a test

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai
    edited May 2014

    I can confirm at this point that the color is failing to be parsed into the resource dictionary correctly. I have assigned a bug to one of my developers and it should be fixed soon :)

  • TMainTMain USMember ✭✭

    Great, thanks Jason for the quick confirmation and follow up! BTW, how would we know when a fix (well, this particular fix) has happened?

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    Release notes :) The next release on nuget.org will come with notes. I will likely also make a release thread here. We dont do public bug tracking in Xamarin.Forms because we really just dont have the manpower to keep up with users there too (takes a lot more triage). I am really just dedicating my work days for the first couple days after launch to being hyper active on the forums, then I need to go fix these bugs for you guys!

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @TMain‌: As Xamarin.Forms supports XAML 2009 syntax, you can (by changing your xmlns:x declaration) use {x:Static ...} markup extension.

    syntax for it is: {x:Static [Member=][prefix:]typeName.staticMemberName}

  • TMainTMain USMember ✭✭

    I'm not sure what you're referring to here. I believe you're recommending that I declare colors in code-behind in a static class, right?

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    another option is (still using xaml 2009)

    <Color x:FactoryMethod="FromHex" x:Key="AlmostSilver">
        <x:Arguments>
            <x:String>#ffcccccc</x:String>
        </x:Arguments>
    </Color>
    
  • VitalyChuprykVitalyChupryk USMember

    Is it possible to store resource dictionaries in separate XAML files? I'd like to share them between different pages.

  • TMainTMain USMember ✭✭

    @VitalyChupryk‌ agreed, that would be pretty fantastic.

    @StephaneDelcroix‌ still can't implement what you've recommended, using either x:Static or StaticResource. Care to provide a working example of utilization of the Color resource structure you're recommending assigned to an actual TextColor or BackgroundColor property?

  • VitalyChuprykVitalyChupryk USMember

    @TMain, XAML 2009 way worked for me.

    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:FactoryMethod="FromHex" x:Key="TitleColor">
                <x:Arguments><x:String>#97cd75</x:String></x:Arguments>
            </Color>
        </ResourceDictionary>
    </ContentPage.Resources>
    
    <Label Text="Log In" Font="18" TextColor="{StaticResource TitleColor}" />
    
  • VitalyChuprykVitalyChupryk USMember

    @StephaneDelcroix‌. Didn't know about x:Static and didn't understand this fro your previous message. Thanks for the sample.

  • TMainTMain USMember ✭✭
    edited May 2014

    @StephaneDelcroix‌ this is working perfectly now with 2009 (the sample I was working from was 2006), so now with the color resource on page as you described, I can use StaticResource. Perfect, thank you!

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    Just to remember , is also possible using a converter:

    public class HexColorConverter : IValueConverter
    {
    
        #region IValueConverter implementation
    
        public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return  (value == null) ? value : (string.IsNullOrEmpty (value.ToString ()) ?
                value : Color.FromHex (value.ToString ()));
        }
    
        public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    
        #endregion
    }
    
    
    <ContentPage.Resources>
    <ResourceDictionary><local:HexColorConverter x:Key="coloronverter" /></ResourceDictionary>
    </ContentPage.Resources>
    <Grid Padding="20,20,20,20" HeightRequest="42"  Grid.Column="0"  WidthRequest="42" BackgroundColor="{Binding > Color, Converter={StaticResource colorConverter}}"><Grid>
    
  • VitalyChuprykVitalyChupryk USMember
    edited May 2014

    @rmarinho‌ There is already color converter - Xamarin.Forms.ColorTypeConverter.

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    Nice! :)

  • JeffLimJeffLim USMember ✭✭
    edited October 2018

    @StephaneDelcroix‌
    Thanks for the sample. That help me a lots.
    Btw,. How can I use the same constant in styles.xml in Android project?

    Example: I want to replace the #02b498 with the constant?

    <?xml version="1.0" encoding="UTF-8"?> <resources> <style name="MyTheme" parent="MyTheme.Base"> </style> <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="colorPrimary">#02b498</item>

  • NMackayNMackay GBInsider, University mod

    You can't use the same resource dictionary in your native Android project, you can however get at the resource dictionary values in renderers etc.

    Btw, in Xamarin 3.2 merged dictionaries work fine (3.0 officially I think but works properly in 3.2). Don't open up threads from 4 years ago if you can help it, Forms was probably about 1.2 at that stage. Best to start up a new thread.

Sign In or Register to comment.