我的应用程序是一个基本的下载应用程序,它允许用户相互下载文件(一个非常基本的 kazaa :-))
好吧,对于每个下载,我都会显示一个进度条,我希望它根据真实的下载进度进行更新。
我有一个 observablecollection 包含一个 downloadInstance 对象,该对象包含一个进度属性。
一旦我更新了进度属性, observablecollection 更改事件可能不会被触发,并且进度条保持没有任何视觉进度。
这是我的threadsaveobservablecollection类
public class ThreadSafeObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler CollectionChanged = this.CollectionChanged;
if (CollectionChanged != null)
foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetInvocationList())
{
DispatcherObject dispObj = nh.Target as DispatcherObject;
if (dispObj != null)
{
Dispatcher dispatcher = dispObj.Dispatcher;
if (dispatcher != null && !dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(
(Action)(() => nh.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
DispatcherPriority.DataBind);
continue;
}
}
nh.Invoke(this, e);
}
}
}
这是初始化过程
uploadActiveInstances = new ThreadSafeObservableCollection<instance>();
instance newInstance = new instance() { File = file, User = user };
uploadActiveInstances.Add(newInstance);
最后这是我的实例类
public class instance
{
public FileShareUser User { get; set; }
public SharedFile File { get; set; }
public int Progress { get; set; }
}
一旦实例的属性发生更改(progress++),我如何引发更改事件?