INotifyPropertyChanged 的可空类型和属性
设置可为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要所有复杂的逻辑。如果
x
和y
都是具有相同底层类型的可空值,则x
等于y
当且仅当x.HasValue
为true
y.HasValue
为true
x.Value
等于y.Value
或
x.HasValue
为false
y.HasValue
为false
在这两种情况下在某些情况下,我们想要发出属性更改通知,因此对不相等性的简单测试就足够了。因此:
请注意,我们需要使用
Not Nullable(Of T).Equals
而不是<>
,因为后者的计算结果为Nothing
如果如果其操作数为Nothing
,则为 1。There is no need for all the complicated logic. If
x
andy
are both nullables with the same underlying type thenx
equalsy
if and only ifx.HasValue
istrue
y.HasValue
istrue
x.Value
equalsy.Value
or
x.HasValue
isfalse
y.HasValue
isfalse
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:
Note that we ned to use
Not Nullable(Of T).Equals
instead of<>
as the latter evaluates toNothing
if one if its operands isNothing
.顺便说一句......我注意到您正在使用 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