1

我目前正在使用与50 EEG 示例类似的结构。我的部分设计是动态添加和删除 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));

                }
        }

}

函数绘图视图模型.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;
       }

    }
4

1 回答 1

0

根据 SciChart 文档,SciChartSurface 实现了 Finalizer 和 Dispose 模式,当图表从内存中卸载时,它会在子元素上调用 dispose。

该文档还显示了RenderSurface 实现了 Finalize,因此当图表不再由任何对象持有时,应自动清理资源。

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

最好的办法是检查您是否使用他们的 NuGet 提要中的最新版本的 SciChart,然后,如果问题仍然存在,请提交包含代码的错误报告以将错误重现给技术支持

于 2018-05-16T13:20:31.377 回答