testing
testing

Reputation: 20279

Get BaseType from object in Xamarin.Forms

I'm trying to find out if the object I have is of type Page. If I ask for GetType() I get ContentPage, which is a subclass of TemplatedPage, which is a subclass of Page (which again is a subclass of VisualElement and so on).

My current approach

if(parentElement.GetType() == typeof(Page))
{ 
    // do something
}

does not work of this. Now I tried to get the BaseType as shown in C# : how do you obtain a class' base class?. If I do this

Type superClass = parentElement.GetType().BaseType;

I get

Error CS1061 'Type' does not contain a definition for 'BaseType' and no extension method 'BaseType' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)

I have System namespace included as well as System.Reflection and I still get this error. Is it possible to get the BaseType in Xamarin.Forms?

Upvotes: 0

Views: 753

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34013

You should just be able to check that out with the is keyword. That will take care of the hard work for you.

So like: if (parentElement is Page) { DoStuffHere(); }

Upvotes: 2

Related Questions