WinRT vs. Silverlight - Part 5 - Defining default style template

See intro blogpost here.

Here's something that's actually NOT different in Silverlight (but it's different from WPF as it has always been).
You declare your style using DefaultStyleKey. This means the code looks like this for Silverlight, WPF and Windows Runtime:

public class MyControl : Control
{
    public MyControl()
    {
#if SILVERLIGHT || NETFX_CORE
        this.DefaultStyleKey = typeof(MyControl);
#endif
    }
    static MyControl() {
#if !SILVERLIGHT && !NETFX_CORE
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(HoverControl),
            new FrameworkPropertyMetadata(
            typeof(HoverControl))); 
#endif
    }
}

Here is what IS different though: You need to set the build action of \Themes\Generic.xaml to "Content". It won't work without it.
Also note that when you add Generic.xaml to your project, it will also get added to App.Xaml. I assume this is a bug in the current release, but you will have to go and delete this entry, or it won't work.

Comments (5) -


  • How would WinRT XAML application will be distribured?
    Will a limited CLR will be downloaded as well just like in silverlight?
    Thanks in advance.
  • Nounda: The apps will be destributed through the Windows Marketplace. There's no need to distribute a CLR. It comes pre-installed with Windows 8.

    Maxwell: That's not true. If you build C# or VB.NET apps in WinRT, you will use the EXACT same CLR that other .NET 4.5 apps are using. There is essentially no difference in that regard (they just limit what assemblies the CLR is allowed to talk to when running in Immersive mode).
    When you talk to the native libraries, a projection between unmanaged and managed space will happen, and again it's the CLR that handles all this for you.
  • Hi,
    I have a confusion over on this
    [ " You need to set the build action of \Themes\Generic.xaml to "Content". It won't work without it.
    Also note that when you add Generic.xaml to your project, it will also get added to App.Xaml. I assume this is a bug in the current release, but you will have to go and delete this entry, or it won't work
    .
    "].

    Whether we need to add the Generic.xaml contents in App.xaml or  Will it automatically added in App.xaml?

    If we define an another Control as the Sub Control of the main Control(for example MenuItem of Menu), it throws Type "[Sub Control Name]" was not found exception. How to resolve?
  • Any news about Default (Implicit) Data Templates?

Pingbacks and trackbacks (3)+

Add comment