0

我正在使用LiveCharts的 Geared 版本构建 WPF 数据可视化工具。我有一个名为 SectionsCollection 的对象SectionsCollection,我需要在数据更改时重新加载它。在重新分配给SectionsCollection.

        try
        {
            if (SectionsCollection != null && SectionsCollection.Count > 0)
            {
                SectionsCollection.Clear();
            }
        }
        catch(Exception e)
        {
            Status += "Error in clearing SectionsCollection.\n"+e;
        }
        SectionsCollection = new SectionsCollection();

以下错误SectionsCollection.Clear();在线路上间歇性发生,标签为NullReferenceException 发生

Exception thrown: 'System.NullReferenceException' in LiveCharts.Wpf.dll

Additional information: Object reference not set to an instance of an object.

如果我检查它SectionsCollection不为空且不为空,为什么会出现此错误?

VisualsCollection 和 SeriesCollection 类型似乎也会出现此错误。

4

1 回答 1

0

尝试添加标志以防止不需要的多个 exec。在您的过程中:

 bool collIsBusy = false;
        try
        {
            if (SectionsCollection != null && SectionsCollection.Count > 0 && !collIsBusy)
            {
                collIsBusy = true; //flag to prevent throttling multiple executions
                SectionsCollection.Clear();
                collIsBusy = false;
            }
        }
        catch (Exception e)
        {
            Status += "Error in clearing SectionsCollection.\n" + e;
        }
        SectionsCollection = new SectionsCollection();
于 2017-12-12T04:06:02.543 回答