C# 依赖属性 - 是否检查 CLR 包装器中的值

发布于 2024-10-10 02:59:58 字数 642 浏览 0 评论 0原文

在用 C# 编写自定义依赖属性时,一个相当常见的包装器类似于:

    public string Surname
    {
        get
        {
            return this.GetValue(SurnameProperty) as string;
        }

        set
        {
            this.SetValue(SurnameProperty, value);
        }
    }

现在,当使用 NotifyPropertyChanged 系统时,我通常会在实际提交值并调用 OnPropertyChanged 之前检查“set”块中的值是否已更改。我应该对依赖属性做同样的事情吗?即:

        set
        {
            if(this.GetValue(SurnameProperty) != value)
               this.SetValue(SurnameProperty, value);
        }

...或者这是完全没有必要并且已经由 CLR 处理的事情?我在 MSDN 上看到的所有示例在调用 SetValue 之前都懒得做任何检查。非常感谢。

On writing custom dependency proerties in C#, a fairly common wrapper goes something like:

    public string Surname
    {
        get
        {
            return this.GetValue(SurnameProperty) as string;
        }

        set
        {
            this.SetValue(SurnameProperty, value);
        }
    }

Now, when using the NotifyPropertyChanged system, I would normally check whether a value has changed in the 'set' block before actually committing the value and calling OnPropertyChanged. Should I do the same for dependency properties? i.e.:

        set
        {
            if(this.GetValue(SurnameProperty) != value)
               this.SetValue(SurnameProperty, value);
        }

...or is this something that is completely unnecessary and already taken care of by the CLR? All examples on MSDN that I have seen to not bother to do any checking before calling SetValue. Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-10-17 02:59:58

简短的回答是,不,框架已经处理好了。

实际上,根据 Adam Nathan 的“Windows Presentation Foundation”,XAML 编译器在编译时依赖于属性扭曲器。然而,.NET 属性包装器实际上在运行时在 XAML 中被绕过。因此,除了 GetValue/SetValue 之外,您实际上应该避免添加任何类型的逻辑。仅当您显式调用该属性时,您在 setter 中添加的任何逻辑才会执行。但是,如果您在 XAML 中绑定该属性,运行时将跳过它。如果您的 setter 中有错误,您可能需要一些时间才能弄清楚。如果您手中有这本书,请参阅第 53 页。

The short answer is, no it's taken care by the framework already.

Actually, according to Adam Nathan's "Windows Presentation Foundation", XAML compiler depends on the property warpper at compile time. However, .NET property wrappers are actually bypassed at run-time in XAML. Therefore, you should actually avoid adding any kinds of logic in addtion to GetValue/SetValue. Whatever logic you added in the setter is only executed if you call the property explicitly. However, if you bind that property in XAML, the runtime will skip it. If you have a bug in your setter, this may take you some time to figure out. Please see page 53 if you got that book in your hand.

甜扑 2024-10-17 02:59:58

不,它是由依赖属性系统处理的。

No, it's taken care of by the dependency property system.

献世佛 2024-10-17 02:59:58

它由属性更改回调来处理。仅当属性值发生更改时才会调用 PropertyChangedCallback

public static readonly DependencyProperty MyDependencyProperty =
                   DependencyProperty.Register(
                         "Dependency",
                          typeof(string),
                          typeof(MyUserControl),
                          new FrameworkPropertyMetadata("Initializing...", new PropertyChangedCallback(OnMyDependencyChangedCallBack)));

It is taken care by the property changed callback. The PropertyChangedCallback is called only when there is a change in the property value.

public static readonly DependencyProperty MyDependencyProperty =
                   DependencyProperty.Register(
                         "Dependency",
                          typeof(string),
                          typeof(MyUserControl),
                          new FrameworkPropertyMetadata("Initializing...", new PropertyChangedCallback(OnMyDependencyChangedCallBack)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文