locked
Example of creating a completely new UI control in C++?

    Question

  • Is there an example yet of creating a new WinRT UI control completely from scratch? I assume that we would use DirectX to render it, so it would have to be written in C++. Is there any sample code on how to do this?

    (Or how about releasing source for one of the simpler existing WinRT UI controls as a sample?)

    Wednesday, September 21, 2011 2:33 PM

Answers

  • Hi Jonathan,

    A custom Xaml control would not be rendered via DirectX but would be created using the Xaml and drawing primitives.  The basic idea is that you would create an object which inherits from Windows::UI::Xaml::Controls::Control and then implement whichever behavior is needed for the new control.  Expose it from a WinRT component DLL and it can be used from C#, VB, or C++ Xaml based apps.

    I'm not aware of any complete samples (yet!), but this will work essentially the same as creating a custom control for Silverlight or WPF so you can look at existing samples of those for inspiration. 

    --Rob

    Wednesday, September 21, 2011 11:42 PM
    Owner

All replies

  • Hi Jonathan,

    A custom Xaml control would not be rendered via DirectX but would be created using the Xaml and drawing primitives.  The basic idea is that you would create an object which inherits from Windows::UI::Xaml::Controls::Control and then implement whichever behavior is needed for the new control.  Expose it from a WinRT component DLL and it can be used from C#, VB, or C++ Xaml based apps.

    I'm not aware of any complete samples (yet!), but this will work essentially the same as creating a custom control for Silverlight or WPF so you can look at existing samples of those for inspiration. 

    --Rob

    Wednesday, September 21, 2011 11:42 PM
    Owner
  • Just so this is possible under WinRT (unlike the C++ dialect of Silverlight for Windows CE where it was impossible to inherit IXRControl, which pretty much made full custom controls impossible).
    Thursday, September 22, 2011 2:10 PM
  • What if I wanted to create one that I control the rendering of completely, not based on other elements? Is it even possible to go that deeply into it that way?
    Thursday, September 22, 2011 3:04 PM
  • Just speculating (based on extremely limited WPF experience) that there will be some sort of writable image object that will present you with an array of pixel values into which you can copy an arbitrary image.  You put such an image into your custom control and make it work. 
    Thursday, September 22, 2011 3:25 PM
  • But then I'm not really taking advantage of hardware acceleration (since all the existing controls use DirectX to render). Just wondering if there's some way to bridge that gap.
    Thursday, September 22, 2011 3:28 PM
  • If you don't want to use a control template then you can step up the chain and inherit from FrameworkElement instead. Either way you can completely customize the look of the control and don't have to assemble it from other elements.

    These base classes provide behavior that all controls need so that they can be placed in the visual tree alongside other controls and interact with the user, but your code would defined how your control renders and reacts to use input. 

    Friday, September 23, 2011 4:00 PM
    Owner
  • @Jonathan, it isn't possible for you to interop DX and XAML in the current developer preview build. This is an area of great interest to our (XAML) team and we're exploring ways on enabling this for the future. Currently you XAML based custom controls will need to use XAML for their rendering.
    Ashish Shetty, Program Manager, Microsoft | http://nerddawg.blogspot.com
    Wednesday, September 28, 2011 10:24 PM