INotifyPropertyChanged 的​​可空类型和属性

发布于 2024-08-18 07:43:43 字数 1555 浏览 7 评论 0原文

设置可为 null 类型的值并实现 iNotifyPropertyChanged 似乎有点矫枉过正。有更好的方法吗?

        Private _WorkPhone As Long?
    Public Property [WorkPhone]() As Long?
        Get
            Return _WorkPhone
        End Get
        Set(ByVal value As Long?)
            If value.HasValue = False Then
                If _WorkPhone.HasValue = True Then
                    MyBase.RaisePropertyChanging("WorkPhone")
                    _WorkPhone = Nothing
                    MyBase.MarkDirty()
                    MyBase.RaisePropertyChanged("WorkPhone")
                End If
            Else
                If _WorkPhone.HasValue Then
                    If _WorkPhone.Value <> value.Value Then
                        MyBase.RaisePropertyChanging("WorkPhone")
                        _WorkPhone = value
                        MyBase.MarkDirty()
                        MyBase.RaisePropertyChanged("WorkPhone")
                    End If
                Else
                    MyBase.RaisePropertyChanging("WorkPhone")
                    _WorkPhone = value
                    MyBase.MarkDirty()
                End If
                MyBase.RaisePropertyChanged("WorkPhone")
            End If
        End Set
    End Property

我尝试使用简单的代码,但 MyBase.RaisePropertyChanging("WorkPhone") 上的断点从未被命中,并且该值也从未改变。

    If _WorkPhone <> value Then
    MyBase.RaisePropertyChanging("WorkPhone")
    _WorkPhone = value
    MyBase.MarkDirty()
    MyBase.RaisePropertyChanged("WorkPhone")
End If

It seems like overkill to set the value of a nullable type and implement iNotifyPropertyChanged. Is there a better way of doing this?

        Private _WorkPhone As Long?
    Public Property [WorkPhone]() As Long?
        Get
            Return _WorkPhone
        End Get
        Set(ByVal value As Long?)
            If value.HasValue = False Then
                If _WorkPhone.HasValue = True Then
                    MyBase.RaisePropertyChanging("WorkPhone")
                    _WorkPhone = Nothing
                    MyBase.MarkDirty()
                    MyBase.RaisePropertyChanged("WorkPhone")
                End If
            Else
                If _WorkPhone.HasValue Then
                    If _WorkPhone.Value <> value.Value Then
                        MyBase.RaisePropertyChanging("WorkPhone")
                        _WorkPhone = value
                        MyBase.MarkDirty()
                        MyBase.RaisePropertyChanged("WorkPhone")
                    End If
                Else
                    MyBase.RaisePropertyChanging("WorkPhone")
                    _WorkPhone = value
                    MyBase.MarkDirty()
                End If
                MyBase.RaisePropertyChanged("WorkPhone")
            End If
        End Set
    End Property

I tried using simple code, but my breakpoint on MyBase.RaisePropertyChanging("WorkPhone") is never hit, and the value never changes.

    If _WorkPhone <> value Then
    MyBase.RaisePropertyChanging("WorkPhone")
    _WorkPhone = value
    MyBase.MarkDirty()
    MyBase.RaisePropertyChanged("WorkPhone")
End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-08-25 07:43:43

不需要所有复杂的逻辑。如果 xy 都是具有相同底层类型的可空值,则 x 等于 y 当且仅当

  1. x.HasValuetrue
  2. y.HasValuetrue
  3. x.Value 等于 y.Value

  1. x.HasValuefalse
  2. y.HasValuefalse

在这两种情况下在某些情况下,我们想要发出属性更改通知,因此对不相等性的简单测试就足够了。因此:

Private _WorkPhone As Long?
    Public Property [WorkPhone]() As Long?
        Get
            Return _WorkPhone
        End Get
        Set(ByVal value As Long?)
            If Not _workPhone.Equals(value)
                MyBase.RaisePropertyChanging("WorkPhone")
                _WorkPhone = value
                MyBase.MarkDirty()
                MyBase.RaisePropertyChanged("WorkPhone")
            EndIf
        End Set
    End Property

请注意,我们需要使用 Not Nullable(Of T).Equals 而不是 <>,因为后者的计算结果为 Nothing 如果如果其操作数为 Nothing,则为 1。

There is no need for all the complicated logic. If x and y are both nullables with the same underlying type then x equals y if and only if

  1. x.HasValue is true
  2. y.HasValue is true
  3. x.Value equals y.Value

or

  1. x.HasValue is false
  2. y.HasValue is false

In neither of these cases would we want to raise a property changed notification and thus a simple test for non-equality will suffice. Thus:

Private _WorkPhone As Long?
    Public Property [WorkPhone]() As Long?
        Get
            Return _WorkPhone
        End Get
        Set(ByVal value As Long?)
            If Not _workPhone.Equals(value)
                MyBase.RaisePropertyChanging("WorkPhone")
                _WorkPhone = value
                MyBase.MarkDirty()
                MyBase.RaisePropertyChanged("WorkPhone")
            EndIf
        End Set
    End Property

Note that we ned to use Not Nullable(Of T).Equals instead of <> as the latter evaluates to Nothing if one if its operands is Nothing.

酷到爆炸 2024-08-25 07:43:43

顺便说一句......我注意到您正在使用 LONG 来存储工作电话号码。即使未签名,在美国区号 429 以上也会失败您可能会考虑 DOUBLE 或 STRING

As an aside...I notice you are using a LONG to store the work phone number. Even unsigned this will fail above areacode 429 in the U.S. You might consider DOUBLE or STRING

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文