【问题标题】:PropertyChanged event always reference nullPropertyChanged 事件始终引用 null
【发布时间】:2013-02-04 15:51:41
【问题描述】:

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

【问题讨论】:

    标签: c# nullreferenceexception inotifypropertychanged


    【解决方案1】:

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

    你需要检查一下。

    【讨论】:

    • 那么如何分配 PropertyChanged?
    • @user2040143:不。如果没有处理程序,则无需执行任何操作。只需检查它是否为空。
    • 是的,它是空的,那么我该如何处理呢?现在,如果我输入 if (PropertyChanged != null) 没有错误,但即使我更改了 pSpeed 的值,路径也不会更新。
    • 我已将 list 替换为 ObservableCollection,它也不起作用。
    • 好的,我已经解决了..通过添加 {set; get;} 到 pSpeed 的清除。还是谢谢。
    猜你喜欢
    • 2015-07-07
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2010-12-07
    • 2015-08-26
    相关资源
    最近更新 更多