如果我的模型实现INotifyPropertyChanged并且我的虚拟机需要?如果支持VM的道具的每个字段实现INotifyPropChange,虚拟机是否需要实现INotifyPropChagned?
说明:在实际情况下,您有SomeOtherProp然后INotifyPropertyChanged绝对需要实施。我真正追求的是我需要做多少工作(复制)以形成良好的模型。
例子:
namespace Question
{
public interface IFoo : INotifyPropertyChanged { }
public interface IBar : INotifyPropertyChanged { }
public interface IModel : INotifyPropertyChanged
{
IFoo Foo { get; set; }
ObservableCollection<IBar> BarCollection { get; }
}
public class VM : TypeSafeViewModelBase
//Clarification: added VM base clase with typesafe override for RaisePropertyChanged
{
private IModel _model;
public VM(IModel model)
{
this._model = model;
//Clarification: added this call...
this._model.PropertyChanged += (sender, args) => base.RaisePropertyChanged(args.PropertyName);
//That is the one I have questions about and ultimateley what I want to avoid
}
public IFoo Foo { get { return this._model.Foo; } }
public ObservableCollection<IBar> BarCollection { get { return this._model.BarCollection; } }
//clarification: added this prop declaration
//I know this would be needed as this property is backed by a private member of this class
private string _someOtherProp;
public string SomeOtherProp
{
get { return this._someOtherProp; }
set
{
this._someOtherProp = value;
base.RaisePropertyChanged(() => this.SomeOtherProp);
}
}
}
}
确实VM需要执行INotifyPropertyChanged?并把所有的事件转交给V?或者将V中的事物绑定到实现PropertyChanged和CollectionChanged接口的最低级别的对象上?
我似乎无法找到我多少胶水代码需要写,如果我有一个良好的,通知模型层一个明确的答案......
PS。如果有问题,我正在使用Prism和Ninject开发SL4。我的模型是可变的,有状态的,并且在本地内存中(我保留了一个本地缓存,因为在每次操作都不可行之后点击服务器)。
我认为你在ViewModel中没有任何视图状态,并且你的模型使用INotifyXXX机制,那么你不需要'ViewModel',因为它根本不是一个真正的ViewModel。 –
@RitchMelton,同意,我的例子太简单了。看到澄清的例子。 VM现在具有状态(例如,认为IsVisible,InitialPosition或类似的东西)。我的生产虚拟机跟踪V状态,并保证中间层。 – dFlat