locked
How to get type of an WinRT object?

Answers

  • You can use dynamic_cast to cast the UIElement to more specific types:

    void MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	Button^ b = dynamic_cast<Button^>(sender);
    	if (b)
    	{
    		// Do Button stuff
    	}
    	else
    	{
    		// not a button
    	}
    }
    

    --Rob 

    • Marked as answer by Bob_Bao Wednesday, December 7, 2011 8:22 AM
    Friday, November 11, 2011 11:19 PM
    Owner

All replies

  • Hi - I believe this functionality is not available for C++ in the Developer Preview build.  What is the scenario you are trying to use GetType() with?
    Monday, November 7, 2011 11:14 PM
  • Thanks for the reply.

    I'm using some XAML controls, such as the StackPanel, where I want to get a reference to child elements using StackPanel.Children.

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.panel.children(v=VS.85).aspx

    The items within this collection are stored as type UIElement, however my stack panel contains various controls which get dynamically added to it. I'm looking for a way to identify if a particular UIElement is of type Image or Button, etc.

     

    Tuesday, November 8, 2011 2:54 AM
  • You can use dynamic_cast to cast the UIElement to more specific types:

    void MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	Button^ b = dynamic_cast<Button^>(sender);
    	if (b)
    	{
    		// Do Button stuff
    	}
    	else
    	{
    		// not a button
    	}
    }
    

    --Rob 

    • Marked as answer by Bob_Bao Wednesday, December 7, 2011 8:22 AM
    Friday, November 11, 2011 11:19 PM
    Owner