简体   繁体   English

实现在WPF和Silverlight中的静态属性上更改了INotifyProperty

[英]Implement INotifyProperty changed on Static Property in WPF and Silverlight

The question is how to implement INotifyPropertyChanged on a static property because the event you implement is not static, and cannot be called by a static property. 问题是如何在静态属性上实现INotifyPropertyChanged ,因为您实现的事件不是静态的,并且不能由静态属性调用。 Also, you cannot bind to a static property in Silverlight. 此外,您无法绑定到Silverlight中的静态属性。

I've seen this question pop up an a few forums with a variety of solutions, none of which were very satisfying. 我已经看到这个问题在一些论坛中出现了各种各样的解决方案,其中没有一个非常令人满意。

Well, I think I've found an elegant solution, but it's so simple I feel like I must be missing something. 嗯,我想我已经找到了一个优雅的解决方案,但它很简单,我觉得我必须遗漏一些东西。

The answer is, to write a non-static property that accesses a static variable like so: 答案是,编写一个访问静态变量的非静态属性,如下所示:

    private static double length;
    public double Length
    {
        get
        {
            return length;
        }
        set
        {
            length = value;
            NotifyPropertyChanged("Length");
        }
    }

I've tested it and it seems to work just fine. 我已经测试了它,似乎工作得很好。 Am I missing something? 我错过了什么吗?

Technically, you're still not binding to a static property - you're binding to an instance of the class, which is using the static field as a backing store. 从技术上讲,您仍然没有绑定到静态属性 - 您绑定到类的实例,该实例使用静态字段作为后备存储。 This will work, to some extent, but... 这在某种程度上会起作用,但......

There is a fundamental problem with this - If you have more than one item bound to this same backing store (which seems like something you're attempting, since you're purposefully making it static), the INotifyPropertyChanged notifications will only happen on the instance where you are currently bound. 这有一个根本问题 - 如果你有多个项目绑定到同一个后备存储(这似乎是你正在尝试的东西,因为你有意使它静态), INotifyPropertyChanged通知只会发生在实例上你目前在哪里受到约束。

Say, for example, you had two UserControls, sitting side by side, both bound to a ViewModel containing this code. 比如说,你有两个并排坐着的UserControl,它们都绑定到包含这段代码的ViewModel。 When control A sets this property, control B will never receive notifications (since it's A's INotifyPropertyChanged that runs), so it will appear out of sync. 当控件A设置此属性时,控件B将永远不会收到通知(因为它是运行的A的INotifyPropertyChanged),因此它将显示为不同步。

If you really want to try to do something like this, you're probably better off having your backing store use a class that implements INotifyPropertyChanged, and "bubble" up the property through your ViewModel class. 如果你真的想尝试做这样的事情,你可能最好让你的后备存储使用一个实现INotifyPropertyChanged的类,并通过你的ViewModel类“冒泡”属性。 This way, multiple instances would all be notified correctly, and you could handle any multithreading/synchronization issues that might occur if necessary. 这样,将正确通知多个实例,并且您可以处理可能在必要时发生的任何多线程/同步问题。

Alternatively, you may want to consider using a single instance property (with an instance field), inside of a Singleton . 或者,您可能需要考虑在Singleton中使用单个实例属性(带有实例字段)。 This, too, would give you shared notifications of your "static" property. 这也会为您提供“静态”属性的共享通知。

Right, but it's not really a static property, is it? 是的,但它不是真正的静态属性,是吗?
it's a public instance property that uses a static backing field. 它是一个使用静态支持字段的公共实例属性。
In essence, you're binding to a specific instance of a class. 实质上,您绑定到类的特定实例。

Sorry, but I think you're doing it wrong. 对不起,但我觉得你做错了。
Personally, without knowing you're scenario, I'm willing to guess that a static property binding isn't really the technical solution you need. 就个人而言,在不知道你的情况的情况下,我愿意猜测静态属性绑定并不是你真正需要的技术解决方案。
What's the problem you're trying to work through? 你正在尝试解决的问题是什么?
Why isn't it better addressed by a normal binding to a ViewModel? 为什么通过与ViewModel的正常绑定更好地解决了这个问题?
What's the use case for doing something like this? 做这样的事情的用例是什么?

Personally, this looks like a perfect scenario to have a ViewModel register to a singleton service, and once a singleton event is raised change ViewModel properties. 就个人而言,这看起来像是一个完美的场景,可以将ViewModel注册到单例服务,并且一旦引发单例事件,就会更改ViewModel属性。

locks for multi-threading? 用于多线程的锁?

C# thread safety with get/set C#线程安全与get / set

Why does the property have to be static? 为什么财产必须是静态的? If it was just a regular instance property, this wouldn't be an issue. 如果它只是一个常规实例属性,这不会是一个问题。

Avoid shared mutable state when possible :) 尽可能避免共享的可变状态:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM