xamarinxamarin.formsstylesstaticresource

Static resource colors are not working when setting on button Xamarin forms


I am setting text and background color of button using static resource but colors are not showing at all.

I have added Theme.cs file on project level in shared project.t

public static class Theme
{
    public static Color Application_GreenColor = Color.FromHex("5EC451");
    public static Color Application_RedColor = Color.FromHex("#EC3323");
}

Below this xaml file, setting colors on button

<?xml version="1.0" encoding="utf-8" ?>
<Grid
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:local="clr-namespace:projectName;assembly=projectName"
    xmlns:extensions="clr-namespace:projectName.Extensions.MarkupExtensions;assembly=projectName"
    x:Class="path/projectName">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>
        <Grid
            Grid.Row="1"
            Margin="20,20,20,20">
            <Grid.ColumnDefinitions>
                <ColumnDefinition
                    Width="*" />
                <ColumnDefinition
                    Width="*" />
            </Grid.ColumnDefinitions>
           <Button
            Margin="0,0,0,0"
            Command="{Binding ConfirmCardCommand}"
            Text="CardOperationsView"
            FontSize="Medium"
            CornerRadius="30"
            ContentLayout="Right"
            TextColor="{x:Static local:Theme.Application_PrimaryButtonTextColor}"
            Grid.Column="1"
            HorizontalOptions="FillAndExpand"
            VerticalOptions="Center"
            BackgroundColor="{x:Static local:Theme.Application_RedColor}" />
        </Grid>
    </Grid>
</Grid>

Above UI is for popup. This is how popup is getting opened

private async Task ShowCardDialogAsync()
{
    _CardPopup = await DialogHelper.ShowCustomBottomPopupAsync(new CardPopupTemplate(), this);
}

From xaml Button is not any background and text color. How can I fix this ?


Solution

  • This is how this issue has fixed. Declare colors in App.xaml file

      <Color x:Key="LblgbColor">#AFABAB</Color>
      <Color x:Key="AccentColor">#BC4C1B</Color>
      <Color x:Key="BackgroundColor">#212C37</Color>
      <Color x:Key="BlackColor">#000000</Color>
      <Color x:Key="BlackTextColor">#253748</Color>
      <Color x:Key="BlueColor">#0072C6</Color>
    

    Use it like this xaml files

    <BoxView Grid.ColumnSpan="3"
    Grid.Row="0"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="StartAndExpand"
    HeightRequest="1"
    BackgroundColor="{StaticResource BackgroundColor}">
    </BoxView>