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
22.14k stars 1.74k forks source link

SizeRequest OnMeasure doesn't fire #14202

Open prok155 opened 1 year ago

prok155 commented 1 year ago

Description

When I override method SizeRequest OnMeasure(double widthConstraint, double heightConstraint) in my custom control it is not getting fired on all platforms. I'm unable to migrate my project from Xamarin to MAUI and I think that some custom controls may not work because of that bug.

Steps to Reproduce

  1. Create MAUI project.

  2. Create following custom control:

    public class MyButton : Button
    {
        protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
        {
            //some logic
            {
    
            }
            return base.OnMeasure(widthConstraint, heightConstraint);
        }
    }
  3. You can debug and see that code inside protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) method never fires.

Link to public reproduction project repository

https://github.com/prok155/OnMeasureBug

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

iOS,Android,Windows,macOS

Did you find any workaround?

Method OnMeasure(double widthConstraint, double heightConstraint) fires in my Xamarin.Forms project, so I think we should wait with migration to MAUI until it will be fixed.

Relevant log output

No response

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

Zhanglirong-Winnie commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 1.0. Can repro on any platform with sample project. OnMeasureBug-master.zip

prok155 commented 1 year ago

I found workaround. There are 2 methods:

protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) protected override Size MeasureOverride(double widthConstraint, double heightConstraint)

When I override MeasureOverride and call InvalidateMeasure() from my content page, overrided method MeasureOverride fires correctly.

In Xamarin.Forms there is only one method OnMeasure, in MAUI there is additional method MeasureOverride. Could you explain the difference between OnMeasure and MeasureOverride? It seems that these methods work almost the same..

This bug is not fixed yet, because MeasureOverride should fire without calling InvalidateMeasure() all the time, but I think that this workaround will help you to fix this issue.