【发布时间】:2019-03-27 13:55:53
【问题描述】:
我正在尝试在下面的代码中为用例创建代码,其中通用 ViewModel 类“捕获”其模型中的所有属性,并呈现具有相同名称和类型的属性,并且还触发 @987654321 @ 数据绑定事件。
有办法吗?我正在使用 .NET 4.6。
public class Rectangle
{
public double Width {get; set;}
public double Height {get; set;}
}
public class RectangleViewModel : MagicViewModel<Rectangle>
{
public RectangleViewModel(Rectangle model)
: base(model){ }
}
public class MagicViewModel<TModel> : INotifyPropertyChanged
{
protected readonly TModel _model;
public MagicViewModel(TModel model)
{
_model = model;
}
// inpc implementation
// what else?
}
public class Program
{
public static void Main(string[] args)
{
var vm = new RectangleViewModel(new Rectangle());
var calls = 0;
vm.PropertyChanged += (sender, args) => calls++;
vm.Height = 10; // magic happened here
Debug.Assert(calls > 0);
}
}
【问题讨论】:
标签: c# mvvm data-binding inotifypropertychanged .net-4.6