将信息存储在不相关的附加属性中是否有代码味道?
我刚刚观看了名为 如何:使用附加的“操作方法”类型 WPF 视频在 WPF 中存储额外数据的属性,它描述了当您想要将两条信息与单个控件关联时该怎么做。如果您想放入一条信息,他们说要使用 Tag 属性。
<Grid>
<TextBox Tag="innerData">
</Grid>
很公平。
但他们接着说,“如果你想存储第二条数据,比如整数,会发生什么?”他们说,解决方案是使用未使用的附加属性。
<Grid>
<TextBox Tag="innerData" Canvas.Top="55">
</Grid>
他们说因为它在网格中,所以没问题!我说“哎呀!”这让我觉得非常恶心……而且读起来很误导。这是常见做法吗?
I was just watching a "how-to" type WPF video called How Do I: Use Attached Properties to Store Extra Data in WPF, and it was describing what to do when you want to associate two pieces of information with a single control. If you want to put one piece of information in, they say to use the Tag property.
<Grid>
<TextBox Tag="innerData">
</Grid>
Fair enough.
But then they say, "what happens if you want to store a second piece of data, say, an integer?" The solution, they say is to use an unused attached property.
<Grid>
<TextBox Tag="innerData" Canvas.Top="55">
</Grid>
They say because it's in a Grid, no problem! I say "Bleh!" This strikes me as extremely nasty...and quite misleading to read. Is this common practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是一种气味。
我不确定这是否常见——我以前从未见过。
坦率地说,我发现
Tag
本身就是一种框架认可的气味。Yes, it is a smell.
I'm not sure if it is common -- I've never seen it before.
Frankly, I find
Tag
to be a framework-endorsed smell itself.它既快速又简单,但最好定义自己的附加属性或自定义控件来处理这些情况。这种事情对于原型或快速的一次性应用程序来说是可以的,但如果代码的实际生命周期需要维护,那么就不应该这样做。
It's quick and easy but it's definitely preferable to define your own Attached Properties or custom control to handle these situations. This is the type of thing that's okay for prototypes or quick one-off apps but shouldn't be done if the code is going to have a real lifespan that will require maintenance down the road.
这不是最佳实践。
我始终认为,代码(包括 Xaml)是为了人类可读而编写的。
需要进行大量研究才能弄清楚谁在使用该财产以及用途。
Not a best practice.
I am always of the opinion, code (including Xaml) is written to be human readable.
It would take quite a bit of research to figure out who is using that property, and for what.
这项技术可以真正节省时间。使用它会告诉您,“我不应该再浪费时间学习这些培训材料。”
This technique can be a real time-saver. Use of it tells you, "I shouldn't waste any more time studying these training materials."