UIElement 在运行时发生转换错误
当我这样做时,我收到运行时错误。
我有这个类:
public abstract class AnnObject : DependencyObject
当我这样做时,它可以正常编译,但会引发运行时错误...
AnnObject aa;
var b = (DependencyObject)aa;
var c = (UIElement)b;
我得到的错误是无法将 AnnObject 转换为 UIElement
。
有人可以简要解释一下这种行为吗?
I get runtime error when I do this.
I have this class:
public abstract class AnnObject : DependencyObject
and when I do this it compiles fine, but throws a runtime error...
AnnObject aa;
var b = (DependencyObject)aa;
var c = (UIElement)b;
The error I get is cannot cast AnnObject to UIElement
.
Can someone please briefly explain this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Silverlight 中 UI 组件的类层次结构是: -
因此,正如 Heinz 指出的那样,您需要从
UIElement
派生,以便能够转换为UIElement
和依赖对象
。就我个人而言,我不认为从DependencyObject
派生有那么有用。我通常会从FrameworkElement
、Control
甚至更高的级别开始。The class hiearchy in Silverlight for UI components is:-
So as Heinz points out you would need to have derived from
UIElement
order to be able to cast toUIElement
andDependencyObject
. Personnally I can't see deriving fromDependencyObject
being that useful. I would normally start atFrameworkElement
,Control
or even higher.您仅从
DependencyObject
派生,而不是从UIElement
派生。You only derive from
DependencyObject
, not fromUIElement
.