2017-08-09 151 views
0

我正在构建一个WPF数据可视化工具,其中的齿轮版本为LiveCharts。 我有一个名为SectionsCollection的SectionsCollection对象,我需要在数据更改时重新加载。在重新分配到SectionsCollection之前,我运行以下代码段。为WPF清除截图LiveCharts中的集合

 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类型也是如此。

回答

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();