请帮助提供扩展 WPF 内置类的最佳方法
我想使用一些附加的专有属性来扩展 Shapes.Rectangle WPF 内置类。我可以通过 3 种不同的方式来完成此操作:
- 声明我自己的包装类并将 WPF 矩形作为其成员之一。
- 使用我的专有属性声明我自己的结构/类并将其放入 Rectangle.Tag 字段中。
- 为我的每个专有属性声明 WPF 依赖属性并使用 Rectangle.SetValue() &矩形.GetValue() 方法。
从性能的角度(速度、内存消耗)来看,最好的方法是什么?在任何时候,只有我的专有属性的一部分具有有意义的价值?
谢谢。
I want to extend Shapes.Rectangle WPF built-in class with some additional proprietary properties. I can do this in 3 different ways:
- Declare my own wrapper class and have WPF Rectangle as one of its members.
- Declare my own struct/class with my proprietary properties and put it in Rectangle.Tag field.
- Declare WPF dependency property for each of my proprietary properties and use Rectangle.SetValue() & Rectangle.GetValue() methods.
What is the best approach from performance point of view (speed, memory consumption), giving that at every moment only part of my proprietary properties will have meaningful value ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个扩展行为的行为。
这是向现有元素添加行为的最佳和最简单的方法(并且混合友好)。
它现在是WPF4的一部分,您可以在添加对System.Windows.Interactivity的引用后使用它。
You can Create a Behavior that extends Behavior.
That's the best and easiest way (and Blend friendly) to add behavior to existing elements.
It's part of WPF4 now, and you can use it after u add a reference to System.Windows.Interactivity.
您是否会在任何地方使用您的“某种”派生矩形,或者它有时是一个矩形,有时是您的?
如果您每次都要使用某些属性,我会说您应该创建一个包装类并尽可能保持轻量级。这将使您的代码尽可能干净。我认为除非你做了数千次这样的操作,否则性能不会成为问题。如果是的话,那么您可能还会遇到其他问题! :)
Are you going to be using your "sort of" derived Rectangle everywhere, or is it going to sometimes be a Rectangle and sometimes yours?
If you're going to be using some of your properties every single time, I would say you should create a wrapper class and just keep it as lightweight as possible. That'll keep your code as clean as possible. I don't think the performance will be an issue unless you're doing thousands of these. And if you are, then you might have other issues! :)