我正在查看其他人的代码,他们有一个类继承自另一个类。这里先给出派生类的代码以及我难以理解的Lambda表达式:
class SampleViewModel : ViewModelBase
{
private ICustomerStorage storage = ModelFactory<ICustomerStorage>.Create();
public ICustomer CurrentCustomer
{
get { return (ICustomer)GetValue(CurrentCustomerProperty); }
set { SetValue(CurrentCustomerProperty, value); }
}
private int quantitySaved;
public int QuantitySaved
{
get { return quantitySaved; }
set
{
if (quantitySaved != value)
{
quantitySaved = value;
NotifyPropertyChanged(p => QuantitySaved); //where does 'p' come from?
}
}
}
public static readonly DependencyProperty CurrentCustomerProperty;
static SampleViewModel()
{
CurrentCustomerProperty = DependencyProperty.Register("CurrentCustomer", typeof(ICustomer),
typeof(SampleViewModel), new UIPropertyMetadata(ModelFactory<ICustomer>.Create()));
}
//more method definitions follow..
注意上面的NotifyPropertyChanged(p => QuantitySaved)
调用。我不明白这里的“p”是从哪里来的。
以下是基类:
public abstract class ViewModelBase : DependencyObject, INotifyPropertyChanged, IXtremeMvvmViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged<T>(Expression<Func<ViewModelBase, T>> property)
{
MvvmHelper.NotifyPropertyChanged(property, PropertyChanged);
}
}
这里有很多与问题无关的内容,但我想在考虑全面性方面犯错误。
问题是,我不明白'p'参数来自哪里,编译器又是如何知道(显然?)从空气中填充ViewModelBase类型值的?
为了好玩,我将代码从'p'更改为'this',因为SampleViewModel继承自ViewModelBase,但是我遇到了一系列编译器错误,其中第一个错误声明Invalid expression term '=>'
,这使我有点困惑,因为我认为它会起作用。
有人能解释一下这里发生了什么吗?
this
,因为它是一个保留关键字。它从来都不能作为参数名。 - jasonExpression<Func<ViewModelBase, int>>
,因为这是该方法参数的类型。 - phoog