当我插入一行到我的DataTable与视图选择似乎我的应用程序崩溃。大概这与一些交叉线程问题有关,但我不完全确定如何围绕它工作。下面是我对DataTable的定义:
public class Logging
{
public DataTable Logs;
public Logging()
{
Logs = new DataTable();
Logs.Columns.Add("Level", typeof(int));
Logs.Columns.Add("Message", typeof(string));
}
public void LogMessage(int level, string message)
{
DataRow row = Logs.NewRow();
row["Level"] = level;
row["Message"] = message;
Logs.Rows.Add(row);
}
}
这生成了我所期望的DataTable,并且工作得很好。然后将其绑定到WPF ListView控件,如下所示:
<ListView x:Name="lvConsole">
<ListView.View>
<GridView>
<GridViewColumn Header="Level" Width="50" DisplayMemberBinding="{Binding Path=Level}"/>
<GridViewColumn Header="Message" Width="376" DisplayMemberBinding="{Binding Path=Message}"/>
</GridView>
</ListView.View>
</ListView>
也可以添加到c#中:
lvConsole.ItemsSource = Util.Logger.Logs.DefaultView;
因此,为了产生问题,我只是打开包含列表视图的表单,单击它并滚动一下,然后我得到以下错误:
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):rnThis exception was thrown because the generator for control 'System.Windows.Controls.ListView Items.Count:20' with name 'lvConsole' has received sequence of CollectionChanged events that do not agree with the current state of the Items collection. The following differences were detected:rn Accumulated count 12 is different from actual count 20. [Accumulated count is (Count at last Reset + #Adds - #Removes since last Reset).]rnrnOne or more of the following sources may have raised the wrong events:rn System.Windows.Controls.ItemContainerGeneratorrn System.Windows.Controls.ItemCollectionrn System.Windows.Data.BindingListCollectionViewrn System.Data.DataViewrn(The starred sources are considered more likely to be the cause of the problem.)rnrnThe most common causes are (a) changing the collection or its Count without raising a corresponding event, and (b) raising an event with an incorrect index or item parameter.rnrnThe exception's stack trace describes how the inconsistencies were detected, not how they occurred. To get a more timely exception, set the attached property 'PresentationTraceSources.TraceLevel' on the generator to value 'High' and rerun the scenario. One way to do this is to run a command similar to the following:n System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator, System.Diagnostics.PresentationTraceLevel.High)rnfrom the Immediate window. This causes the detection logic to run after every CollectionChanged event, so it will slow down the application.rn"}
有没有人知道我需要做什么来解决这个问题?我尝试了很多方法,其中之一是:
private static object _syncLock = new object();
BindingOperations.EnableCollectionSynchronization(Logs.Rows, _syncLock);
这似乎没有做任何有用的,也许这是有关我从两个线程调用LogMessage一次?
我很确定我已经找到了一个很好的解决方案,但不确定它会持续多久。无论如何我都要分享,希望它是正确的。
private void logMessage(object parameters)
{
object[] Paras = (object[])parameters;
int level = Convert.ToInt32(Paras[0]);
string message = (string)(Paras[1]);
DataRow row = Logs.NewRow();
row["Level"] = level;
row["Message"] = message;
Logs.Rows.Add(row);
}
public void LogMessage(int level, string message)
{
object lvl = Convert.ToString(level);
object msg = message;
object[] ob = { lvl, msg };
if (Util.client.player.isRunning)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ParameterizedThreadStart(logMessage), ob);
ParameterizedThreadStart pts = new ParameterizedThreadStart(logMessage);
}
}
我必须创建一个新方法,它使用应用程序调度程序来调用编辑列表的方法,如果我是正确的,它将从应用程序主线程编辑列表(巧合的是,不是写入列表的那个,而是不断显示列表的那个)。
这是相当令人困惑的,如果有人能想到一个更好的方式来解释它,请继续:)