1

我是像 MVVM 这样的深度编码的新手,在MSDN Simple MVVM page 页面上,模型类是在没有 INotifyPropertyChanged 的​​情况下定义的,只有它使用的 VIEWModelBase。

主要代码如下(代码是从提到的页面复制的):

Namespace SimpleMVVM.Model

    Public Class Customer
        Public Property CustomerID() As Integer 
        Public Property FullName() As String 
        Public Property Phone() As String 
    End Class 
End Namespace

Namespace SimpleMVVM.Model
    Public Class CustomerRepository
        Private _customers As List(Of Customer)

        Public Sub New()
            _customers = New List(Of Customer) From
            {
                New Customer() With {.CustomerID = 1, .FullName = "Dana Birkby", .Phone = "394-555-0181"},
                New Customer() With {.CustomerID = 2, .FullName = "Adriana Giorgi", .Phone = "117-555-0119"},
                New Customer() With {.CustomerID = 3, .FullName = "Wei Yu", .Phone = "798-555-0118"}
            }
        End Sub 

        Public Function GetCustomers() As List(Of Customer)
            Return _customers
        End Function 

        Public Sub UpdateCustomer(SelectedCustomer As Customer)
            Dim customerToChange = _customers.Single(Function(c) c.CustomerID = SelectedCustomer.CustomerID)
            customerToChange = SelectedCustomer
        End Sub 
    End Class 
End Namespace


Imports System.ComponentModel

Namespace SimpleMVVM.ViewModel

    Public MustInherit Class ViewModelBase
        Implements INotifyPropertyChanged

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Protected Overridable Sub OnPropertyChanged(propname As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propname))
        End Sub 
    End Class 
End Namespace

Imports System.Collections.Generic
Imports SimpleMVVM.Model

Namespace SimpleMVVM.ViewModel

    Public Class CustomerViewModel
        Inherits ViewModelBase

        Private _customers As List(Of Customer)
        Private _currentCustomer As Customer
        Private _repository As CustomerRepository
        Private _updateCustomerCommand As RelayCommand

        Public Sub New()
            _repository = New CustomerRepository()
            _customers = _repository.GetCustomers()

            WireCommands()
        End Sub 

        Private Sub WireCommands()
            UpdateCustomerCommand = New RelayCommand(AddressOf UpdateCustomer)
        End Sub 

        Public Property UpdateCustomerCommand() As RelayCommand
            Get 
                Return _updateCustomerCommand
            End Get 
            Private Set(value As RelayCommand)
                _updateCustomerCommand = value
            End Set 
        End Property 

        Public Property Customers() As List(Of Customer)
            Get 
                Return _customers
            End Get 
            Set(value As List(Of Customer))
                _customers = value
            End Set 
        End Property 

        Public Property CurrentCustomer() As Customer
            Get 
                Return _currentCustomer
            End Get 
            Set(value As Customer)
                If _currentCustomer.Equals(value) Then
                    _currentCustomer = value
                    OnPropertyChanged("CurrentCustomer")
                    UpdateCustomerCommand.IsEnabled = True 
                End If 
            End Set 
        End Property 

        Public Sub UpdateCustomer()
            _repository.UpdateCustomer(CurrentCustomer)
        End Sub 
    End Class 
End Namespace
  1. 我的问题是,与在 viewmodelbase 中的 CustomerOBject 上实现的 INotifyproperty 相比,在类客户中的每个属性上实现的 INotifypropertychanged 的​​ INotifyPropertyChanged 效果是否相同。

  2. 在第二种情况下,当客户对象仅修改一个属性值时,例如 CurrentCustomer.FullName="asdasd" 是否会引发每个属性的 Inotifypropertychanged 事件?

  3. Viewmodelclass 可以像 datacontract 一样通过 WCF 发送,或者 datacontract 仅适用于没有 InotifyPropertyChanged 的​​ Modelclass

谢谢你。

4

1 回答 1

2

我将尝试回答您的问题:

  1. 在您的类/视图模型上实现 INotifyPropertyChanged。您要发送通知的所有属性都必须调用 OnPropertyChanged("YourPropertyName");

  2. 不,只要调用 OnPropertyChanged("YourPropertyName"); ,只会影响已更改的属性。

  3. 您可以发送一个带有 [DataContract] 属性的视图模型,但您确实应该对您的模型执行此操作。

于 2012-12-17T20:17:56.527 回答