WPF/UWP: DependencyObject的GetValue()和ReadLocalValue()有什么区别?
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 sample 中得到的只是它可能 return DependencyProperty.UnsetValue
,这并不能说明什么。
好像就是这样。大多数情况下,是的,它用于
Return the local value, or return the sentinel value UnsetValue
if no local value is set.
根据 MSDN Reference:
You should use GetValue for most typical "get" operations for a dependency property. ReadLocalValue does not return the effective value for a variety of circumstances where the value was not locally set.
Values that are set by styles, themes, templates, the default value from metadata, or property value inheritance are not considered to be local values. However, bindings and other expressions are considered to be local values, after they have been evaluated.
When no local value is set, this method returns UnsetValue.
If the returned value is other than UnsetValue, you can query the metadata of the requested dependency property to determine whether there is a more specific type that the return value can be converted to.
我是 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 sample 中得到的只是它可能 return DependencyProperty.UnsetValue
,这并不能说明什么。
好像就是这样。大多数情况下,是的,它用于
Return the local value, or return the sentinel value
UnsetValue
if no local value is set.
根据 MSDN Reference:
You should use GetValue for most typical "get" operations for a dependency property. ReadLocalValue does not return the effective value for a variety of circumstances where the value was not locally set.
Values that are set by styles, themes, templates, the default value from metadata, or property value inheritance are not considered to be local values. However, bindings and other expressions are considered to be local values, after they have been evaluated.
When no local value is set, this method returns UnsetValue.
If the returned value is other than UnsetValue, you can query the metadata of the requested dependency property to determine whether there is a more specific type that the return value can be converted to.