forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppThemeBinding.cs
89 lines (76 loc) · 1.8 KB
/
AppThemeBinding.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
using System;
namespace Xamarin.Forms
{
class AppThemeBinding : BindingBase
{
WeakReference<BindableObject> _weakTarget;
BindableProperty _targetProperty;
public AppThemeBinding() => Application.Current.RequestedThemeChanged += (o, e) => Device.BeginInvokeOnMainThread(() => ApplyCore());
internal override BindingBase Clone() => new AppThemeBinding
{
Light = Light,
_isLightSet = _isLightSet,
Dark = Dark,
_isDarkSet = _isDarkSet,
Default = Default
};
internal override void Apply(bool fromTarget)
{
base.Apply(fromTarget);
ApplyCore();
}
internal override void Apply(object context, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
{
_weakTarget = new WeakReference<BindableObject>(bindObj);
_targetProperty = targetProperty;
base.Apply(context, bindObj, targetProperty, fromBindingContextChanged);
ApplyCore();
}
internal override void Unapply(bool fromBindingContextChanged = false)
{
base.Unapply(fromBindingContextChanged);
_weakTarget = null;
_targetProperty = null;
}
void ApplyCore()
{
if (_weakTarget == null || !_weakTarget.TryGetTarget(out var target))
return;
target?.SetValueCore(_targetProperty, GetValue());
}
object _light;
object _dark;
bool _isLightSet;
bool _isDarkSet;
public object Light
{
get => _light;
set
{
_light = value;
_isLightSet = true;
}
}
public object Dark
{
get => _dark;
set
{
_dark = value;
_isDarkSet = true;
}
}
public object Default { get; set; }
object GetValue()
{
switch (Application.Current.RequestedTheme)
{
default:
case OSAppTheme.Light:
return _isLightSet ? Light : Default;
case OSAppTheme.Dark:
return _isDarkSet ? Dark : Default;
}
}
}
}