xamarin/Xamarin.Forms

Effect that changes Label's background does not work when the FastRenderers flag is set

Open

#5,336 opened on Feb 21, 2019

 (4 comments) (0 reactions) (0 assignees)C# (1,926 forks)batch import
4.0.0a/fastrendererse/3 :clock3:help wantedinactivep/Androidt/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Description

I've created an effect to show a border around many controls, everything seems good until I tested it while the FastRenderers flag is enabled, it worked for all my tests except for the Labels.

I'm creating the border by setting the TextView's background, I wasn't able to figure out the exact problem because the FastRenderers.LabelRenderer class is internal sealed and it's using the BackgroundManager which is also internal.

Steps to Reproduce

To reproduce the issue, I've modified the ShadowEffect sample project, I've just enabled the FastRenderers flag and modified the OnAttached override to set the Background instead of the ShadowLayer:

protected override void OnAttached()
{
    try
    {
        var control = Control as Android.Widget.TextView;
        var effect = (ShadowEffect)Element.Effects.FirstOrDefault(e => e is ShadowEffect);
        if (effect != null)
        {
            Android.Graphics.Color color = effect.Color.ToAndroid();

            if (!(control.Background is Android.Graphics.Drawables.GradientDrawable gradientBackground))
            {
                gradientBackground = new Android.Graphics.Drawables.GradientDrawable();
                control.SetBackground(gradientBackground);
            }

            gradientBackground.SetStroke(1, color);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Cannot set property on attached control. Error: ", ex.Message);
    }
}

Expected Behavior

Actual Behavior

Basic Information

  • Android Target Framework: 8.1 (API 27)
  • Latest Xamarin.Forms (3.5.0.129452)

Reproduction Link

Issue.zip

Contributor guide