0

Let's take the following case

Public MustInherit Class AnexaClass
    Inherits ObjectBase
    Private _proprietar As New ProprietarClass
    Public Property proprietar As ProprietarClass
        Get
            Return _proprietar
        End Get
        Set(value As ProprietarClass)
            _proprietar = value
            OnPropertyChanged("proprietar")
        End Set
    End Property
End Class

Public Class Anexa3Class
    Inherits AnexaClass

    Private _Proprietari As New ObservableCollection(Of ProprietarClass)
    Public Property Proprietari As ObservableCollection(Of ProprietarClass)
        Get
            Return _Proprietari
        End Get
        Set(value As ObservableCollection(Of ProprietarClass))
            _Proprietari = value
            OnPropertyChanged("Proprietari")
            If _Proprietari.Count > 0 Then
                Me.proprietar = _Proprietari(0) 'this line sets Proprietar to be the same as the first item of Proprietari and it works as it should be
            End If
        End Set
    End Property

Public MustInherit Class AnexaViewModel(Of AnexaT As {AnexaClass, New})
    Inherits ObjectBase

    Private _Anexa As New AnexaT
    Public Property Anexa As AnexaT
        Get
            Return _Anexa
        End Get
        Set(value As AnexaT)
            _Anexa = value
            OnPropertyChanged("Anexa")
        End Set
    End Property
    Public Sub ToXML()
        MsgBox(Anexa.proprietar.nume) 'at this point Anexa.proprietar is nothing
        MsgBox(Anexa.Proprietari(0).nume) ' but this is fine, even though Proprietari is only declared inside the derived class Anexa3Class
        ''Some other code''
    End Sub
End Class
Public Class Anexa3ViewModel
    Inherits AnexaViewModel(Of Anexa3Class)
End Class

My program instantiate Anexa3ViewModel, then it sets Proprietari property which sets Proprietar to be Proprietari(0) (when I debug, this seems to work correctly), then I call ToXML by pressing a button through commanding. Inside ToXML Anexa.proprietar is nothing, but Anexa.Proprietari(0) has the correct value.

Apparently proprietar property lost its value, or there are two Proprietar Properties stored, one for my base class and one for the derived class. I thought this is possible only by shadowing a base property, which I'm not doing. I think there is some inheritance notion that I fail to understand.

Could someone shed some light on this please?

Clarifications1: I know that Proprietari's setter doesn't get fire if I simply add an item to the collection. This is not my problem as I set the whole collection at once and its setter gets fired and I can see that proprietar gets the correct value of Proprietari(0). The problem is that it is loosing its value when it gets to ToXML.

4

1 回答 1

0

在没有看到应用程序的其余部分的情况下,最好的猜测是您有其他代码向 Anexa3Class 中的 Proprietari 集合添加值,而不是通过属性设置整个集合。

根据您的应用程序需求和设计,有一些潜在的解决方法。一种方法是处理 Proprietari 集合上的CollectionChanged事件:

Private WithEvents _Proprietari As New ObservableCollection(Of ProprietarClass)
Private Sub _Proprietari_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _Proprietari.CollectionChanged
    If Me.proprietar Is Nothing AndAlso e.Action = Specialized.NotifyCollectionChangedAction.Add Then
        Me.proprietar = e.NewItems(0)
    End If
End Sub

另一个可能的选择是将 propietar 更改为可覆盖的只读属性并始终返回第 0 个值,但这可能会破坏 OnPropertyChanged 逻辑。

于 2013-07-02T21:52:04.723 回答