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

by Morten 17. September 2011 19:38

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.

Tags:

Windows Runtime | WPF vs Silverlight | WPF | Silverlight

Comments (5) -

Nounda
Nounda Israel
9/18/2011 10:53:23 AM #


How would WinRT XAML application will be distribured?
Will a limited CLR will be downloaded as well just like in silverlight?
Thanks in advance.

Maxwell Cabral
Maxwell Cabral United States
9/26/2011 11:32:10 AM #

There is no CLR in WinRT, it's native code.

Morten
Morten United States
9/26/2011 11:36:21 AM #

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.

Karthick
Karthick India
12/2/2011 3:59:10 AM #

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?

Lex Lavnikov
Lex Lavnikov Germany
12/15/2011 7:18:57 AM #

Any news about Default (Implicit) Data Templates?

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Morten Nielsen

Silverlight MVP

Morten Nielsen
<--That's me
E-mail me Send mail

Twitter @dotMorten 

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2005-2014

Month List

Recent Comments

Comment RSS