开发者

Casting error for UIElement on runtime

开发者 https://www.devze.com 2023-01-07 10:20 出处:网络
I get runtime error when I do this. I have this class: public abstract class AnnObject : DependencyObject

I get runtime error when I do this.

I have this class:

public abstract class AnnObject : DependencyObject

and开发者_JAVA百科 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?


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.


You only derive from DependencyObject , not from UIElement.

0

精彩评论

暂无评论...
验证码 换一张
取 消