[英]What is the difference between myArray.GetValue(2) and myArray[2] in C#?
[英]WPF/UWP: What is the difference between DependencyObject's GetValue() and ReadLocalValue()?
我是WPF开发人员。 我很想知道, DependencyObject
的GetValue
和ReadLocalValue
方法有什么区别? 我知道GetValue
可以用来实现依赖属性,如下所示:
public static DependencyProperty FoobarProperty =
DependencyProperty.Register(nameof(Foobar),
typeof(int),
typeof(DeclaringClass),
new PropertyMetadata(0, OnFoobarChanged));
public int Foobar
{
get { return (int)GetValue(FoobarProperty); }
set { SetValue(FoobarProperty, value); }
}
private static void OnFoobarChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var newValue = (int)e.NewValue;
// do something with the new value...
}
在一个简单的解释中, ReadLocalValue
用途是什么? 我可以从MSDN样本获得的是它可能会返回DependencyProperty.UnsetValue
,这并不能说明多少。
这似乎与此有关。 在大多数情况下,是的,它已经习惯了
返回本地值,如果未设置本地值,则返回Sentinel值
UnsetValue
。
根据MSDN 参考 :
对于依赖项属性,您应该使用GetValue进行大多数典型的“get”操作。 对于未在本地设置值的各种情况,ReadLocalValue不返回有效值。
由样式,主题,模板,元数据的默认值或属性值继承设置的值不被视为本地值。 但是,绑定和其他表达式在被评估之后被认为是本地值。
如果未设置本地值,则此方法返回UnsetValue。
如果返回的值不是UnsetValue,则可以查询所请求的依赖项属性的元数据,以确定是否存在可以将返回值转换为更具体的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.