#160 – Be Careful When Setting Dependency Property Values from a DependencyObject Constructor
来源:互联网 发布:手机淘宝松子类目 编辑:程序博客网 时间:2024/06/06 00:02
在构造函数中为依赖属性赋值必须小心
原文地址:https://wpf.2000things.com/2010/12/19/160-be-careful-when-setting-dependency-property-values-from-a-dependencyobject-constructor/
使用C#编码的时候,我们应当尽量避免在构造函数中调用虚函数。因为你的构造函数很有可能是通过子类调用的,而这个时候子类并没有完全初始化完成。如果子类中重写了在构造函数中被调用的虚函数,那么这个被重写的虚函数在构造函数中将不会被父类调用。
如果你用FxCop检查你的代码,就会发现有DoNotCallOverridableMethodsInConstructors 这样一条规则。
这条规则适用于WPF。如果你在构造函数中为依赖属性赋值,那么在属性系统中,虚函数或者回调函数将不会被调用。
避免这个问题的最好方法是:
所有在属性回调中用到的实例初始化都在默认构造函数中进行。(译者注:不是太明白具体原因。。。)
0 0
- #160 – Be Careful When Setting Dependency Property Values from a DependencyObject Constructor
- #116 – Dependency Property Values Are Stored in the DependencyObject(依赖属性存储在DependencyObject中)
- #147 – Use SetCurrentValue When You Want to Set A Dependency Property Value from Within a Control
- Be careful when Android up merge
- Please be careful when include a resouce.h file to any cpp to avoid regression.
- Hashtables - When you create your own key object in a Hashtable, be careful
- #121 – Being Notified When the Value of a Dependency Property Changes(属性值改变通知)
- #158 – When to Create a Custom Dependency Property(哪些情况要创建自定义依赖属性)
- Be careful of static field when unit testing!
- Be Very Careful When You Write SQL Trigger
- be careful
- Be careful about the "id" property
- #122 – Validating a Dependency Property(验证依赖属性)
- get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initia
- get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initia
- CareerCup Find top k values (asec) which can either be the number from the array A
- WPF Background property does not point to a dependencyobject in path '(0).(1)'
- Be careful JVM
- 史上最全的机器学习资料(上)
- UnitTest
- 手工消除dataguard的gap
- 史上最全的机器学习资料(下)
- webService原理及使用
- #160 – Be Careful When Setting Dependency Property Values from a DependencyObject Constructor
- 数据库恢复,记录一次全量数据文件丢失的恢复
- TFTP、NFS、FTP三种文件传送协议(附linux协议配置)
- 蓝牙模块在HHARM2410上的移植
- Android:layout_weight
- 使用视频作为网页背景的技术探讨
- unix文件描述符——socket
- 史上最全的“大数据”学习资源(上)
- javaSE学习笔记——接口