属性更改事件始终引用 null

本文关键字:引用 null 事件 属性 | 更新日期: 2023-09-27 17:57:24

我希望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"));
    }
}

属性更改事件始终引用 null

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

您需要检查一下。