Skip to content

Commit d398415

Browse files
wip
1 parent 15b7beb commit d398415

File tree

17 files changed

+140
-10
lines changed

17 files changed

+140
-10
lines changed

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", "GridLength.GridLengthTypeConverter")), typeof(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
/// <include file="../../docs/Microsoft.Maui.Controls/ColumnDefinition.xml" path="//Member[@MemberName='Width']/Docs/*" />
22-
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
22+
[System.ComponentModel.TypeConverter(typeof(Microsoft.Maui.GridLength.GridLengthTypeConverter))]
2323
public GridLength Width
2424
{
2525
get { return (GridLength)GetValue(WidthProperty); }

src/Controls/src/Core/ColumnDefinitionCollectionTypeConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
2424
if (strValue != null)
2525
{
2626
var lengths = strValue.Split(',');
27-
var converter = new GridLengthTypeConverter();
27+
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
2828
var definitions = new ColumnDefinition[lengths.Length];
2929
for (var i = 0; i < lengths.Length; i++)
3030
definitions[i] = new ColumnDefinition { Width = (GridLength)converter.ConvertFromInvariantString(lengths[i]) };
@@ -39,7 +39,7 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
3939
{
4040
if (value is not ColumnDefinitionCollection cdc)
4141
throw new NotSupportedException();
42-
var converter = new GridLengthTypeConverter();
42+
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
4343
return string.Join(", ", cdc.Select(cd => converter.ConvertToInvariantString(cd.Width)));
4444
}
4545
}

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.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(Microsoft.Maui.GridLength.GridLengthTypeConverter))]
2525
public GridLength Height
2626
{
2727
get { return (GridLength)GetValue(HeightProperty); }

src/Controls/src/Core/RowDefinitionCollectionTypeConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
2424
if (strValue != null)
2525
{
2626
var lengths = strValue.Split(',');
27-
var converter = new GridLengthTypeConverter();
27+
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
2828
var definitions = new RowDefinition[lengths.Length];
2929
for (var i = 0; i < lengths.Length; i++)
3030
definitions[i] = new RowDefinition { Height = (GridLength)converter.ConvertFromInvariantString(lengths[i]) };
@@ -38,7 +38,7 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
3838
{
3939
if (value is not RowDefinitionCollection rdc)
4040
throw new NotSupportedException();
41-
var converter = new GridLengthTypeConverter();
41+
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
4242
return string.Join(", ", rdc.Select(rd => converter.ConvertToInvariantString(rd.Height)));
4343
}
4444
}
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+
}

src/Core/src/Primitives/GridLength.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,42 @@ public override string ToString()
120120
/// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
121121
public static bool operator !=(GridLength left, GridLength right) => !(left == right);
122122

123-
private sealed class GridLengthTypeConverter : TypeConverter
123+
public sealed class GridLengthTypeConverter : TypeConverter
124124
{
125125
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
126-
=> sourceType == typeof(double);
126+
=> sourceType == typeof(double) || sourceType == typeof(string);
127127

128128
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
129129
=> value switch
130130
{
131131
double d => (GridLength)d,
132+
string strValue => strValue.Trim() switch
133+
{
134+
"auto" => GridLength.Auto,
135+
"*" => new GridLength(1, GridUnitType.Star),
136+
#pragma warning disable CA1846, CA1865
137+
_ 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),
138+
#pragma warning restore CA1846, CA1865
139+
_ when double.TryParse(strValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length),
140+
_ => throw new FormatException(),
141+
},
132142
_ => throw new NotSupportedException(),
133143
};
134144

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();
145+
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);
146+
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
147+
{
148+
if (destinationType == typeof(string) && value is GridLength length)
149+
{
150+
if (length.IsAuto)
151+
return "auto";
152+
if (length.IsStar)
153+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
154+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}";
155+
}
156+
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
157+
158+
}
137159
}
138160
}
139161
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#nullable enable
22
Microsoft.Maui.ElementHandlerExtensions
3+
Microsoft.Maui.GridLength.GridLengthTypeConverter
4+
Microsoft.Maui.GridLength.GridLengthTypeConverter.GridLengthTypeConverter() -> void
5+
override Microsoft.Maui.GridLength.GridLengthTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) -> bool
6+
override Microsoft.Maui.GridLength.GridLengthTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
7+
override Microsoft.Maui.GridLength.GridLengthTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
8+
override Microsoft.Maui.GridLength.GridLengthTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type? destinationType) -> object!
39
Microsoft.Maui.HandlerDisconnectPolicy
410
Microsoft.Maui.HandlerDisconnectPolicy.Automatic = 0 -> Microsoft.Maui.HandlerDisconnectPolicy
511
Microsoft.Maui.HandlerDisconnectPolicy.Manual = 1 -> Microsoft.Maui.HandlerDisconnectPolicy

0 commit comments

Comments
 (0)