2013-04-25 132 views
1

我使用Catel来实现WPF应用程序。Catel(MVVM框架)ObservableCollection

我有一个课程,从ObservableCollection延伸,每次插入一个项目UI必须更新。

CODE(简体版):

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框架中是否有任何解决这类问题的方法。

更多信息:

此应用程序的性能是主要的问题,因为我增加了许多项目以集合。

UPDATE:

使用解决问题,但是用户界面,只要我出去使用的冻结时间约5-7秒。 我的猜测是,这是由Dispatcher

引起我已经手动撤消了OnCollectionChanged

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { 
    DispatcherHelper.CurrentDispatcher.BeginInvoke(new Action(() => base.OnCollectionChanged(e)), 
       DispatcherPriority.ContextIdle); 
} 

这不是一个很好的解决方案。有没有更好的方法来避免这个问题?

回答

1

你可能会考虑使用FastObservableCollection在Catel:

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

只要你走出使用的,它会通过它的更改通知

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

+0

Thx为快速回复。这个解决方案给了我一个新问题。问题最后我解释了新问题。感谢您的时间。 – RayOldProf 2013-04-26 10:11:07

+0

更新时列表有多大?您是否尝试过其他值而不是ContextIdle? – 2013-04-26 14:17:53