正确处理SciChartSurface

时间:2018-05-15 17:39:22

标签: c# scichart

我目前使用与50 EEG example类似的结构。我的部分设计是动态添加和删除10-50个功能图,因此有效管理我的记忆非常重要。当我在VS2015中运行内存分析器时,我发现每次我尝试从集合中的函数图时,SciChart.Charting.Visuals.Axes.LabelProviders.NumericTickLabel仍然在内存中徘徊。处理NumericTickLabel或删除scichartsurface的正确方法是什么?

XAML.CS

    <ResourceDictionary>
       <DataTemplate x:Key="FunctionItemTemplate">
       <Grid>
           <s:SciChartSurface Name=FunctionSurface
                              RenderableSeries="{Binding RenderableSeriesCollections}"/>

       </Grid>
    </ResourceDictionary>

    <ListView Name="FunctionPlots
          ItemTemplate="{StaticResource FunctionItemTemplate}"
          ItemsSource="{Binding FunctionsCollection}" />

MainWindowViewModel.cs

public class MainWindowViewModel : INotifyProperty
{
       private ObservableCollection<FunctionPlotViewModel> _functionsCollection;

       public MainWindowViewModel()
       {
           FunctionsCollection = new ObservableCollection<FunctionPlotViewModel>();
       }

       public ObservableCollection<FunctionPlotViewModel> FunctionsCollection
       {
             get { return _functionsCollection;}
             set 
             {
                   _functionsCollection = value;
                   OnPropertyChanged("FunctionsCollection");     
             }  
       }

       public void RemoveLastFunctionPlot()
       {
              int lastIndex = FunctionCollections.Count - 1;
              FunctionCollections.Remove(lastIndex);
              //Doesn't Collect
              GC.Collect();
              GC.WaitForPendingFinalizer();
              GC.Collect();
       }

       public event PropertyChangedEventHanlder PropertyChanged;
       protected void OnPropertyChanged(string name)
       {
                PropertyChangedEventHanlder handler = PropertyChanged;

                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));

                }
        }

}

FunctionPlotViewModel.cs

 public class FunctionPlotViewModel: INotifyProperty
    {
       private ObservableCollection<IRenderableSeries> _renderableSeriesCollections;

       public FunctionPlotViewModel()
       {
           RenderableSeriesCollection = new ObservableCollection<IRenderableSeries>();
       }

       public ObservableCollection<IRenderableSeries> RenderableSeriesCollections
       {
             get { return _renderableSeriesCollections;}
             set 
             {
                   _renderableSeriesCollections= value;
                   OnPropertyChanged("RenderableSeriesCollections");     
             }  
       }

       public  void SetUpCustomAxis(double TimeOfDayInitial, IAxis AxisToBindTo)
       {
             var defaultSurface = RenderableSeriesCollections.FirstOrDefault().DataSeries.ParentSurface();
              if(defaultSurface == null)
              {
                  return;
              }
              var newAxis = new NumericAxis();
              var customNumericLabel = new CustomNumericLabelProvider();
              defaultSurface.XAxis = newAxis;
              Binding visibleRangeBinding = new Binding("VisibleRange");
              visibleRangeBinding.Source = AxisToBindTo;
              visibleRangeBinding.Mode = BindingMode.TwoWay;
              ((NumericAxis)defaultSurface.XAxis).SetBinding(NumericAxis.VisibleRangeProperty, visibleRangeBinding);

       }

       public void CleanUp()
       {
           var defaultSurface = RenderableSeriesCollections.FirstOrDefault().DataSeries.ParentSurface();
              if(defaultSurface == null)
              {
                  return;
              }
           foreach(var renderSeries in RenderableSeriesCollections)
           {
                renderSeries.DataSeries.Clear();
           }
           RenderableSeriesCollections.Clear();
           var token  = defaultSurface.SuspendUpdates();
           token.Dispose();

           //More things to do??
       }

       public event PropertyChangedEventHanlder PropertyChanged;
       protected void OnPropertyChanged(string name)
       {
                PropertyChangedEventHanlder handler = PropertyChanged;

                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));

                }
        }

}

CustomNumericLabelProvider.cs

     public class CustomNumericLabelProvider : NumericProvider
     {
       public override void OnBeginAxisDraw()
       {
       }

       public override string FormatLabel(IComparable dataValue)
       {
            if (dataValue == null)
                return String.Emtpy;
            return ConvertToTimeOfDay(dataValue);

       }

       public string ConvertToTimeOfDay(IComparable value)
       {
             //commented out for brevity
             return value;
       }

       public override string FormatCursorLabel(IComparable dataValue)
       {
            //commented out for brevity
            return dataValue;
       }

    }

1 个答案:

答案 0 :(得分:0)

根据SciChart文档SciChartSurface implements the Finalizer and Dispose patterns,当从内存中卸载图表时,会调用子元素上的dispose。

文档还显示RenderSurface implements Finalize,因此当任何对象不再保留图表时,应自动清理资源。

如果您遇到任何不同的事情,那可能就是一个错误。

最好的办法是检查您是否使用latest version of SciChart from their NuGet feed,之后,如果问题仍然存在,请提交包含代码的错误报告,以便将错误重现到tech support