gpt4 book ai didi

c# - WPF/UWP : What is the difference between DependencyObject's GetValue() and ReadLocalValue()?

转载 作者:行者123 更新时间:2023-11-30 16:50:04 26 4
gpt4 key购买 nike

我是 WPF 开发人员。我很想知道,DependencyObjectGetValueReadLocalValue 方法有什么区别?我知道 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中获得的一切是它可能返回 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.

关于c# - WPF/UWP : What is the difference between DependencyObject's GetValue() and ReadLocalValue()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com