【问题标题】:Catel (MVVM framework) ObservableCollectionCatel(MVVM 框架) ObservableCollection
【发布时间】: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


    【解决方案1】:

    您可以考虑在 Catel 中使用FastObservableCollection

    using (fastCollection.SuspendChangeNotifications())
    {
        // TODO: Add and remove all your items here
    }
    

    一旦你退出使用,它就会通过它的更改通知

    要解决线程问题,您可以使用 DispatcherHelper。

    【讨论】:

    • 感谢您的快速回复。这个解决方案给了我一个新问题。我已经在问题的末尾解释了新问题。谢谢你的时间。
    • 更新时列表有多大?您是否尝试过 ContextIdle 以外的其他值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2011-07-04
    相关资源
    最近更新 更多