[英]Why does Silverlight View not throw a null exception?
Given a Silverlight View with the following binding: 给定具有以下绑定的Silverlight视图:
<TextBox Width="200" Text="{Binding Customer.FirstName, Mode=TwoWay}"/>
and the code behind has the following: 后面的代码如下:
CustomerClass Customer {get; set;}
This will not throw a NullReferenceException
however the following 这不会引发
NullReferenceException
但是以下内容
String FirstName
{
get { return Customer.FirstName; }
}
does when I attempt to bind to FirstName
instead of Customer.FirstName
, why is this and how could this be corrected? 当我尝试绑定到
FirstName
而不是Customer.FirstName
,为什么会这样,如何解决? (Other than just binding directly to Customer.FirstName
or initializing the CustomerClass
object) (除了直接绑定到
Customer.FirstName
或初始化CustomerClass
对象之外)
EDIT : To address the possible duplicate issue. 编辑 :解决可能的重复问题。 I thought that the binding still tried to get a reference when the view is first initialized, is this not the case?
我以为绑定是在第一次初始化视图时仍然尝试获取引用,不是这种情况吗? If so then I can see the difference between getting a reference and just being bound at view time
如果是这样,那么我可以看到获得参考与仅在查看时绑定之间的区别
I thought that the binding still tried to get a reference when the view is first initialized, is this not the case?
我以为绑定是在第一次初始化视图时仍然尝试获取引用,不是这种情况吗?
The binding process is designed to handle null values and will check the initial target reference which is given and not attempt any action if null. 绑定过程旨在处理空值,并将检查给定的初始目标引用 ,如果为空则不尝试任何操作。 Remember binding is simply the process of reflection off of a named path/location and not the actual extraction of the value.
请记住,绑定只是对指定路径/位置的反射过程,而不是对值的实际提取。
When the binding discovers that the location it is trying to reflect off of which is Customer.FirstName
is null, it stops right there. 当绑定发现它试图反映的位置是
Customer.FirstName
为null时,它将在那里停止。
But when it is given a binding of FirstName
, that location reference for the binding is quite valid. 但是,当给定
FirstName
的绑定时,该绑定的位置 引用是非常有效的。 Bingo! 答对了! Then when the ultimate operation after the binding goes to extract a value the
getter
is called which ultimately throws an exception because either Customer
or FirstName
is null. 然后,当绑定后的最终操作要提取值时,将调用
getter
,该方法最终会引发异常,因为Customer
或FirstName
为null。
how could this be corrected?
如何纠正呢?
Look into adding to the binding with either TargetNullValue , which is an alternate binding to use when null. 考虑使用TargetNullValue添加到绑定中,这是null时要使用的备用绑定。 Or directly providing a FallbackValue to use while the binding is null.
或在绑定为null时直接提供FallbackValue以使用。
Or design the GUI to not rely on sub properties of an object which may be null. 或者将GUI设计为不依赖于可能为null的对象的子属性。
XAML bindings will not throw a NullReferenceException
in and of themselves, but they also don't catch exceptions that are thrown by property getters. XAML绑定本身不会抛出
NullReferenceException
,但它们也不会捕获由属性getter抛出的异常。 In some "flavors," like WPF, there are extended binding properties like FallbackValue
and TargetNullValue
. 在某些“风味”(如WPF)中,具有扩展的绑定属性(如
FallbackValue
和TargetNullValue
。 (I'm not sure how much of that is supported in Silverlight.) (我不确定Silverlight支持多少功能。)
When a binding fails, a trace warning message will be written, but an exception will not be thrown. 绑定失败时,将写入跟踪警告消息,但不会引发异常。
In the first case, the property getter is evaluated and returning null
. 在第一种情况下,将评估属性getter并返回
null
。
In the second case, the property getter is evaluated but cannot return a value to the binding system because of the exception. 在第二种情况下,将对属性getter进行评估,但由于异常而无法将值返回到绑定系统。
To correct it, simply check for null
in the getter and return a default value if so: 要更正它,只需在getter中检查是否为
null
,如果是,则返回默认值:
string FirstName
{
get { return Customer == null ? string.Empty : Customer.FirstName; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.