dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.72k forks source link

Invalidate() does not invoke Draw() #24035

Open nk-alex opened 1 month ago

nk-alex commented 1 month ago

Description

Maybe related to #12560. I'm transitioning my Xamarin.Forms custom view to maui when I encontered this problem.

Here is how my custom view renderer used to look back in Xamarin.Forms:

internal class CustomViewRenderer : ViewRenderer
{
    private CustomView _customView;
    private int _screenPixelsWidth;

    public CustomViewRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
        {
            return;
        }

        SetLayerType(LayerType.Software, null);
        SetBackgroundColor(Color.Transparent);

        _customView = (CustomView)e.NewElement;
        _customView.DrawRectangleHole += OnDrawEvent;

        var displayMetrics = new DisplayMetrics();
        CrossCurrentActivity.Current.Activity.WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
        _screenPixelsWidth = displayMetrics.WidthPixels;
    }

    protected override void OnDraw(Canvas canvas)
    {
        //Do Stuff
    }

    private void OnDrawEvent(object sender, EventArgs e)
    {
        Invalidate();
    }
}

This is how is looking on MAUI:

internal class CustomViewRenderer : ViewRenderer
{
    private CustomView _customView;
    private int _screenPixelsWidth;

    public CustomViewRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Microsoft.Maui.Controls.View> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
        {
            return;
        }

        SetLayerType(LayerType.Software, null);
        SetBackgroundColor(Color.Transparent);

        _customView = (CustomView)e.NewElement;
        _customView.DrawRectangleHole += OnDrawEvent;

        var displayMetrics = new DisplayMetrics();

        //Seems depercated bet still returns metrics
        Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics);

        _screenPixelsWidth = displayMetrics.WidthPixels;
    }

    protected override void OnDraw(Canvas canvas)
    {
        //Do Stuff
    }

    private void OnDrawEvent(object sender, EventArgs e)
    {
        Invalidate();
    }
}

OnDrawEvent is called, Invalidate() is called as well, but OnDraw never gets called. Am I doing something wrong?

Steps to Reproduce

Create a custom view public class CustomView : View

Create a custom renderer and try to Invalidate() in order to get OnDraw executed

Link to public reproduction project repository

No response

Version with bug

8.0.71 SR7.1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 28.0

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Zhanglirong-Winnie commented 1 day ago

@nk-alex Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!