Why can't I set Button BackgroundColor with Style?

EasyGoingPat 261 Reputation points
2021-09-01T07:34:36.453+00:00

I have an odd problem that has just developed, seemingly for no reason (i.e. I have not updated anything).

This works as expected:

Content = new Grid()
{
 BackgroundColor = Color.Transparent,
 RowDefinitions = Rows.Define( GridLength.Auto ),
 ColumnDefinitions = Columns.Define( 55, GridLength.Star, 55 ),
 Children =
 {
 new Button { Text = "Press Me", BackgroundColor = Color.Yellow }.Row( 0 ).Column( 1 )
 }
};

This doesn't work. It did work but now has stopped since I last visited this code some time ago. As far as I can tell the button just gets the Xamarin default colour (light-blue)

Style ButtonStyle = new Style( typeof(Button) )
{
 Setters =
 {
 new Setter { Property = Button.BackgroundColorProperty, Value = Color.Yellow }
 }
}

Content = new Grid()
{
 BackgroundColor = Color.Transparent,
 RowDefinitions = Rows.Define( GridLength.Auto ),
 ColumnDefinitions = Columns.Define( 55, GridLength.Star, 55 ),
 Children =
 {
 new Button { Text = "Press Me", Style = ButtonStyle, }.Row( 0 ).Column( 1 )
 }
};

This code is so simple that I am struggling to think where I can look for an answer. I have never encountered a problem anything like this with Xamarin.

Does anyone have any idea what may be going on?

Kind wishes - Patrick

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,313 questions
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-09-02T02:26:24.2+00:00

    Hello PatrickRyder,​

    Welcome to our Microsoft Q&A platform!

    There are some errors when testing the above code. The 'RowDefinitions = Rows.Define' line code can not be complied correctly, do you use some other package. I created a basic demo to test the function, which works fine. Here is the sample code, you could refer to:

       public partial class MainPage : ContentPage  
       {  
           public MainPage()  
           {  
               var ButtonStyle = new Style(typeof(Button))  
               {  
                   Setters =  
                   {  
                       new Setter { Property = Button.BackgroundColorProperty, Value = Color.Yellow }  
                   }  
               };  
         
               var grid = new Grid()  
               {  
                   BackgroundColor = Color.Transparent,  
                   RowDefinitions =  
                   {  
                       new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) }  
                   },  
                   ColumnDefinitions =  
                   {  
                       new ColumnDefinition()  
                   }  
               };  
         
               var button = new Button   
               {   
                   Text = "Press Me",   
                   Style = ButtonStyle   
               };  
         
               grid.Children.Add(button);  
               Grid.SetRow(button, 0);  
               Grid.SetColumn(button, 0);  
         
               Content = grid;  
         
               button.Clicked += Button_Clicked;  
           }  
       }  
    

    The style is consumed successfully on the button.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful