10using System.Collections.ObjectModel;
15 internal class AsyncLoadManager : INotifyBusy
17 private IManageProperties _target;
18 private Action<IPropertyInfo> _onPropertyChanged;
20 public AsyncLoadManager(IManageProperties target, Action<IPropertyInfo> onPropertyChanged)
23 _onPropertyChanged = onPropertyChanged;
27 private object _syncRoot =
new object();
28 private readonly ObservableCollection<IAsyncLoader> _loading =
new ObservableCollection<IAsyncLoader>();
35 return _loading.Any();
39 private bool IsAlreadyLoadingProperty(IPropertyInfo property)
43 return _loading.Any(l => l.Property == property);
47 public void BeginLoad(IAsyncLoader loader)
49 if (IsAlreadyLoadingProperty(loader.Property))
return;
56 OnPropertyBusy(loader.Property.Name,
true);
59 loader.Load(LoaderComplete);
62 void LoaderComplete(IAsyncLoader loader, IDataPortalResult e)
67 _loading.Remove(loader);
73 _target.LoadProperty(loader.Property, e.Object);
74 _onPropertyChanged(loader.Property);
78 OnPropertyBusy(loader.Property.Name,
false);
82 OnUnhandledAsyncException(
this,
new AsyncLoadException(loader.Property,
string.Format(
Resources.
AsyncLoadException, loader.Property.FriendlyName), e.Error));
85 #region INotifyPropertyBusy Members
88 protected void OnPropertyBusy(
string propertyName,
bool busy)
90 if (BusyChanged !=
null)
91 BusyChanged(
this,
new BusyChangedEventArgs(propertyName, busy));
96 #region INotifyBusy Members
98 bool INotifyBusy.IsBusy
100 get {
return (
this as INotifyBusy).IsSelfBusy; }
103 bool INotifyBusy.IsSelfBusy
105 get {
return IsLoading; }
110 #region INotifyUnhandledAsyncException Members
114 private EventHandler<ErrorEventArgs> _unhandledAsyncException;
116 public event EventHandler<ErrorEventArgs> UnhandledAsyncException
118 add { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Combine(_unhandledAsyncException, value); }
119 remove { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Remove(_unhandledAsyncException, value); }
122 protected virtual void OnUnhandledAsyncException(ErrorEventArgs error)
124 if (_unhandledAsyncException !=
null)
125 _unhandledAsyncException(
this, error);
128 protected void OnUnhandledAsyncException(
object originalSender, Exception error)
130 OnUnhandledAsyncException(
new ErrorEventArgs(originalSender, error));
A strongly-typed resource class, for looking up localized strings, etc.
static string AsyncLoadException
Looks up a localized string similar to Failed on async load of property {0}.
delegate void BusyChangedEventHandler(object sender, BusyChangedEventArgs e)
Delegate for handling the BusyChanged event.