Setting Color in XAML from hex string declared in code

Hi!) Sorry perhaps for a nub question but

How can be Color set in App.xaml Resources with a string that is declared in code like :

       <Color x:FactoryMethod="FromHex" x:Key="TitleColor">
        <x:Arguments>
          <x:String>#97cd75</x:String>
        </x:Arguments>
      </Color>

And

      public const string TitleColor = "#97cd75";

So that something like

       <Color>{x:Static MyColors.TitleColor}</Color>

can be acheived

Thanx! in advance;)

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    I think you might want to take a look at the documentation here!

  • Thanks but as you could guess I did) I have dynamic styles and can do some hacks in code in Resources dictionary after InitialiseComponent but that's not an option, at least for now :D

    Also btw - why? do I get this:(

    'Xamarin.Forms.Color' is declared in another module and needs to be imported

    when my namespace imports do look like this

             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:forms="clr-namespace:Xamarin.Forms"
    
  • VitaliyHnatyk.5863VitaliyHnatyk.5863 UAMember
    edited April 2016

    Umm perhaps I should ask - why

    <Color><x:Static Member="MyColors.TitleColor"/></Color> 
    

    or

    <Color><xString><x:Static Member="MyColors.TitleColor"/></x:String></Color>
    

    compiles and doesn't throw any exceptions, nor it does what I suppose it should do - initializing the color with Transparent instead.

  • JerzyPiechowiakJerzyPiechowiak USMember ✭✭

    @VitaliyHnatyk.5863
    Do you have working solution for this? I've occured the same error today, when I turned on xaml compilation.

Sign In or Register to comment.