-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
OrientationStateTrigger.cs
63 lines (53 loc) · 1.94 KB
/
OrientationStateTrigger.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
#nullable disable
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Devices;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/OrientationStateTrigger.xml" path="Type[@FullName='Microsoft.Maui.Controls.OrientationStateTrigger']/Docs/*" />
public sealed class OrientationStateTrigger : StateTriggerBase
{
/// <include file="../../docs/Microsoft.Maui.Controls/OrientationStateTrigger.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public OrientationStateTrigger()
{
UpdateState();
}
/// <include file="../../docs/Microsoft.Maui.Controls/OrientationStateTrigger.xml" path="//Member[@MemberName='Orientation']/Docs/*" />
public DisplayOrientation Orientation
{
get => (DisplayOrientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
/// <summary>Bindable property for <see cref="Orientation"/>.</summary>
public static readonly BindableProperty OrientationProperty =
BindableProperty.Create(nameof(Orientation), typeof(DisplayOrientation), typeof(OrientationStateTrigger), null,
propertyChanged: OnOrientationChanged);
static void OnOrientationChanged(BindableObject bindable, object oldvalue, object newvalue)
{
((OrientationStateTrigger)bindable).UpdateState();
}
protected override void OnAttached()
{
base.OnAttached();
if (!DesignMode.IsDesignModeEnabled)
{
UpdateState();
DeviceDisplay.MainDisplayInfoChanged += OnInfoPropertyChanged;
}
}
protected override void OnDetached()
{
base.OnDetached();
DeviceDisplay.MainDisplayInfoChanged -= OnInfoPropertyChanged;
}
void OnInfoPropertyChanged(object sender, DisplayInfoChangedEventArgs e) =>
UpdateState();
void UpdateState()
{
var currentOrientation = DeviceDisplay.MainDisplayInfo.Orientation;
if (Orientation.IsLandscape())
SetActive(currentOrientation.IsLandscape());
else
SetActive(currentOrientation.IsPortrait());
}
}
}