locked
Error when trying to use global colors RRS feed

  • Question

  • User385055 posted

    Hi,

    I have the following code in my "App.xaml"

    <Application.Resources>
        <ResourceDictionary>
          <Color x:Key="BackgroundBlue">#334E9D</Color>
          <Color x:Key="ButtonOrange">#F1881E</Color>
            <Setter Property="BackgroundColor" Value="ButtonOrange" />
            <Setter Property="CornerRadius" Value="50px" />
            <Setter Property="BorderColor" Value="ButtonOrange" />
            <Setter Property="BorderWidth" Value="2px" />
            <Setter Property="FontAttributes" Value="Bold" />
            <Setter Property="TextColor" Value="White" />
          </Style>
          <Style TargetType="ContentPage" ApplyToDerivedTypes="True">
            <Setter Property="BackgroundColor" Value="BackgroundBlue" />
          </Style>
        </ResourceDictionary>
      </Application.Resources>
    

    But I am getting the following error

    Cannot convert "ButtonOrange" into Xamarin.Forms.Color

    Any advice on how to fix this would be great.

    Thanks in advance!

    Wednesday, May 20, 2020 10:51 AM

All replies

  • User41045 posted

    @rpdben It seems like you are missing the <Style> tag.

    Now coming to the issue, you need to access the Resources using the StaticResource keyword. <Setter Property="BackgroundColor" Value="{StaticResource ButtonOrange}" />

    Hope it helps.

    .

    .

    .

    .

    .



    Looking for Free* Microsoft Support on Xamarin Issues, visit https://aka.ms/xamarinsupport


    Wednesday, May 20, 2020 11:34 AM
  • User385055 posted

    Sorry, I had forgotten to paste the Style tag!

    I did this but when I build the solution {StaticResource ButtonOrange} is automatically replaced with ButtonOrange and I get the same error

    Wednesday, May 20, 2020 11:49 AM
  • User41045 posted

    You also need to correct the CornerRadius and BorderWidth with just number and no px

    Can you test the attached sample?

    This is the output I got

    .

    .

    .

    .

    .



    Looking for Free* Microsoft Support on Xamarin Issues, visit https://aka.ms/xamarinsupport


    Wednesday, May 20, 2020 12:03 PM
  • User385055 posted

    Thanks, I have changed these.

    Your project ran fine.

    It seems that there is something in my build path that when I click "build" or "deploy" it deletes the StaticResource bit and reformats my file. Do you know where I could find this setting to try and turn it off?

    EDIT: This does not happen when I compile your project, it only happens when I compile mine

    Wednesday, May 20, 2020 12:25 PM
  • User41045 posted

    @rpdben Do you think it's possible to share your project in here?

    If not, you can always open a ticket with us at https://aka.ms/xamarinsupport You can ask for it to be assigned it to me and I will personally look into it.

    Wednesday, May 20, 2020 1:36 PM
  • User385055 posted

    @AnubhavRanjan, unfortunately, I can't share this with you as it contains confidential information. Is there another way we can approach this?

    Wednesday, May 20, 2020 2:17 PM
  • User41045 posted

    @rpdben Do you think you can take the .csproj file from my project and compare against yours? If what you are saying is removing the ApplicationResources, although it shouldn't, we will get to know.

    Wednesday, May 20, 2020 2:26 PM