2

我得到这个错误NullReferenceException was unhandled by user code。错误发生在这一行

PropertyChanged(this, new PropertyChangedEventArgs("AboveAircraft"));

我试过if( this != null)了,它仍然有错误。我该如何摆脱它?

代码如下所示:

public int AboveAircraft
{
        get { return _above; }
        set
        {
            if (SetProperty(ref _above, value, "AboveAircraft") && _updateModel)
            {
                if (Model.AltitudeBand == null)
                {
                    Model.AltitudeBand = new AltitudeBand();
                }

                if (this != null && AboveAircraft != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AboveAircraft"));
                    if (_above < _below)
                    {
                        BelowAircraft = _above;
                    }
                }

                Model.AltitudeBand .Above = new AltitudeBandLimit() { Unit = AltitudeUnit.Foot, Value = _above };
            }
        }
}
4

1 回答 1

6

您需要检查是否PropertyChanged为空:

if (PropertyChanged != null)
{
    PropertyChanged(this, new PropertyChangedEventArgs("AboveAircraft"));
}

只有在其他地方注册了至少一个处理程序时,它才会不为空。

于 2013-08-31T19:34:43.340 回答