This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
BindingBase.cs
111 lines (86 loc) · 2.69 KB
/
BindingBase.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
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace Xamarin.Forms
{
public abstract class BindingBase
{
static readonly ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext> SynchronizedCollections = new ConditionalWeakTable<IEnumerable, CollectionSynchronizationContext>();
BindingMode _mode = BindingMode.Default;
string _stringFormat;
internal BindingBase()
{
}
public BindingMode Mode
{
get { return _mode; }
set
{
if (value != BindingMode.Default && value != BindingMode.OneWay && value != BindingMode.OneWayToSource && value != BindingMode.TwoWay)
throw new ArgumentException("mode is not a valid BindingMode", "mode");
ThrowIfApplied();
_mode = value;
}
}
public string StringFormat
{
get { return _stringFormat; }
set
{
ThrowIfApplied();
_stringFormat = value;
}
}
internal bool AllowChaining { get; set; }
internal object Context { get; set; }
internal bool IsApplied { get; private set; }
public static void DisableCollectionSynchronization(IEnumerable collection)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
SynchronizedCollections.Remove(collection);
}
public static void EnableCollectionSynchronization(IEnumerable collection, object context, CollectionSynchronizationCallback callback)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (callback == null)
throw new ArgumentNullException(nameof(callback));
SynchronizedCollections.Add(collection, new CollectionSynchronizationContext(context, callback));
}
protected void ThrowIfApplied()
{
if (IsApplied)
throw new InvalidOperationException("Can not change a binding while it's applied");
}
internal virtual void Apply(bool fromTarget)
{
IsApplied = true;
}
internal virtual void Apply(object context, BindableObject bindObj, BindableProperty targetProperty)
{
IsApplied = true;
}
internal abstract BindingBase Clone();
internal virtual object GetSourceValue(object value, Type targetPropertyType)
{
if (StringFormat != null)
return string.Format(StringFormat, value);
return value;
}
internal virtual object GetTargetValue(object value, Type sourcePropertyType)
{
return value;
}
internal static bool TryGetSynchronizedCollection(IEnumerable collection, out CollectionSynchronizationContext synchronizationContext)
{
if (collection == null)
throw new ArgumentNullException(nameof(collection));
return SynchronizedCollections.TryGetValue(collection, out synchronizationContext);
}
internal virtual void Unapply()
{
IsApplied = false;
}
}
}