-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncVirtualizingCollection.cs
224 lines (192 loc) · 7.37 KB
/
AsyncVirtualizingCollection.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
namespace DataVirtualization
{
/// <summary>
/// Derived VirtualizatingCollection, performing loading asychronously.
/// </summary>
/// <typeparam name="T">The type of items in the collection</typeparam>
public class AsyncVirtualizingCollection<T> : VirtualizingCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AsyncVirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
public AsyncVirtualizingCollection(IItemsProvider<T> itemsProvider)
: base(itemsProvider)
{
_synchronizationContext = SynchronizationContext.Current;
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncVirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
/// <param name="pageSize">Size of the page.</param>
public AsyncVirtualizingCollection(IItemsProvider<T> itemsProvider, int pageSize)
: base(itemsProvider, pageSize)
{
_synchronizationContext = SynchronizationContext.Current;
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncVirtualizingCollection<T>"/> class.
/// </summary>
/// <param name="itemsProvider">The items provider.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="pageTimeout">The page timeout.</param>
public AsyncVirtualizingCollection(IItemsProvider<T> itemsProvider, int pageSize, int pageTimeout)
: base(itemsProvider, pageSize, pageTimeout)
{
_synchronizationContext = SynchronizationContext.Current;
}
#endregion
#region SynchronizationContext
private readonly SynchronizationContext _synchronizationContext;
/// <summary>
/// Gets the synchronization context used for UI-related operations. This is obtained as
/// the current SynchronizationContext when the AsyncVirtualizingCollection is created.
/// </summary>
/// <value>The synchronization context.</value>
protected SynchronizationContext SynchronizationContext
{
get { return _synchronizationContext; }
}
#endregion
#region INotifyCollectionChanged
/// <summary>
/// Occurs when the collection changes.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Raises the <see cref="E:CollectionChanged"/> event.
/// </summary>
/// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler h = CollectionChanged;
if (h != null)
h(this, e);
}
/// <summary>
/// Fires the collection reset event.
/// </summary>
private void FireCollectionReset()
{
NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
OnCollectionChanged(e);
}
#endregion
#region INotifyPropertyChanged
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the <see cref="E:PropertyChanged"/> event.
/// </summary>
/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler h = PropertyChanged;
if (h != null)
h(this, e);
}
/// <summary>
/// Fires the property changed event.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
private void FirePropertyChanged(string propertyName)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
OnPropertyChanged(e);
}
#endregion
#region IsLoading
private bool _isLoading;
/// <summary>
/// Gets or sets a value indicating whether the collection is loading.
/// </summary>
/// <value>
/// <c>true</c> if this collection is loading; otherwise, <c>false</c>.
/// </value>
public bool IsLoading
{
get
{
return _isLoading;
}
set
{
if ( value != _isLoading )
{
_isLoading = value;
}
FirePropertyChanged("IsLoading");
}
}
#endregion
#region Load overrides
/// <summary>
/// Asynchronously loads the count of items.
/// </summary>
protected override void LoadCount()
{
Count = 0;
IsLoading = true;
ThreadPool.QueueUserWorkItem(LoadCountWork);
}
/// <summary>
/// Performed on background thread.
/// </summary>
/// <param name="args">None required.</param>
private void LoadCountWork(object args)
{
int count = FetchCount();
SynchronizationContext.Send(LoadCountCompleted, count);
}
/// <summary>
/// Performed on UI-thread after LoadCountWork.
/// </summary>
/// <param name="args">Number of items returned.</param>
private void LoadCountCompleted(object args)
{
Count = (int)args;
IsLoading = false;
FireCollectionReset();
}
/// <summary>
/// Asynchronously loads the page.
/// </summary>
/// <param name="index">The index.</param>
protected override void LoadPage(int index)
{
IsLoading = true;
ThreadPool.QueueUserWorkItem(LoadPageWork, index);
}
/// <summary>
/// Performed on background thread.
/// </summary>
/// <param name="args">Index of the page to load.</param>
private void LoadPageWork(object args)
{
int pageIndex = (int)args;
IList<T> page = FetchPage(pageIndex);
SynchronizationContext.Send(LoadPageCompleted, new object[]{ pageIndex, page });
}
/// <summary>
/// Performed on UI-thread after LoadPageWork.
/// </summary>
/// <param name="args">object[] { int pageIndex, IList(T) page }</param>
private void LoadPageCompleted(object args)
{
int pageIndex = (int)((object[]) args)[0];
IList<T> page = (IList<T>)((object[])args)[1];
PopulatePage(pageIndex, page);
IsLoading = false;
FireCollectionReset();
}
#endregion
}
}