C# PropertyChanged事件始终引用null

C# PropertyChanged事件始终引用null,c#,nullreferenceexception,inotifypropertychanged,C#,Nullreferenceexception,Inotifypropertychanged,我希望figSpeed总是反映pSpeed但是当我在onChangeX方法中绑定时,我总是得到一个System.NullReferenceException 有人能帮我吗?似乎参考是正确的,情况也是如此 PointsToPathConverterclass: [ValueConversion(typeof(List<Point>), typeof(Geometry))] public class PointsToPathConverter : IValueConverter {

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

有人能帮我吗?似乎参考是正确的,情况也是如此

PointsToPathConverter
class:

[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
}
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"));
    }
}

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


您需要对此进行检查。

那么我如何分配PropertyChanged?@user2040143:否。如果没有处理程序,则无需执行任何操作。只需检查它是否为空。是的,它为空,那么我如何处理它??现在,如果我把if(PropertyChanged!=null)放进去,没有错误,但是即使我更改了pSpeed的值,路径也不会更新。我已经用observeCollection替换了list,它不起作用。好的,我已经解决了它。通过在pSpeed的declear中添加{set;get;}。无论如何,谢谢你。