SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

0
0

Unless I put in a large delay on construction of my base class, I get an exception “Object reference not set to an instance of an object” on calling

_sciChartEventAggregator = numericAxis.Services.GetService();

Please could you tell me what is going on? I can send you an example in my product, but as this is confidential, I cannot post the example on the forum. If the example will help please tell me how I can send it to you

Thanks

  • You must to post comments
0
0

Hi there,

AxisBase.Services property is initialized when an axis is attached to SciChartSurface instance. So please check if you access it after that. Also be aware, that this call is equal to SciChartSurface.Services.GetService(), as axes share same Services instance as their parent surface.

Please, let us know if this info helps you to solve the issue.

Best regards,
Yuriy

  • wilx
    I think mine is fine, but I think you should have a look. I have attached my project here ( password protected). I will email the password via the support form on your website.If you run our program, just ignore all the comms errors on startup, as obviously you are not connected to our hardware, everything should run fine as I have put a time delay in constructor for AnalogueMonitorViewModelIf you change the code marked by my initials mjw to either call AddYAxis directly ( which contains the call to _sciChartEventAggregator = numericAxis.Services.GetService();) or shorten the delay on the timer to call AddYAxis, you will hit the problemThanks
  • You must to post comments
0
0

Hi there,

We’ve investigated the issue. Exception is thrown because your axes collection haven’t been attached to the surface yet. You can easily prevent it by this check:

if(numericAxis.ParentSurface != null)
{
   _sciChartEventAggregator = numericAxis.Services.GetService();
}

How to know when you can get it? Well, you could consider listening to AxisBase.PropertyChanged. Something like that:

        numericAxis.PropertyChanged = OnAxisPropertyChanged;
....

        private void OnAxisPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var numericAxis = sender as IAxis;
            if(e.PropertyName=="ParentSurface")
            {
               if(numericAxis.ParentSurface != null)
               {
                  _sciChartEventAggregator = numericAxis.Services.GetService();
               }
            }
        }

Please, don’t hesitate to ask if you have any questions,

Best regards,
Yuriy

  • wilx
    Thanks for the help, but I am finding it hard to understand where you could put this code to hold off until the collection is added to the surface. Using my code as the example, could you get it working and show me where you would put the code to hold off, thanks.
  • Yuriy
    Hi there,We looked over your app, and found out that only usage of Services instance is to perform ZoomExtents on the chart. There are other ways to do this, you could consider using any of them: - call dataSeries.InvalidateParentSurface(RangeMode.ZoomToFit) or RangeMode.ZoomToFitY - pass the chart's ZoomExtents command to the ViewModel through binding and call Execute() wherever you need in the VM - inject the SciChartSurface as IInvalidatable to the ViewModel (you could take a look at this MiVVM pattern article)And regarding your question, you should add this subscription numericAxis.PropertyChanged = OnAxisPropertyChanged; after axis creation (or where you add an axis to the Y axes collection).Hope this helps!Best regards, Yuriy
  • wilx
    So are you suggesting I should replace
    _sciChartEventAggregator.Publish(new ZoomExtentsMessage(this));
    
    with one of– call dataSeries.InvalidateParentSurface(RangeMode.ZoomToFit) or RangeMode.ZoomToFitY – pass the chart’s ZoomExtents command to the ViewModel through binding and call Execute() wherever you need in the VM – inject the SciChartSurface as IInvalidatable to the ViewModel (you could take a look at this MiVVM pattern article)and remove the _sciChartEventAggregator?I tried usingdataSeries.InvalidateParentSurface(RangeMode.ZoomToFit); but getError 33 'Abt.Controls.SciChart.XyDataSeries' does not contain a definition for 'InvalidateParentSurface' and no extension method 'InvalidateParentSurface' accepting a first argument of type 'Abt.Controls.SciChart.XyDataSeries' could be found (are you missing a using directive or an assembly reference?) C:\QNX630\CSharp\AVRHMI\src\AVR_HMI\ViewModels\BaseAnalogueChartViewModel.cs 1231 24 AVR_HMI
  • Yuriy
    Hi,Sorry for a delay! I tried to look into the project, it was hard a bit for me to deal with the code, I can't get it working without the delay, it throws different exceptions and results in empty views. What I tried to do, was to replace _sciChartEventAggregator.Publish(new ZoomExtentsMessage(this)); withif (dataSeries.DataSet != null) { dataSeries.DataSet.InvalidateParentSurface(RangeMode.ZoomToFit); } , but can't test whether it works or not... Also I tried to have this command in the BaseAnalogueChartVM:public ICommand ZoomExtentsCommand { get { return _zoomExtentsCommand; } set { _zoomExtentsCommand = value; OnPropertyChanged("ZoomExtentsCommand"); } }and bind it in such a way(AnalogueMonitor.xaml):<SciChart:SciChartSurface x:Name="sciChart" SeriesSource="{Binding SeriesSource}" SciChart:ThemeManager.Theme="Oscilloscope" .... ZoomExtentsCommand="{Binding ZoomExtentsCommand, Mode=OneWayToSource}" />but this seems not to work as expected(can't realize why it returns null to the VM).And I ended up with the idea: try to replace _sciChartEventAggregator.Publish(new ZoomExtentsMessage(this)); with VPManager.InvalidateParentSurface(RangeMode.ZoomToFit);Could you please try this? It should help!Best regards, Yuriy
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.