我有一组方法允许用户轻松使用 PropertHasChanged 事件,然后允许进行一些额外的处理。这是方法:
public virtual void SetPropertyValue<T>(ref T currentValue, T newValue, Action<T> extraFunction = null, Action voidAfterSetAction = null) where T : class
{
if (currentValue == newValue) return;
currentValue = newValue;
PropertyHasChanged();
if (extraFunction != null) extraFunction(newValue);
if (voidAfterSetAction != null) voidAfterSetAction();
}
对我来说很明显,有时我需要在 extraFunction 操作中使用旧值。这就是我打算这样做的方式:
public virtual void SetPropertyValue<T>(ref T currentValue, T newValue, Action<T, T> extraFunction = null, Action voidAfterSetAction = null) where T : class
{
var oldVal = currentValue;
if (currentValue == newValue) return;
currentValue = newValue;
PropertyHasChanged();
if (extraFunction != null) extraFunction(oldVal, newValue);
if (voidAfterSetAction != null) voidAfterSetAction();
}
您可能会注意到,extraFunction 操作现在需要两个参数。VS 在我创建该方法时没有问题(没有红色 qwigglies),但是当我构建它时会引发许多错误,表明第一种方法和第二种方法之间的用法不明确。如果是这种情况,那么我怎样才能实现我正在寻找的东西?
编辑
这是该方法的通常用法:
SetPropertyValue(ref _streetAddress1, value, null, () => SalesData.StreetAddress1 = value);