Skip to content

Commit 971791a

Browse files
Fix ImageHandler Vertical&Horizontal Options with AspectFit
1 parent ce4d7d6 commit 971791a

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 30403, "Image under WinUI does not respect VerticalOptions and HorizontalOptions with AspectFit", PlatformAffected.UWP)]
4+
public class Issue30403 : TestContentPage
5+
{
6+
protected override void Init()
7+
{
8+
Title = "Issue 30403";
9+
10+
Content = new Grid
11+
{
12+
BackgroundColor = Colors.LightGray,
13+
Children =
14+
{
15+
new Image
16+
{
17+
AutomationId = "TestImage",
18+
Source = "dotnet_bot.png",
19+
Aspect = Aspect.AspectFit,
20+
VerticalOptions = LayoutOptions.Center,
21+
HorizontalOptions = LayoutOptions.Center,
22+
}
23+
}
24+
};
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue30403 : _IssuesUITest
8+
{
9+
public Issue30403(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "Image under WinUI does not respect VerticalOptions and HorizontalOptions with AspectFit";
14+
15+
[Test]
16+
[Category(UITestCategories.Image)]
17+
public void ImageRespectsVerticalAndHorizontalOptionsWithAspectFit()
18+
{
19+
App.WaitForElement("TestImage");
20+
21+
// Verify that the image is positioned correctly according to VerticalOptions.Center and HorizontalOptions.Center
22+
// with AspectFit aspect ratio. The image should appear in the center of the Grid.
23+
VerifyScreenshot();
24+
}
25+
}

src/Core/src/Handlers/Image/ImageHandler.Windows.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.UI.Xaml;
34
using Microsoft.UI.Xaml.Controls;
45
using Microsoft.UI.Xaml.Media;
6+
using Microsoft.UI.Xaml.Media.Imaging;
57
using WImage = Microsoft.UI.Xaml.Controls.Image;
68

79
namespace Microsoft.Maui.Handlers
@@ -55,6 +57,38 @@ protected override void RemoveContainer()
5557
UpdateValue(nameof(IView.Width));
5658
}
5759

60+
/// <inheritdoc/>
61+
public override Graphics.Size GetDesiredSize(double widthConstraint, double heightConstraint)
62+
{
63+
if (VirtualView.Aspect == Aspect.AspectFit
64+
&& VirtualView.HorizontalLayoutAlignment != Primitives.LayoutAlignment.Fill
65+
&& VirtualView.VerticalLayoutAlignment != Primitives.LayoutAlignment.Fill)
66+
{
67+
if (PlatformView.MaxWidth == double.PositiveInfinity
68+
&& PlatformView.MaxHeight == double.PositiveInfinity)
69+
{
70+
var imageSize = GetImageSize();
71+
72+
if (imageSize.Width != 0 && imageSize.Height != 0)
73+
{
74+
PlatformView.MaxWidth = imageSize.Width;
75+
PlatformView.MaxHeight = imageSize.Height;
76+
}
77+
}
78+
}
79+
else
80+
{
81+
if (VirtualView.MaximumHeight != PlatformView.MaxHeight
82+
|| VirtualView.MaximumWidth != PlatformView.MaxWidth)
83+
{
84+
PlatformView.MaxWidth = VirtualView.MaximumWidth;
85+
PlatformView.MaxHeight = VirtualView.MaximumHeight;
86+
}
87+
}
88+
89+
return base.GetDesiredSize(widthConstraint, heightConstraint);
90+
}
91+
5892
/// <summary>
5993
/// Maps the abstract <see cref="IView.Height"/> property to the platform-specific implementations.
6094
/// </summary>
@@ -146,6 +180,20 @@ void OnImageOpened(object sender, RoutedEventArgs e)
146180
UpdateValue(nameof(IImage.IsAnimationPlaying));
147181
}
148182

183+
private Graphics.Size GetImageSize()
184+
{
185+
if (PlatformView.Source is BitmapSource bitmap)
186+
{
187+
// BitmapSource may not have PixelWidth/PixelHeight set until image is loaded
188+
if (bitmap.PixelWidth > 0 && bitmap.PixelHeight > 0)
189+
{
190+
return new Graphics.Size(bitmap.PixelWidth, bitmap.PixelHeight);
191+
}
192+
// If not available, set to zero
193+
}
194+
return Graphics.Size.Zero;
195+
}
196+
149197
partial class ImageImageSourcePartSetter
150198
{
151199
public override void SetImageSource(ImageSource? platformImage)

src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Microsoft.Maui.SwipeViewSwipeEnded.Deconstruct(out Microsoft.Maui.SwipeDirection
2626
Microsoft.Maui.SwipeViewSwipeEnded.SwipeViewSwipeEnded(Microsoft.Maui.SwipeViewSwipeEnded! original) -> void
2727
Microsoft.Maui.SwipeViewSwipeStarted.Deconstruct(out Microsoft.Maui.SwipeDirection SwipeDirection) -> void
2828
Microsoft.Maui.SwipeViewSwipeStarted.SwipeViewSwipeStarted(Microsoft.Maui.SwipeViewSwipeStarted! original) -> void
29+
override Microsoft.Maui.Handlers.ImageHandler.GetDesiredSize(double widthConstraint, double heightConstraint) -> Microsoft.Maui.Graphics.Size
2930
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.Equals(object? obj) -> bool
3031
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.GetHashCode() -> int
3132
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.ToString() -> string!

0 commit comments

Comments
 (0)