简体   繁体   English

什么时候实现iNotifyPropertyChanged接口?

[英]When to implement iNotifyPropertyChanged interface?

From my understanding, it appears that INotifyPropertyChanged is very useful when working with UI elements that are related to one object source. 据我了解,当使用与一个对象源相关的UI元素时, INotifyPropertyChanged似乎非常有用。 I'm just curious as to why some classes in the .net framework which may in some way be used with the UI do not implement INotifyPropertyChanged ? 我只是好奇为什么.net框架中的某些类(可能以某种方式与UI一起使用)无法实现INotifyPropertyChanged For example SerialPort class. 例如SerialPort类。

Is there a recommended guideline on the use of INotifyPropertyChanged ? 是否有关于使用INotifyPropertyChanged的建议指南? or have I totally misunderstood the concept of the interface? 还是我完全误解了接口的概念?

This question was brought about after trying to link several custom user controls to the SerialPort class. 尝试将多个自定义用户控件链接到SerialPort类后,引起了此问题。

You understood the interface correctly. 您正确理解了界面。

But thinking SerialPort class is somehow related to UI is the bad thing. 但是认为SerialPort类与UI有某种联系是一件坏事。 This class is supposed to allow access to this one resource. 该类应该允许访问这一资源。 Nothing more, nothing less. 仅此而已。 If you want to somehow display information related to SerialPort , you should create your own business class, that will use SerialPort and expose needed information through properties and INotifyPropertyChanged . 如果要以某种方式显示与SerialPort相关的信息,则应该创建自己的业务类,该业务类将使用SerialPort并通过属性和INotifyPropertyChanged公开所需的信息。

Generally INotifyPropertyChanged is implemented on class having business logic of application developed for WPF or Silverlight. 通常,INotifyPropertyChanged在具有为WPF或Silverlight开发的应用程序的业务逻辑的类上实现。

If SerialPort class is used by other applications like WCF or Winform 2.0, INotifyPropertyChanged will be of little or no use. 如果SerialPort类被其他应用程序(如WCF或Winform 2.0)使用,则INotifyPropertyChanged几乎没有用。

Anyway its an interface that is understood by WPF framework engine. 无论如何,它是WPF框架引擎可以理解的接口。 And You can create a business class which can implement INotifyPropertyChanged and internally hold SerialPort class. 并且,您可以创建一个业务类,该业务类可以实现INotifyPropertyChanged并在内部保留SerialPort类。

Hope it makes sense. 希望有道理。

INotifyPropertyChanged is to allow objects to communicate to their bindings, so in that sense your understanding is correct. INotifyPropertyChanged是为了允许对象与其绑定进行通信,因此从某种意义上讲您的理解是正确的。

You can wrap the SerialPort to provide this. 您可以包装 SerialPort以提供此功能。

I'm not sure why SerialPort doesn't implement this interface, so in that sense I can't answer that part of the question. 我不确定为什么SerialPort不实现此接口,所以从这个意义上讲,我无法回答问题的那一部分。

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

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