Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] Xamarin.Forms issue with control colors on appearing #14740

Open
ShadowOfPhantom opened this issue Oct 16, 2021 · 9 comments
Open

[Bug] Xamarin.Forms issue with control colors on appearing #14740

ShadowOfPhantom opened this issue Oct 16, 2021 · 9 comments
Assignees
Labels
5.0.0 Regression on 5.0.0 p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. t/bug 🐛

Comments

@ShadowOfPhantom
Copy link

ShadowOfPhantom commented Oct 16, 2021

Some elements are changing its colors on appearing with Xamarin.Forms 5.0.0.2125. It works well with previous version 5.0.0.2083. Looks like something is broken, please check this out

  • Version with issue: 5.0.0.2125

  • Last known good version: 5.0.0.2083

  • Platform Target Frameworks:

  • Android: 11.0 (R )

Videos are attached below

Workaround

Downgrade to 5.0.0.2083

IMG_1160.MOV
IMG_1165.MOV
IMG_1166.MOV

@jsuarezruiz

Could you attach a small sample where reproduce the issue?. Or, could you share more info?.
The views where the issue happens, are Frames?, are you using Shell or a FlyoutPage?

Sample:
ControlColorsIssue.zip

I was unable to reproduce this issue with Shell, looks like Shell is a workaround. But I have an app without FlyoutPage or shell and there is the same issue.
This happens for both BoxView and frame, I attached a video sample of attached project, please take a look

IMG_1198.MOV
@ShadowOfPhantom ShadowOfPhantom added s/unverified New report that has yet to be verified t/bug 🐛 labels Oct 16, 2021
@jsuarezruiz
Copy link
Contributor

@ShadowOfPhantom Could you attach a small sample where reproduce the issue?. Or, could you share more info?.
The views where the issue happens, are Frames?, are you using Shell or a FlyoutPage?

@jsuarezruiz jsuarezruiz added 5.0.0 Regression on 5.0.0 p/Android labels Oct 18, 2021
@jsuarezruiz jsuarezruiz added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Oct 18, 2021
@ShadowOfPhantom
Copy link
Author

@ShadowOfPhantom Could you attach a small sample where reproduce the issue?. Or, could you share more info?. The views where the issue happens, are Frames?, are you using Shell or a FlyoutPage?

Hi! I edited the topic with new info

@jsuarezruiz
Copy link
Contributor

@ShadowOfPhantom Thanks for the attached sample. I am going to review it.

@jsuarezruiz jsuarezruiz removed the s/unverified New report that has yet to be verified label Oct 20, 2021
@jsuarezruiz jsuarezruiz self-assigned this Oct 20, 2021
@jsuarezruiz jsuarezruiz removed s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. labels Oct 20, 2021
@jsuarezruiz
Copy link
Contributor

Mnn, the behavior is different between the sample:
frame-issue-1

And testing the same code with the latest 5.0.0 Xamarin.Forms branch:
frame-issue-2

We need to review also this differences.

@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Nov 26, 2021
@DouglasGiovanella
Copy link

I'm also facing this problem, on 5.0.0.2083 works fine, but updating to 5.0.0.2125 or newer this issue starts to happen when setting a page on the Flyout detail.

@jsuarezruiz running the sample provided by @ShadowOfPhantom i was able to reproduce, but was hard to notice and only happens a few times. I took the liberty to change a few things in it to be more easy to reproduce:

Screen.Recording.2021-12-01.at.15.50.49.mov

ControlColorsIssue.zip

@DouglasGiovanella
Copy link

Well, after some digging i track this issue to the BoxView after the changes on this PR: https://github.com/xamarin/Xamarin.Forms/pull/11812/files and maybe it is related to #14970.

I had a fews problems running the XF project on my Mac, so i couldn't test some fix alternatives and submit a PR.

@jsuarezruiz @jfversluis This issue could be prioritized to the next service release? Since i didn't find any workaround for this, its blocking us to update the XF to its latest version 😞

@jfversluis
Copy link
Member

Thanks for the investigation on this! Can't make promises on prioritizing, but we'll look into it :)

@ShadowOfPhantom
Copy link
Author

The same with navbar if I define Style via App.xaml instead of styles.xml of Android project.
I attached a video

<Style TargetType="NavigationPage">
                <Setter Property="BackgroundColor" Value="{StaticResource MainColor}" />
                <Setter Property="BarBackgroundColor" Value="{StaticResource LightPink}" />
            </Style>

Also it doesn't flicker if I set color like this:

MainPage = new NavigationPage(new TabbedView())
            {
                BarBackgroundColor = Color.LightPink,
            };
IMG_5243.MOV

@DouglasGiovanella
Copy link

After a fews tests, looks like this issue is related to the GradientStrokeDrawable, more precisely on the draw method.

protected override void OnDraw(Shape shape, Canvas canvas, Paint paint)
{
	base.OnDraw(shape, canvas, paint);

	if (_backgroundColor.HasValue)
		paint.Color = _backgroundColor.Value;

	if (_strokePaint.StrokeWidth > 0)
		shape.Draw(canvas, _strokePaint);
}

The call for base.OnDraw(shape, canvas, paint); its draws the view with a black backgound because the Paint color its not set already.
Moving the setter paint.Color = _backgroundColor.Value before the base call seems to solve this issue, but i dont know the real impact of doing this.

As a workaround for the boxView problem, i solved by making a custom renderer for the boxView thats forces the right color on the GradientStrokeDrawable Paint:

[assembly: ExportRenderer(typeof(BoxView), typeof(BoxViewRenderer))]
namespace Test.Droid.Renderers {
    public class BoxViewRenderer : BoxRenderer {
        public BoxViewRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) {
            base.OnElementChanged(e);
            BlackBackgroundWorkaround();
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
            base.OnElementPropertyChanged(sender, e);

            string[] propsToUpdateBackground = {
                VisualElement.BackgroundColorProperty.PropertyName,
                VisualElement.BackgroundProperty.PropertyName,
                BoxView.ColorProperty.PropertyName,
                BoxView.CornerRadiusProperty.PropertyName
            };

            if (propsToUpdateBackground.Contains(e.PropertyName)) {
                BlackBackgroundWorkaround();
            }
        }

        // https://github.com/xamarin/Xamarin.Forms/issues/14740
        private void BlackBackgroundWorkaround() {
            try {
                if (Element.CornerRadius == default) return;

                FieldInfo fieldInfo =
                    typeof(BoxRenderer).GetField("_backgroundDrawable", BindingFlags.NonPublic | BindingFlags.Instance);

                if (fieldInfo != null) {
                    GradientStrokeDrawable gradientStrokeDrawable = fieldInfo.GetValue(this) as GradientStrokeDrawable;

                    AndroidColor? gradientColor = gradientStrokeDrawable?.GetColor();
                    if (gradientColor.HasValue) gradientStrokeDrawable.Paint.Color = gradientColor.Value;
                }
            } catch (Exception e) {
                // something is wrong
            }
        }
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
5.0.0 Regression on 5.0.0 p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. t/bug 🐛
Projects
None yet
Development

No branches or pull requests

4 participants