Casting error for UIElement on runtime
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
.
精彩评论