[Windows] Fix AspectFit image fills container regression causing UI test failures on candidate branch#35923
Merged
kubaflo merged 1 commit intoJun 15, 2026
Conversation
kubaflo
approved these changes
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue.
Thank you!
Root Cause
PR #34699 introduced
_cachedImageSizeto eliminate timing-dependent behavior inGetDesiredSize(). As part of that change,MapSourceAsync()resets the cache toZerowhenever a new image source is assigned.However,
GetDesiredSize()was updated to rely exclusively on_cachedImageSizeand no longer considered the live decoded size available onBitmapSource. This created a timing gap where WinUI had already completed image decoding and populatedBitmapSource.PixelWidth/PixelHeight, but the asynchronousImageOpenedevent had not yet fired to update the cache.If a layout pass occurred during this window,
GetDesiredSize()observed a zero-sized cache, skipped the natural-size constraint, and allowed the image to expand to the available container size instead of respecting its decoded pixel dimensions.Description of Change
Two minimal updates were made in
ImageHandler.Windows.cs:GetDesiredSize()BitmapSourcefirst._cachedImageSizeonly when the live size is not yet available.UpdatePlatformMaxConstraints().GetImageSize()PlatformViewto prevent a potential null-reference exception whenGetDesiredSize()is called after handler disconnection.Regression Introduced By
PR #34699
Issues Fixed
Fixes the following candidate branch regressions:
In
Issue30403SmallImages.csfile:ConstrainedContainers_ShouldHandleSmallImagesSmallImageEndAlignment_ShouldRespectAlignmentSmallImageInLargeContainer_ShouldNotExceedIntrinsicSizeSmallImageStartAlignment_ShouldRespectAlignmentSizeComparison_ShouldShowDifferentSizesTinyImage_ShouldRemainTinyTested the behaviour in the following platforms