0

我希望它figSpeed总是反映pSpeed,但是当我在onChangeX方法中绑定时,我总是得到一个System.NullReferenceException

任何人都可以帮助我吗?似乎参考是正确的,情况也是如此。

PointsToPathConverter班级:

[ValueConversion(typeof(List<Point>), typeof(Geometry))]
public class PointsToPathConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<Point> points = (List<Point>)value;

        if (points.Count > 0)
        {
            Point start = points[0];
            List<LineSegment> segments = new List<LineSegment>();

            for (int i = 1; i < points.Count; i++)
            {
                segments.Add(new LineSegment(points[i], true));
            }

            PathFigure figure = new PathFigure(start, segments, false); // true if closed
            PathGeometry geometry = new PathGeometry();
            geometry.Figures.Add(figure);
            return geometry;
        }
        else
        {
            return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
    #endregion
}

dataProjectorVM班级:

public class dataProjectorVM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Path figSpeed;
    public List<Point> pSpeed;

    public dataProjectorVM()
    {
        pSpeed = new List<Point>();
        pSpeed.Add(new Point(0, 0));
        Binding bind;

        bind = new Binding("pSpeed")
        {
            Source = this,
            Mode = BindingMode.OneWay,
            Converter = new PointsToPathConverter(),
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        };

        figSpeed = new Path()
        {
            Stroke = Brushes.Black,
            StrokeThickness = 1
        };

        figSpeed.SetBinding(Path.DataProperty, bind);
    }

    public void onChangeX()
    {
        pSpeed.Clear();
        double pm = -2;

        foreach (dataPacket dp in appMain.dataMgr.retrive.result)
        {
            double _pm = appMain.dataMgr.projector.getX(dp.pm);

            if (_pm > pm + 1)
            {
                pm = _pm;
                pSpeed.Add(new Point(pm, appMain.dataMgr.projector.getSpeedY(dp.speed)));
            }
        }

        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("pSpeed"));
    }
}
4

1 回答 1

0

如果PropertyChanged事件没有任何处理程序,this.PropertyChanged则为空。

你需要检查一下。

于 2013-02-04T15:54:11.760 回答