为什么 System.Windows.Shape.Path 被密封?
我试图扩展形状类以包含附加变量,但发现该类是密封的。我怎样才能使用替代实施方法简单地实现这一目标?
正在创建一个新类并存储一个形状,并通过最简单的方法传递所有方法调用;我确信还有更好的方法吗?
I was trying to extend the shape class to contain an additional variable but have found the class is sealed. How can I achive this simply, using an alternate implementation approach?
Is creating a new class and storing a shape an passing all the method calls through the easiest approach; I'm sure there is a better way though?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要添加的变量是您只能读取的变量,并且是作为 Path 类其他成员的结果进行计算的,则可以添加一个将返回您的值的扩展方法。
但是,如果情况并非如此:
请考虑创建一个继承自 Path 的父类 - Shape 的类,该类未密封。
然后向其中添加一个私有 Path 成员。您可以免费获取所有形状方法调用,并且只需为 Path 的唯一成员提供包装器。
If the variable you want to add is one which you would only ever be reading from, and is calculated as a result of other members of the Path class, you could add an extension method which would return your value.
However, if this is not the case:
Consider creating a class which inherits from Path's parent - Shape, which is not sealed.
Then add a private Path member to this. You can grab all the shape method calls for free and would only have to provide wrappers for the unique members of Path.
WPF 有一个强大的概念,名为附加属性。我不确定你想做什么,但也许你可以通过使用这个概念来解决你的问题。举个例子,
Grid.Row
和Grid.Column
属性是附加到Grid
内元素的属性。TextBlock
类不知道Grid
使用的Grid.Row
和Grid.Column
属性。您可以在MSDN 上阅读有关附加属性的更多信息。
WPF has a powerful concept named Attached Properties. I'm not sure what you are trying to do, but perhaps you can solve your problem by using this concept. To provide an exampe, the
Grid.Row
andGrid.Column
properties are properties attached to elements inside aGrid
.The
TextBlock
class is unaware of theGrid.Row
andGrid.Column
properties used by theGrid
.You can read more about attached properties on MSDN.