-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
LayoutOptionsConverter.cs
93 lines (84 loc) · 3.31 KB
/
LayoutOptionsConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#nullable disable
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.Maui.Controls.Internals;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/LayoutOptionsConverter.xml" path="Type[@FullName='Microsoft.Maui.Controls.LayoutOptionsConverter']/Docs/*" />
[Xaml.ProvideCompiled("Microsoft.Maui.Controls.XamlC.LayoutOptionsConverter")]
public sealed class LayoutOptionsConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;
#pragma warning disable CS0618 // Type or member is obsolete (AndExpand options)
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
if (strValue != null)
{
var parts = strValue.Split('.');
if (parts.Length > 2 || (parts.Length == 2 && parts[0] != "LayoutOptions"))
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(LayoutOptions)}");
strValue = parts[parts.Length - 1];
switch (strValue)
{
case "Start":
return LayoutOptions.Start;
case "Center":
return LayoutOptions.Center;
case "End":
return LayoutOptions.End;
case "Fill":
return LayoutOptions.Fill;
case "StartAndExpand":
return LayoutOptions.StartAndExpand;
case "CenterAndExpand":
return LayoutOptions.CenterAndExpand;
case "EndAndExpand":
return LayoutOptions.EndAndExpand;
case "FillAndExpand":
return LayoutOptions.FillAndExpand;
}
FieldInfo field = typeof(LayoutOptions).GetFields().FirstOrDefault(fi => fi.IsStatic && fi.Name == strValue);
if (field != null)
return (LayoutOptions)field.GetValue(null);
}
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(LayoutOptions)}");
}
#pragma warning restore CS0618 // Type or member is obsolete
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not LayoutOptions options)
throw new NotSupportedException();
if (options.Alignment == LayoutAlignment.Start)
return $"{nameof(LayoutAlignment.Start)}{(options.Expands ? "AndExpand" : "")}";
if (options.Alignment == LayoutAlignment.Center)
return $"{nameof(LayoutAlignment.Center)}{(options.Expands ? "AndExpand" : "")}";
if (options.Alignment == LayoutAlignment.End)
return $"{nameof(LayoutAlignment.End)}{(options.Expands ? "AndExpand" : "")}";
if (options.Alignment == LayoutAlignment.Fill)
return $"{nameof(LayoutAlignment.Fill)}{(options.Expands ? "AndExpand" : "")}";
throw new NotSupportedException();
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
=> true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
=> false;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
=> new(new[] {
"Start",
"Center",
"End",
"Fill",
"StartAndExpand",
"CenterAndExpand",
"EndAndExpand",
"FillAndExpand"
});
}
}