将项添加到AsyncObservableCollection

时间:2015-06-02 13:39:10

标签: c# wpf

我有一个基本的WPF-Application.It包含绑定到Datagrid的{​​{1}}。当从不同的线程向Collection添加项目时,我在Random获得以下异常。

AsyncObservableCollection

一些代码:

注意:An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: An ItemsControl is inconsistent with its items source. "Information for developers (use Text Visualizer to read this):\r\nThis exception was thrown because the generator for control 'System.Windows.Controls.DataGrid Items.Count:3' with name 'logPanel' has received sequence of CollectionChanged events that do not agree with the current state of the Items collection. The following differences were detected:\r Accumulated count 1 is different from actual count 3. [Accumulated count is (Count at last Reset + #Adds - #Removes since last Reset).]\r \n At index 0: Generator's item 'BorgingIskMainApp.Model.LogItem' is different from actual item 'BorgingIskMainApp.Model.LogItem'. 是AsyncObservableCollection

绑定

myLogItems

初始化任务

    public MainWindow()
    {
        myLogItems = new AsyncObservableCollection<LogItem>();
        logPanel.ItemsSource = myLogItems;
    }

我的方法

    System.Windows.Application.Current.Dispatcher.BeginInvoke(
    System.Windows.Threading.DispatcherPriority.Normal,(Action)delegate()
    {
        DocumentTest cg = new DocumentTest();
        Task.Factory.StartNew(() => { cg.PerformWVSTest(myLogItems); });
    });

我的对象

    public void PerformWVSTest(ObservableCollection<LogItem> dgResults)
    {
        try
        {

            dgResults.Insert(0,lgItem.CreateNewLogItem(dgResults.Count, "Start WVS-Test", true));
         ....
        }
    }

任何人都知道可能导致此异常的原因是什么?

注意:异常可能随时发生。 public class LogItem { public LogItem() { logId = 0; logText = String.Empty; isSuccess = true; } public Int32 logId { get; set; } public String logText { get; set; } public Boolean isSuccess { get; set; } public LogItem CreateNewLogItem(Int32 pLogId, String pLogText, Boolean success) { ADF.Diagnostics.TextFileTracer.Write(pLogText); return new LogItem { logId = pLogId, logText = pLogText, isSuccess = success }; } } 发生

时没有明确的时间或指数

注2:代码Always

AsyncObservableCollection

AsyncCollection的来源:wpf-binding-to-an-asynchronous-collection

所有归功于对象的设计者!

0 个答案:

没有答案