Xamarin.forms: [Visual] [iOS] [Material] Button.BackgroundColor not working in C#

Created on 29 Mar 2019  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

If i have a Button on the screen with Visual="Material" when i change BackgroundColor property of the button in code the change does nothing on iOS, this works if the button is Visual="Default", and works fine on Android for both.

Steps to Reproduce

  1. Open a xamarin project with iOS
  2. place a button on the screen with Visual="Material"
  3. change button's background colour (toggle between 2 colours) in code using button click event

Expected Behavior

The background colour should toggle/change every click

Actual Behavior

Nothing happens

Basic Information

  • Version with issue: v3.6.0.264807
  • Last known good version: N/A
  • IDE: Visual Studio 2017: 15.9.10
  • Platform Target Frameworks:

    • iOS: 12.2

  • Affected Devices:
    Simulator and physical

Reproduced in TheLittleThingsPlayground by simply adding this to ButtonsPage.xaml.cs Constructor:

``` C#
Task.Run( async () =>
{
while( true )
{
if( testColorChange.BackgroundColor == Color.Black )
{
testColorChange.BackgroundColor = Color.Green;
}
else
{
testColorChange.BackgroundColor = Color.Black;
}

await Task.Delay( 1000 );

}
} );
```

and changing any button to have the x:Name="testColorChange"

visual 3 in-progress iOS 馃崕 bug

Most helpful comment

Replicated.

System Information
Visual Studio: 16.1.1
Xamarin.iOS and Xamarin.Mac SDK version: 12.10.0.153(750a879)
Xamarin.Forms version: 4.0.0.425677
Xamarin.Forms.Visual.Material version: 4.0.0.425677


Implementing a custom renderer solves this problem.

internal class CustomMaterialButtonRenderer : MaterialButtonRenderer
{
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == nameof(Element.BackgroundColor))
        {
            Control.BackgroundColor = Element.BackgroundColor.ToUIColor();
        }
    }
}

All 7 comments

has there been any update on this issue?

Replicated.

System Information
Visual Studio: 16.1.1
Xamarin.iOS and Xamarin.Mac SDK version: 12.10.0.153(750a879)
Xamarin.Forms version: 4.0.0.425677
Xamarin.Forms.Visual.Material version: 4.0.0.425677


Implementing a custom renderer solves this problem.

internal class CustomMaterialButtonRenderer : MaterialButtonRenderer
{
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == nameof(Element.BackgroundColor))
        {
            Control.BackgroundColor = Element.BackgroundColor.ToUIColor();
        }
    }
}

Happening in 4.2-nightly. I wonder if it's the material SemanticColorScheme check...wild guess. https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Material.iOS/MaterialButtonRenderer.cs#L149

for those wondering how to export the workaround custom renderer (@darshanio ) to override material renderer only:

[assembly: ExportRenderer(typeof(Button), typeof(CustomRenderers.iOS.CustomMaterialButtonRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]

I don't like the idea of requiring to use a custom renderer for this, Is there an eta on official fix from Xamarin.Forms side?

HI all!
Any news on this?

Thank you!

@PureWeen, it has been a few months has there been any development on this issue?

Was this page helpful?
0 / 5 - 0 ratings