UIElement 在运行时发生转换错误

发布于 2024-09-10 08:55:25 字数 325 浏览 2 评论 0原文

当我这样做时,我收到运行时错误。

我有这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-09-17 08:55:25

Silverlight 中 UI 组件的类层次结构是: -

DependencyObject
  UIElement
    FrameworkElement
      Control

因此,正如 Heinz 指出的那样,您需要从 UIElement 派生,以便能够转换为 UIElement依赖对象。就我个人而言,我不认为从 DependencyObject 派生有那么有用。我通常会从 FrameworkElementControl 甚至更高的级别开始。

The class hiearchy in Silverlight for UI components is:-

DependencyObject
  UIElement
    FrameworkElement
      Control

So as Heinz points out you would need to have derived from UIElement order to be able to cast to UIElement and DependencyObject. Personnally I can't see deriving from DependencyObject being that useful. I would normally start at FrameworkElement, Control or even higher.

会发光的星星闪亮亮i 2024-09-17 08:55:25

您仅从 DependencyObject 派生,而不是从 UIElement 派生。

You only derive from DependencyObject , not from UIElement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文