iOS Adding an Axis to Model then Rendering Throws NullReferenceException

Oystein Bjorke 10 years ago 0
This discussion was imported from CodePlex

benhysell wrote at 2014-03-14 02:24:

See the running example code in the OxyPlot.XamarinIOS Example Browser app in the fork https://oxyplot.codeplex.com/SourceControl/network/forks/benhysell/PlotViewModelIssue

With PlotView unsealed I can now do the following:
public class GraphViewBad : PlotView
    {

        public GraphViewBad ()
        {

        }

        public void BuildGraph()
        {
            Model = new PlotModel ("Bad Plot");
            Model.TitleFontSize = 12;
            Model.TitleFont = "Helvetica";
            Model.TitleFontWeight = FontWeights.Normal;
            Model.TitlePadding = 0;
            Model.Padding = new OxyPlot.OxyThickness (3, 0, 0, 12);
            Model.PlotAreaBackground = OxyColors.White;
            Model.PlotAreaBorderThickness = 0;
            Model.Background = OxyColors.Red;

            //once an axis is added application fails with null ref
            Model.Axes.Add (new LinearAxis (AxisPosition.Left) {
                TickStyle = TickStyle.Outside,
                AxislineStyle = LineStyle.Solid,
                MajorStep = 1, 
                Minimum = 0,
                Maximum = 10
            });
        }
    }
Nothing fancy, a red background with a y-axis...enough to show the error.

When I run this code I get a System.NullReferenceException in PlotElement.cs
/// <summary>
        /// Gets the actual font.
        /// </summary>
        protected internal string ActualFont
        {
            get
            {
                return this.Font ?? this.PlotModel.DefaultFont;
            }
        }
If I change my code to the following the view displays as expected:
public class GraphViewGood : PlotView
    {
        PlotModel model;

        public GraphViewGood ()
        {

        }

        public void BuildGraph()
        {
            model = new PlotModel ("Good Plot");
            model.TitleFontSize = 12;
            model.TitleFont = "Helvetica";
            model.TitleFontWeight = FontWeights.Normal;
            model.TitlePadding = 0;
            model.Padding = new OxyPlot.OxyThickness (3, 0, 0, 12);
            model.PlotAreaBackground = OxyColors.White;
            model.PlotAreaBorderThickness = 0;
            model.Background = OxyColors.Red;

            model.Axes.Add (new LinearAxis (AxisPosition.Left) {
                TickStyle = TickStyle.Outside,
                AxislineStyle = LineStyle.Solid,
                MajorStep = 1, 
                Minimum = 0,
                Maximum = 10
            });
            Model = model;
        }
    }