Caliburn NotifyOfPropertyChange:System.InvalidCastException
Caliburn NotifyOfPropertyChanged : System.InvalidCastException
我们在我们的应用程序中使用了 caliburn 框架。
我的视图模型中有一个 属性,当它的值发生变化时,它应该为另一个 属性 调用一个 NotifyOfPropertyChanged
。
我认为我可以按如下方式做到这一点:
public AnalogSensorState State
{
get
{
if (LowerErrorLevelExceeded)
{
return AnalogSensorState.LowerErrorExceeded;
}
if (LowerWarningLevelExceeded)
{
return AnalogSensorState.LowerWarningExceeded;
}
if (UpperErrorLevelExceeded)
{
return AnalogSensorState.UpperErrorExceeded;
}
if (UpperWarningLevelExceeded)
{
return AnalogSensorState.UpperWarningExceeded;
}
return AnalogSensorState.Ok;
}
}
public bool LowerErrorLevelExceeded
{
get => _lowerErrorLevelExceeded;
set
{
Set(ref _lowerErrorLevelExceeded, value, nameof(LowerErrorLevelExceeded));
NotifyOfPropertyChange(() => nameof(State));
}
}
...
当执行布尔值 setter 时,我在 NotifyOfPropertyChange
调用中得到一个无效的转换异常。
为什么?
异常:
System.InvalidCastException: 'Unable to cast object of type
'System.Linq.Expressions.ConstantExpression' to type
'System.Linq.Expressions.MemberExpression'.'
根据 Caliburn.Micro
中的 PropertyChangedBase
class,您应该使用 NotifyOfPropertyChange(nameof(State));
或 NotifyOfPropertyChange(() => State);
。它接受名称为 属性 或名称为 Expression<Func<TProperty>>
的字符串 属性
/// <summary>Notifies subscribers of the property change.</summary>
/// <param name="propertyName">Name of the property.</param>
public virtual void NotifyOfPropertyChange([CallerMemberName] string propertyName = null)
{
if (!this.IsNotifying || this.PropertyChanged == null)
return;
this.OnUIThread((Action) (() => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName))));
}
public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
{
this.NotifyOfPropertyChange(property.GetMemberInfo().Name);
}
我们在我们的应用程序中使用了 caliburn 框架。
我的视图模型中有一个 属性,当它的值发生变化时,它应该为另一个 属性 调用一个 NotifyOfPropertyChanged
。
我认为我可以按如下方式做到这一点:
public AnalogSensorState State
{
get
{
if (LowerErrorLevelExceeded)
{
return AnalogSensorState.LowerErrorExceeded;
}
if (LowerWarningLevelExceeded)
{
return AnalogSensorState.LowerWarningExceeded;
}
if (UpperErrorLevelExceeded)
{
return AnalogSensorState.UpperErrorExceeded;
}
if (UpperWarningLevelExceeded)
{
return AnalogSensorState.UpperWarningExceeded;
}
return AnalogSensorState.Ok;
}
}
public bool LowerErrorLevelExceeded
{
get => _lowerErrorLevelExceeded;
set
{
Set(ref _lowerErrorLevelExceeded, value, nameof(LowerErrorLevelExceeded));
NotifyOfPropertyChange(() => nameof(State));
}
}
...
当执行布尔值 setter 时,我在 NotifyOfPropertyChange
调用中得到一个无效的转换异常。
为什么?
异常:
System.InvalidCastException: 'Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'System.Linq.Expressions.MemberExpression'.'
根据 Caliburn.Micro
中的 PropertyChangedBase
class,您应该使用 NotifyOfPropertyChange(nameof(State));
或 NotifyOfPropertyChange(() => State);
。它接受名称为 属性 或名称为 Expression<Func<TProperty>>
的字符串 属性
/// <summary>Notifies subscribers of the property change.</summary>
/// <param name="propertyName">Name of the property.</param>
public virtual void NotifyOfPropertyChange([CallerMemberName] string propertyName = null)
{
if (!this.IsNotifying || this.PropertyChanged == null)
return;
this.OnUIThread((Action) (() => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName))));
}
public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
{
this.NotifyOfPropertyChange(property.GetMemberInfo().Name);
}