Tôi là một newbie lambda, vì vậy nếu tôi thiếu thông tin quan trọng trong mô tả của tôi, vui lòng cho tôi biết. Tôi sẽ giữ ví dụ đơn giản nhất có thể.. Biểu thức lambda net-- thông số này đến từ đâu?
Tôi sẽ xem xét mã của người khác và họ có một lớp kế thừa từ một mã khác. Đây là lớp có nguồn gốc đầu tiên, cùng với các biểu thức lambda tôi đang gặp rắc rối hiểu biết:
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..
Lưu ý các cuộc gọi đến NotifyPropertyChanged(p => QuantitySaved)
chút ở trên. Tôi không hiểu "p" xuất phát từ đâu.
Đây là lớp cơ sở:
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);
}
}
Có rất nhiều trong đó đó không phải Gecman cho câu hỏi Tôi chắc chắn, nhưng tôi muốn phạm sai lầm ở mặt bên của tính toàn diện.
Vấn đề là, tôi không hiểu thông số 'p' đến từ đâu, và cách trình biên dịch biết (rõ ràng?) Điền vào một giá trị kiểu của ViewModelBase từ không khí mỏng?
Để giải trí, tôi đã đổi mã từ 'p' thành 'this', vì SampleViewModel kế thừa từ ViewModelBase, nhưng tôi đã gặp phải một loạt lỗi trình biên dịch, cái đầu tiên trong số đó đã nêu là Invalid expression term '=>'
. nghĩ rằng sẽ làm việc.
Có ai có thể giải thích những gì đang xảy ra ở đây không?
Ah, ok. Vì vậy, nó là một tuyên bố phương pháp. Tôi nghĩ đoạn trích được đề cập là - vào thời điểm đó-- gọi NotifyPropertyChanged() bằng 'p' làm tham số. Cảm ơn tất cả mọi người, đôi mắt của tôi phải mệt mỏi. – larryq
Không, bạn không thể sử dụng 'this' vì đó là từ khóa dành riêng. Nó không bao giờ hợp lệ như tên tham số. – jason
@larryq: Nó đang gọi NotifyPropertyChanged với một cây * biểu thức * có tham số * được gọi là p. Không có tách p từ lambda hơn bất kỳ bạn có thể tách các tuyên bố chính thức tham số 'tài sản' từ NotifyPropertyChanged. –