简体   繁体   English

WPF 中公共/私有成员的编码约定是什么?

[英]What's the coding convention about public/private members in WPF with binding?

I know this is a largely debated question in stackoverflow (as eg in Should I use public or private variables? ), but I haven'd been able to find a suitable answer to my doubt.我知道这是 stackoverflow 中一个备受争议的问题(例如在我应该使用公共变量还是私有变量? ),但我一直能够找到合适的答案来解决我的疑问。

Shortly said, OO coding etiquette mandates that members of a class be kept private or that getters/setters be used as an alternative.简而言之,OO 编码礼仪要求 class 的成员保密,或者使用 getter/setter 作为替代方法。

However, data binding in WPF will not succeed unless the bound class members are public.但是,除非绑定的 class 成员是公共的,否则 WPF 中的数据绑定将不会成功。 So, what's the best way out here?那么,这里最好的出路是什么? Shall I accept declaring public members as an ugly but necessary compromise?我应该接受宣布公众成员是一种丑陋但必要的妥协吗?

Data binding is normally used on properties, not fields, so that does not contradict the general principle that fields should be kept private.数据绑定通常用于属性,而不是字段,因此这与字段应保持私有的一般原则并不矛盾。

The sentence "that members of a class be kept private" is, as such, not correct.因此,“class 的成员必须保密”这句话是不正确的。 Fields should be kept private.字段应保密。 The term "members" means fields as well as methods or properties.术语“成员”意味着字段以及方法或属性。 And of course, it is perfectly sensible to have methods or properties that are public.当然,拥有公开的方法或属性是完全明智的。

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

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