Repository navigation
fix: Workaround a lifecycle bug to allow ContentControl to be measured properly when not in visual tree - #14368
Youssef1313 wants to merge 5 commits into
Conversation
| protected override Size MeasureOverride(Size availableSize) => base.MeasureOverride(availableSize); | ||
| protected override Size MeasureOverride(Size availableSize) | ||
| { | ||
| // This is the same as Control.MeasureOverride, except that we use Content if FindFirstChild is null. |
There was a problem hiding this comment.
Can you create an issue to track this issue specifically? Just so we can include it in lifecycle fixes backlog
There was a problem hiding this comment.
@MartinZikmund I guess this is already #190 ?
The issue mentions:
On Windows the template of a control is being applied only when this control is in the visual tree and either we explicitly invoke the ApplyTemplate method, or we wait for the first measure pass.
The case I'm dealing with here is the "wait for the first measure pass". I'll add a link to the issue in code comments.
| var child = this.FindFirstChild() ?? (Content as UIElement); | ||
| return child != null ? MeasureElement(child, availableSize) : new Size(0, 0); |
There was a problem hiding this comment.
Looks like this is causing another issue:
Could be that we were testing for an invalid behavior, though:
There was a problem hiding this comment.
@jeromelaban This test also fails in WinUI. However, there is another NaN-measuring test that doesn't fail in WinUI. So I still need to figure that out 👀
There was a problem hiding this comment.
@jeromelaban The exception on WinUI is a weird "COMException". It happens when Measure is called from C# with NaN. However, if Measure is called with NaN somewhere in the C++ code, it is allowed.
I think it should be acceptable to modify this test and expect no exception be thrown?
To see this behavior on WinUI, use this code
var grid = new Grid();
var SUT = new MyControl02();
SUT.Content = new Grid();
grid.Children.Add(SUT);
SUT.BaseAvailableSize = new Size(double.NaN, double.NaN);
grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));and
public partial class MyControl02 : ContentControl
{
public List<Size> MeasureOverrides { get; } = new List<Size>();
public Size? BaseAvailableSize;
protected override Size MeasureOverride(Size availableSize)
{
MeasureOverrides.Add(availableSize);
//(Content as UIElement).Measure(BaseAvailableSize ?? availableSize);
//return (Content as UIElement).DesiredSize;
return base.MeasureOverride(BaseAvailableSize ?? availableSize);
}
}The above code will work without any crashes/exceptions.
However, base.MeasureOverride will end up calling ContentControl.MeasureOverride which in turn will call Measure with NaN.
Now, if you remove the base.MeasureOverride line and uncomment the two lines above it, it will crash.
There was a problem hiding this comment.
This was likely present to align with some original behavior. Did you take a look at why this test was originally added?
Otherwise, if adjusting the test matches more with what WinUI does, I'm good with it.
There was a problem hiding this comment.
fac0b0c to
8629db6
Compare
|
Fixed in lifecycle already :) |

GitHub Issue (If applicable): Related #7271
PR Type
What kind of change does this PR introduce?
What is the current behavior?
What is the new behavior?
Copilot Summary
🤖 Generated by Copilot at 5b95944
This pull request fixes a unit test failure on Skia by overriding the
MeasureOverridemethod ofContentControlto handle a null first child. This is a temporary workaround for a bug in the ContentControl measurement logic on Skia.PR Checklist
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.Other information
Internal Issue (If applicable):