silverlight - 如果支持 VM 的 props 的每个字段都实现 INotifyPropChange,VM 是否需要实现 INotifyPropChagned?

标签 silverlight silverlight-4.0 mvvm inotifypropertychanged

如果我的模型实现了 INotifyPropertyChanged 并且我的 VM 需要这样做吗?

澄清:在您拥有 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 中的东西绑定(bind)到实现 PropertyChanged 和 CollectionChanged 接口(interface)的最低级别的对象?

如果我有一个格式良好的通知模型层,我似乎无法找到我需要编写多少胶水代码的明确答案......

PS。如果这很重要,我正在使用 Prism 和 Ninject 在 SL4 中进行开发。我的模型是可变的、有状态的并且在本地内存中(我保留一个本地缓存,因为在每次操作后都访问服务器是不切实际的)。

最佳答案

不,您不需要在您的情况下实现接口(interface)本身。例如,VM 不需要实现 INotifyPropertyChange如果您使用 RIA 服务业务对象,因为那些已经实现了它。

然而!很可能您仍然希望这样做,因为“IsBusy”、“CanSave”等属性通常属于 VM 本身,然后您需要接口(interface)。

通常每个应用程序都有某种类型的 VMBase实现 INotifyPropertyChanged 的对象, INotifyDataErrorInfo等等。每个虚拟机都继承自这个基类

关于silverlight - 如果支持 VM 的 props 的每个字段都实现 INotifyPropChange,VM 是否需要实现 INotifyPropChagned?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203524/

相关文章:

silverlight - 带有 WCF 的 Silverlight 中的跨域策略错误

silverlight - 是否可以在 Silverlight 中选择性地为标签着色?

wpf - 同一程序集中 WPF 和 Silverlight 的资源

c# - 如何替换 RichTextBox 中的超链接链接(Windows 手机)

Silverlight DragDrop.DoDragDrop()

vb.net - 在 Silverlight MVVM 中重新查询 CanExecute 的体面方法?

c# - 必应 map Silverlight 控件 : How do I Display the Sun's Footprint?

c# - MVVM Light - RaisePropertyChanged 不起作用

ios - SwiftUI 中 MVVM 模式中多个切换的绑定(bind)变量数组

wpf - 如何获得 WPF 验证错误