2015-02-09 26 views
0

我使用WPF Toolkit绘制图形并绑定数据以显示我使用observable集合的图并将数据点添加到集合并将此集合作为itemsource为情节。将批量数据添加到Observable Collection会降低应用程序的性能

我的问题是当我向我的收藏中添加很多数据点时,或者从收藏中重新获取数据点并尝试显示它正在花费时间显示的情节。

我能为这个问题做些什么?

请给我建议,以便提高性能。

在此先感谢。

+0

大约有多少数据点导致该问题?另外检查是否[this SO post](http://stackoverflow.com/questions/11958375/how-to-add-thousands-of-items-to-a-- bindded-collection-without-locking-gui)或[this ](http://updatecontrols.net/doc/tips/common_mistakes_observablecollection)或[this](http://tliangnet.blogspot.in/2013/04/observablecollection-performance-issue.html)可以帮助你。 – 2015-02-09 12:49:13

+0

我有大约16000点左右(我必须从csv文件中读取)。我可以阅读所有内容,但只绘制性能问题。 – Balerina 2015-02-11 05:28:05

回答

0

ObservableCollection通知每一个变化,这意味着很多事件,因此它影响性能。您可以使用List而不是ObservableCollection,但大多数人不推荐它。

public class MainVm:INotifyPropertyChanged 
{ 
    List<Point> _points; 
    public List<Point> Points 
    { 
     get { return _points; } 
     set { _points = value; Notify("Points"); } 
    } 
    public void Notify(string name) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

在这种情况下,您必须手动通知对列表所做更改的视图。例如在任何地方比你的视图模型的构造等,或查看你必须这样做:

Points.Add(new Point()); 
Points.Add(new Point()); 
Points.Add(new Point()); 
//View is not notified yet 

var tmp = Points; 
Points = null;//View is notified and cleared 
Points = tmp;//View is notified and updated 
0

我用的ObservableCollection的子类从this后添加一个批处理更新,并允许您添加一个.AddRange直到最后才批量触发更新通知。

为了保持完整性,我已经复制了原始帖子中的代码,但是如果您发现它很有用,请加快链接的帖子。

Imports System.Collections.Specialized 
Imports System.ComponentModel 
Imports System.Collections.ObjectModel 

Public Class ObservableRangeCollection(Of T) : Inherits ObservableCollection(Of T) : Implements INotifyCollectionChanging(Of T) 
    ''' <summary> 
    ''' Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. 
    ''' </summary> 
    ''' <remarks></remarks> 
    Public Sub New() 
     MyBase.New() 
    End Sub 

    ''' <summary> 
    ''' Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. 
    ''' </summary> 
    ''' <param name="collection">collection: The collection from which the elements are copied.</param> 
    ''' <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> 
    Public Sub New(ByVal collection As IEnumerable(Of T)) 
     MyBase.New(collection) 
    End Sub 

    ''' <summary> 
    ''' Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
    ''' </summary> 
    Public Sub AddRange(ByVal collection As IEnumerable(Of T)) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Add, collection) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     Dim index = Items.Count - 1 
     For Each i In collection 
      Items.Add(i) 
     Next 

     OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection, index)) 
    End Sub 


    ''' <summary> 
    ''' Inserts the collection at specified index. 
    ''' </summary> 
    Public Sub InsertRange(ByVal index As Integer, ByVal Collection As IEnumerable(Of T)) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Add, Collection) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     For Each i In Collection 
      Items.Insert(index, i) 
     Next 

     OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)) 
    End Sub 


    ''' <summary> 
    ''' Removes the first occurence of each item in the specified collection from ObservableCollection(Of T). 
    ''' </summary> 
    Public Sub RemoveRange(ByVal collection As IEnumerable(Of T)) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Remove, collection) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     For Each i In collection 
      Items.Remove(i) 
     Next 

     OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)) 
    End Sub 



    ''' <summary> 
    ''' Clears the current collection and replaces it with the specified item. 
    ''' </summary> 
    Public Sub Replace(ByVal item As T) 
     ReplaceRange(New T() {item}) 
    End Sub 

    ''' <summary> 
    ''' Clears the current collection and replaces it with the specified collection. 
    ''' </summary> 
    Public Sub ReplaceRange(ByVal collection As IEnumerable(Of T)) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Replace, Items) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     Items.Clear() 
     For Each i In collection 
      Items.Add(i) 
     Next 
     OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)) 
    End Sub 

    Protected Overrides Sub ClearItems() 
     Dim e As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Reset, Items) 
     OnCollectionChanging(e) 

     If e.Cancel Then Exit Sub 

     MyBase.ClearItems() 
    End Sub 

    Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As T) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Add, item) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     MyBase.InsertItem(index, item) 
    End Sub 

    Protected Overrides Sub MoveItem(ByVal oldIndex As Integer, ByVal newIndex As Integer) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)() 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     MyBase.MoveItem(oldIndex, newIndex) 
    End Sub 

    Protected Overrides Sub RemoveItem(ByVal index As Integer) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Remove, Items(index)) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     MyBase.RemoveItem(index) 
    End Sub 

    Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As T) 
     Dim ce As New NotifyCollectionChangingEventArgs(Of T)(NotifyCollectionChangedAction.Replace, Items(index)) 
     OnCollectionChanging(ce) 
     If ce.Cancel Then Exit Sub 

     MyBase.SetItem(index, item) 
    End Sub 

    Protected Overrides Sub OnCollectionChanged(ByVal e As Specialized.NotifyCollectionChangedEventArgs) 
     If e.NewItems IsNot Nothing Then 
      For Each i As T In e.NewItems 
       If TypeOf i Is INotifyPropertyChanged Then AddHandler DirectCast(i, INotifyPropertyChanged).PropertyChanged, AddressOf Item_PropertyChanged 
      Next 
     End If 
     MyBase.OnCollectionChanged(e) 
    End Sub 

    Private Sub Item_PropertyChanged(ByVal sender As T, ByVal e As ComponentModel.PropertyChangedEventArgs) 
     OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, sender, IndexOf(sender))) 
    End Sub 

    Public Event CollectionChanging(ByVal sender As Object, ByVal e As NotifyCollectionChangingEventArgs(Of T)) Implements INotifyCollectionChanging(Of T).CollectionChanging 
    Protected Overridable Sub OnCollectionChanging(ByVal e As NotifyCollectionChangingEventArgs(Of T)) 
     RaiseEvent CollectionChanging(Me, e) 
    End Sub 
End Class 


Public Interface INotifyCollectionChanging(Of T) 
    Event CollectionChanging(ByVal sender As Object, ByVal e As NotifyCollectionChangingEventArgs(Of T)) 
End Interface 

Public Class NotifyCollectionChangingEventArgs(Of T) : Inherits CancelEventArgs 

    Public Sub New() 
     m_Action = NotifyCollectionChangedAction.Move 
     m_Items = New T() {} 
    End Sub 

    Public Sub New(ByVal action As NotifyCollectionChangedAction, ByVal item As T) 
     m_Action = action 
     m_Items = New T() {item} 
    End Sub 

    Public Sub New(ByVal action As NotifyCollectionChangedAction, ByVal items As IEnumerable(Of T)) 
     m_Action = action 
     m_Items = items 
    End Sub 

    Private m_Action As NotifyCollectionChangedAction 
    Public ReadOnly Property Action() As NotifyCollectionChangedAction 
     Get 
      Return m_Action 
     End Get 
    End Property 

    Private m_Items As IList 
    Public ReadOnly Property Items() As IEnumerable(Of T) 
     Get 
      Return m_Items 
     End Get 
    End Property 
End Class 
+0

我实现了上面建议的方式,即从Observable集合中获取一个类,并使用AddRange函数添加数据。但我仍然没有发现性能上的重大差异。我正在使用WPF工具包进行绘图。这对性能是否重要?如此,请给我建议改善表现的方法。提前致谢 – Balerina 2015-03-02 14:01:43

相关问题