silverlight and wpf --DefaultStyleKey

http://www.sharpgis.net/post/2011/09/17/WinRT-vs-Silverlight-Part-5-Defining-default-style-template.aspx

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.

posted @ 2013-09-24 20:23  kong326  阅读(258)  评论(0)    收藏  举报