Skip to content

Commit b4d1237

Browse files
committed
Add implicit converter for integer
1 parent 84adf91 commit b4d1237

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Globalization;
67

78
namespace Microsoft.Toolkit.Uwp.UI.Controls
@@ -63,6 +64,13 @@ public AspectRatio(double ratio)
6364
/// <param name="ratio"><see cref="double"/> value representing the <see cref="AspectRatio"/>.</param>
6465
public static implicit operator AspectRatio(double ratio) => new AspectRatio(ratio);
6566

67+
/// <summary>
68+
/// Implicit conversion operator to convert a <see cref="int"/> to an <see cref="AspectRatio"/> value.
69+
/// Creates a simple aspect ratio of N:1, where N is int
70+
/// </summary>
71+
/// <param name="width"><see cref="int"/> value representing the <see cref="AspectRatio"/>.</param>
72+
public static implicit operator AspectRatio(int width) => new AspectRatio(width, 1.0);
73+
6674
/// <summary>
6775
/// Converter to take a string aspect ration like "16:9" and convert it to an <see cref="AspectRatio"/> struct.
6876
/// Used automatically by XAML.

UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,46 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
9191
});
9292
}
9393

94+
[TestCategory("ConstrainedBox")]
95+
[TestMethod]
96+
public async Task Test_ConstrainedBox_Normal_IntegerWidth()
97+
{
98+
await App.DispatcherQueue.EnqueueAsync(async () =>
99+
{
100+
var treeRoot = XamlReader.Load(@"<Page
101+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
102+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
103+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
104+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2"" Height=""100"">
105+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
106+
</controls:ConstrainedBox>
107+
</Page>") as FrameworkElement;
108+
109+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
110+
111+
// Initialize Visual Tree
112+
await SetTestContentAsync(treeRoot);
113+
114+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
115+
116+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
117+
118+
// Check Size
119+
Assert.AreEqual(2.0, panel.AspectRatio, 0.01, "ApectRatio does not meet expected value of 2.0");
120+
121+
// Force Layout calculations
122+
panel.UpdateLayout();
123+
124+
var child = panel.Content as Border;
125+
126+
Assert.IsNotNull(child, "Could not find inner Border");
127+
128+
// Check Size
129+
Assert.AreEqual(200, child.ActualWidth, 0.01, "Actual width does not meet expected value of 200");
130+
Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100");
131+
});
132+
}
133+
94134
[TestCategory("ConstrainedBox")]
95135
[TestMethod]
96136
public void Test_ConstrainedBox_AspectRatioParsing_WidthAndHeight()

0 commit comments

Comments
 (0)