-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
OnPlatform.cs
69 lines (61 loc) · 2 KB
/
OnPlatform.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
#nullable disable
using System.Collections.Generic;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Devices;
namespace Microsoft.Maui.Controls
{
[ContentProperty("Platforms")]
public class OnPlatform<T>
{
public OnPlatform()
{
Platforms = new List<On>();
}
public IList<On> Platforms { get; private set; }
bool hasDefault;
T @default;
public T Default
{
get { return @default; }
set
{
hasDefault = true;
@default = value;
}
}
#pragma warning disable RECS0108 // Warns about static fields in generic types
static readonly IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
#pragma warning restore RECS0108 // Warns about static fields in generic types
public static implicit operator T(OnPlatform<T> onPlatform)
{
if (s_valueConverter != null)
{
foreach (var onPlat in onPlatform.Platforms)
{
if (onPlat.Platform == null)
continue;
if (!onPlat.Platform.Contains(DeviceInfo.Platform.ToString()))
continue;
return (T)s_valueConverter.Convert(onPlat.Value, typeof(T), null, null);
}
// fallback for UWP
foreach (var onPlat in onPlatform.Platforms)
{
if (onPlat.Platform != null && onPlat.Platform.Contains("UWP") && DeviceInfo.Platform == DevicePlatform.WinUI)
return (T)s_valueConverter.Convert(onPlat.Value, typeof(T), null, null);
}
}
return onPlatform.hasDefault ? onPlatform.@default : default(T);
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/On.xml" path="Type[@FullName='Microsoft.Maui.Controls.On']/Docs/*" />
[ContentProperty("Value")]
public class On
{
/// <include file="../../docs/Microsoft.Maui.Controls/On.xml" path="//Member[@MemberName='Platform']/Docs/*" />
[System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))]
public IList<string> Platform { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/On.xml" path="//Member[@MemberName='Value']/Docs/*" />
public object Value { get; set; }
}
}