如果我的模型實現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