-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DeviceStateTrigger.cs
51 lines (42 loc) · 1.48 KB
/
DeviceStateTrigger.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
#nullable disable
using Microsoft.Maui.Devices;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/DeviceStateTrigger.xml" path="Type[@FullName='Microsoft.Maui.Controls.DeviceStateTrigger']/Docs/*" />
public sealed class DeviceStateTrigger : StateTriggerBase
{
/// <include file="../../docs/Microsoft.Maui.Controls/DeviceStateTrigger.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public DeviceStateTrigger()
{
}
/// <include file="../../docs/Microsoft.Maui.Controls/DeviceStateTrigger.xml" path="//Member[@MemberName='Device']/Docs/*" />
public string Device
{
get => (string)GetValue(DeviceProperty);
set => SetValue(DeviceProperty, value);
}
/// <summary>Bindable property for <see cref="Device"/>.</summary>
public static readonly BindableProperty DeviceProperty =
BindableProperty.Create(nameof(Device), typeof(string), typeof(DeviceStateTrigger), string.Empty,
propertyChanged: OnDeviceChanged);
static void OnDeviceChanged(BindableObject bindable, object oldvalue, object newvalue)
{
(bindable as DeviceStateTrigger)?.UpdateState();
}
protected override void OnAttached()
{
base.OnAttached();
UpdateState();
}
void UpdateState()
{
if (string.IsNullOrEmpty(Device))
return;
var device = DevicePlatform.Create(Device);
if (device == DevicePlatform.Create("UWP"))
SetActive(DeviceInfo.Platform == DevicePlatform.WinUI);
else
SetActive(DeviceInfo.Platform == device);
}
}
}