2013-04-25 114 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