How to define a color using the <Color/> element of Xamarin Forms within a Resourcedictionary

Asked

Viewed 122 times

1

Hello, I’m following you this tutorial and, starting from what it shows along with the Xamarin.Forms documentation, I can create a Resource Dictionary as follows:

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="App.Assets.Colors">

    <Color x:Key="TestBlue">Blue</Color>
    <Color x:Key="TestRed">Red</Color>
    <Color x:Key="TestGreen">Green</Color>

</ResourceDictionary>

And with the ResourceDictionary created, I can call you in any component, including in the global application Resources so:

<?xml version="1.0" encoding="utf-8" ?>
<Application 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="App.App">

    <Application.Resources>
        <ResourceDictionary Source="/Assets/Colors.xaml" />
    </Application.Resources>

</Application>

The problem is that when I compile the application to be able to use this resource somewhere in the code, it brings me an error described:

The type 'Color' does not support direct content.

Can someone help me? I have done this in Windows Presentation Forms and I would like to be able to make more flexible the control of variables and extend styles in a simple and dynamic way but it has a good time that I do not research and I do not think how to do...

  • Can provide an example of where you are trying to use the color?

  • The problem is not to use the color, is to be able to declare it and import it in Xamarin. I made the same code as in Wpf and it still didn’t work

  • Tried to activate the shampoo build? Put <?xaml-comp compile="true" ?> before opening the tag of ResourceDictionary in your shampoo?

  • No, I didn’t try... To tell you the truth I didn’t even know it existed ... I’ll take a look tomorrow and confirm here if it happened

  • @Leandroluk, your code should work... there are some differences between the XAML of wpf and Xamarin.Forms, but this is not one of them... Managed to make work by enabling the compilation of XAML?

1 answer

-1

Resourcedictionary is for you to create shared resources like you said, but in this case you are trying to inform an xml file to specify colors. The correct use would be as below.

Put the code below in App.xaml

<Application.Resources>
   <ResourceDictionary MergedWith="light:LightThemeResources">
      <!-- Cores compartilhadas -->
      <Color x:Key="BackgroundColor">#F4F6FA</Color>
      <Color x:Key="TextColor">#722881</Color>

      <!-- FONT SIZE -->

      <OnPlatform
          x:Key="MidMediumSize"
          x:TypeArguments="x:Double"
          iOS="12"
          Android="14"
          WinPhone="14"/>
   </ResourceDictionary>
</Application.Resources>

On your xaml pages you can use the Resources as follows:

<Label Text="{Binding Name}" FontSize="{StaticResource MidMediumSize}" FontAttributes = "Bold" TextColor = "{StaticResource TextColor}" />
  • The question is exactly about the tag <Color /> and this answer does not solve the problem in Xamarin Forms because the way it is implemented is different from WPF (which is the way I put it in the example). Regarding Resourcedictionary, I intend to use it as the example above, separating each importance in a file and importing them into App.xaml.

  • But this solution is not for WPF. I removed this solution from a Xamarin.Forms project I work on. You are trying to put a shaman file as a Resource Dictionary, so it is giving error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.