Skip to content

Commit baa8a9b

Browse files
[X|C] only use a single GridLengthTypeConverter
- fixes #29334
1 parent 3273d2b commit baa8a9b

File tree

17 files changed

+157
-20
lines changed

17 files changed

+157
-20
lines changed

src/Controls/Foldable/src/TwoPaneView.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Maui.Devices;
66
using Microsoft.Maui.Foldable;
77
using Microsoft.Maui.Graphics;
8+
using Microsoft.Maui.Converters;
89

910
namespace Microsoft.Maui.Controls.Foldable
1011
{
@@ -152,7 +153,7 @@ public double MinWideModeWidth
152153
/// <summary>
153154
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 1, or sets the GridLength value of pane 1.
154155
/// </summary>
155-
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
156+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
156157
public GridLength Pane1Length
157158
{
158159
get { return (GridLength)GetValue(Pane1LengthProperty); }
@@ -162,7 +163,7 @@ public GridLength Pane1Length
162163
/// <summary>
163164
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 2, or sets the GridLength value of pane 2.
164165
/// </summary>
165-
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
166+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
166167
public GridLength Pane2Length
167168
{
168169
get { return (GridLength)GetValue(Pane2LengthProperty); }

src/Controls/src/Build.Tasks/XamlCache.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public ICollection<string> GetResourceNamesInUse(VariableDefinition variableDefi
7474
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexAlignSelfTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexAlignSelf>) },
7575
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexWrapTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexWrap>) },
7676
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexBasisTypeConverter")), typeof(FlexBasisTypeConverter) },
77+
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "GridLengthTypeConverter")), typeof(Converters.GridLengthTypeConverter) },
78+
7779
};
7880

7981
// State used by SetPropertiesVisitor

src/Controls/src/Core/ColumnDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ColumnDefinition(GridLength width)
1919
=> SetValue(WidthProperty, width);
2020

2121
/// <summary>Gets or sets the width of the column.</summary>
22-
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
22+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
2323
public GridLength Width
2424
{
2525
get { return (GridLength)GetValue(WidthProperty); }

src/Controls/src/Core/GridLengthTypeConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Microsoft.Maui.Controls
88
{
99
[ProvideCompiled("Microsoft.Maui.Controls.XamlC.GridLengthTypeConverter")]
10+
[Obsolete("Microsoft.Maui.Controls.GridLengthTypeConverter is obsolete. Use Microsoft.Maui.Converters.GridLengthConverter instead.")]
1011
public class GridLengthTypeConverter : TypeConverter
1112
{
1213
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

src/Controls/src/Core/RowDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public RowDefinition(GridLength height)
2121
}
2222

2323
/// <include file="../../docs/Microsoft.Maui.Controls/RowDefinition.xml" path="//Member[@MemberName='Height']/Docs/*" />
24-
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
24+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
2525
public GridLength Height
2626
{
2727
get { return (GridLength)GetValue(HeightProperty); }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui29334"
5+
Title="Maui29334">
6+
<Grid x:Name="grid">
7+
<Grid.RowDefinitions>
8+
<RowDefinition>
9+
<RowDefinition.Height>
10+
<OnIdiom x:TypeArguments="GridLength" Default="50" Desktop="100"/>
11+
</RowDefinition.Height>
12+
</RowDefinition>
13+
<RowDefinition Height="*" />
14+
</Grid.RowDefinitions>
15+
16+
<Label Text="Hello, World!" />
17+
</Grid>
18+
19+
</ContentPage>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
2+
3+
using Microsoft.Maui.ApplicationModel;
4+
using Microsoft.Maui.Controls.Core.UnitTests;
5+
using Microsoft.Maui.Dispatching;
6+
using Microsoft.Maui.UnitTests;
7+
using NUnit.Framework;
8+
9+
public partial class Maui29334 : ContentPage
10+
{
11+
12+
public Maui29334() => InitializeComponent();
13+
14+
public Maui29334(bool useCompiledXaml)
15+
{
16+
//this stub will be replaced at compile time
17+
}
18+
19+
[TestFixture]
20+
class Test
21+
{
22+
[SetUp]
23+
public void Setup()
24+
{
25+
Application.SetCurrentApplication(new MockApplication());
26+
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
27+
}
28+
29+
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);
30+
31+
[Test]
32+
public void OnIdiomGridLength([Values] bool useCompiledXaml)
33+
{
34+
var page = new Maui29334(useCompiledXaml);
35+
36+
}
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Globalization;
4+
5+
namespace Microsoft.Maui.Converters;
6+
7+
public sealed class GridLengthTypeConverter : TypeConverter
8+
{
9+
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
10+
=> sourceType == typeof(double) || sourceType == typeof(string);
11+
12+
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
13+
=> value switch
14+
{
15+
double d => (GridLength)d,
16+
string strValue => strValue.Trim().ToLowerInvariant() switch
17+
{
18+
"auto" => GridLength.Auto,
19+
"*" => new GridLength(1, GridUnitType.Star),
20+
#pragma warning disable CA1846, CA1865
21+
_ when strValue.EndsWith("*", StringComparison.Ordinal) && double.TryParse(strValue.Substring(0, strValue.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length, GridUnitType.Star),
22+
#pragma warning restore CA1846, CA1865
23+
_ when double.TryParse(strValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length),
24+
_ => throw new FormatException(),
25+
},
26+
_ => throw new NotSupportedException(),
27+
};
28+
29+
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);
30+
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
31+
{
32+
if (destinationType == typeof(string) && value is GridLength length)
33+
{
34+
if (length.IsAuto)
35+
return "auto";
36+
if (length.IsStar)
37+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
38+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}";
39+
}
40+
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
41+
42+
}
43+
}

src/Core/src/Primitives/GridLength.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Diagnostics;
55
using System.Globalization;
6+
using Microsoft.Maui.Converters;
67

78
namespace Microsoft.Maui
89
{
@@ -119,21 +120,5 @@ public override string ToString()
119120
/// <param name="right">The second GridLength to compare.</param>
120121
/// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
121122
public static bool operator !=(GridLength left, GridLength right) => !(left == right);
122-
123-
private sealed class GridLengthTypeConverter : TypeConverter
124-
{
125-
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
126-
=> sourceType == typeof(double);
127-
128-
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
129-
=> value switch
130-
{
131-
double d => (GridLength)d,
132-
_ => throw new NotSupportedException(),
133-
};
134-
135-
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => false;
136-
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) => throw new NotSupportedException();
137-
}
138123
}
139124
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#nullable enable
2+
Microsoft.Maui.Converters.GridLengthTypeConverter
3+
Microsoft.Maui.Converters.GridLengthTypeConverter.GridLengthTypeConverter() -> void
24
*REMOVED*Microsoft.Maui.Platform.MauiPicker.ShowPopupOnFocus.get -> bool
35
*REMOVED*Microsoft.Maui.Platform.MauiPicker.ShowPopupOnFocus.set -> void
46
*REMOVED*override Microsoft.Maui.Platform.MauiPicker.OnFocusChanged(bool gainFocus, Android.Views.FocusSearchDirection direction, Android.Graphics.Rect? previouslyFocusedRect) -> void
@@ -32,6 +34,10 @@ Microsoft.Maui.SwipeViewSwipeEnded.Deconstruct(out Microsoft.Maui.SwipeDirection
3234
Microsoft.Maui.SwipeViewSwipeEnded.SwipeViewSwipeEnded(Microsoft.Maui.SwipeViewSwipeEnded! original) -> void
3335
Microsoft.Maui.SwipeViewSwipeStarted.Deconstruct(out Microsoft.Maui.SwipeDirection SwipeDirection) -> void
3436
Microsoft.Maui.SwipeViewSwipeStarted.SwipeViewSwipeStarted(Microsoft.Maui.SwipeViewSwipeStarted! original) -> void
37+
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) -> bool
38+
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
39+
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
40+
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type? destinationType) -> object!
3541
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.Equals(object? obj) -> bool
3642
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.GetHashCode() -> int
3743
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.ToString() -> string!

0 commit comments

Comments
 (0)