【发布时间】:2013-04-25 11:59:19
【问题描述】:
我正在使用Catel 来实现 WPF 应用程序。
我有一个从ObservableCollection 扩展的类,每次插入一个项目时都必须更新 UI。
代码(简化版):
public abstract class LogCollections : ObservableCollection<Log4NetLog> {
private readonly Object _locker;
protected LogCollections() {
_logChart = new LoggingLevelChart();
_locker = new object();
}
public object Locker {
get { return _locker; }
protected override void InsertItem(int index, Log4NetLog item) {
lock (_locker) {
base.InsertItem(index, item);
if (item == null) {
return;
}
Log4NetLog temp = item as Log4NetLog;
// Updating
if (temp != null) {
// Updating
}
} //UnLock
}
}
}
到目前为止,我一直在使用 BindingOperations.EnableCollectionSynchronization,它仅在 .NET 4.5 中可用。不幸的是,我必须使用 .Net 4 编译代码。
我想知道,Catel框架中是否有解决此类问题的东西。
更多信息:
对于这个应用程序性能是主要问题,因为我向集合中添加了许多项目。
更新:
使用FastObservableCollection 可以解决问题,但是一旦我停止使用,UI 就会冻结大约 5-7 秒。
我的猜测是,这是由Dispatcher
我已经手动覆盖了OnCollectionChanged:
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
DispatcherHelper.CurrentDispatcher.BeginInvoke(new Action(() => base.OnCollectionChanged(e)),
DispatcherPriority.ContextIdle);
}
这不是一个好的解决方案。有没有更好的方法来避免这个问题?
【问题讨论】:
标签: c# wpf mvvm observablecollection catel