-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
BindableObjectExtensions.cs
131 lines (109 loc) · 4.22 KB
/
BindableObjectExtensions.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/BindableObjectExtensions.xml" path="Type[@FullName='Microsoft.Maui.Controls.BindableObjectExtensions']/Docs/*" />
public static class BindableObjectExtensions
{
internal static void RefreshPropertyValue(this BindableObject self, BindableProperty property, object value)
{
var ctx = self.GetContext(property);
if (ctx != null && ctx.Bindings.Count > 0)
{
var binding = ctx.Bindings.Last().Value;
// support bound properties
if (!ctx.Attributes.HasFlag(BindableObject.BindableContextAttributes.IsBeingSet))
binding.Apply(false);
}
else
{
// support normal/code properties
self.SetValue(property, value, SetterSpecificity.FromHandler);
}
}
internal static void PropagateBindingContext<T>(this BindableObject self, IEnumerable<T> children)
=> PropagateBindingContext(self, children, BindableObject.SetInheritedBindingContext);
internal static void PropagateBindingContext<T>(this BindableObject self, IEnumerable<T> children, Action<BindableObject, object> setChildBindingContext)
{
if (children == null)
return;
var bc = self.BindingContext;
foreach (var child in children)
{
var bo = child as BindableObject;
if (bo == null)
continue;
setChildBindingContext(bo, bc);
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableObjectExtensions.xml" path="//Member[@MemberName='SetBinding']/Docs/*" />
public static void SetBinding(this BindableObject self, BindableProperty targetProperty, string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null,
string stringFormat = null)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
if (targetProperty == null)
throw new ArgumentNullException(nameof(targetProperty));
var binding = new Binding(path, mode, converter, stringFormat: stringFormat);
self.SetBinding(targetProperty, binding);
}
public static T GetPropertyIfSet<T>(this BindableObject bindableObject, BindableProperty bindableProperty, T returnIfNotSet)
{
if (bindableObject == null)
return returnIfNotSet;
if (bindableObject.IsSet(bindableProperty))
return (T)bindableObject.GetValue(bindableProperty);
return returnIfNotSet;
}
internal static bool TrySetDynamicThemeColor(
this BindableObject bindableObject,
string resourceKey,
BindableProperty bindableProperty,
out object outerColor)
{
if (Application.Current.TryGetResource(resourceKey, out outerColor))
{
bindableObject.SetDynamicResource(bindableProperty, resourceKey);
return true;
}
return false;
}
internal static void AddRemoveLogicalChildren(this BindableObject bindable, object oldValue, object newValue)
{
if (!(bindable is Element owner))
return;
if (oldValue is Element oldView)
owner.RemoveLogicalChild(oldView);
if (newValue is Element newView)
owner.AddLogicalChild(newView);
}
internal static bool TrySetAppTheme(
this BindableObject self,
string lightResourceKey,
string darkResourceKey,
BindableProperty bindableProperty,
Brush defaultDark,
Brush defaultLight,
out object outerLight,
out object outerDark)
{
if (!Application.Current.TryGetResource(lightResourceKey, out outerLight))
{
outerLight = defaultLight;
}
if (!Application.Current.TryGetResource(darkResourceKey, out outerDark))
{
outerDark = defaultDark;
}
self.SetAppTheme(bindableProperty, outerLight, outerDark);
return (Brush)outerLight != defaultLight || (Brush)outerDark != defaultDark;
}
public static void SetAppTheme<T>(this BindableObject self, BindableProperty targetProperty, T light, T dark) => self.SetBinding(targetProperty, new AppThemeBinding { Light = light, Dark = dark });
/// <include file="../../docs/Microsoft.Maui.Controls/BindableObjectExtensions.xml" path="//Member[@MemberName='SetAppThemeColor']/Docs/*" />
public static void SetAppThemeColor(this BindableObject self, BindableProperty targetProperty, Color light, Color dark)
=> SetAppTheme(self, targetProperty, light, dark);
}
}