Page view counter

Skinnable Custom Controls – Change to generic.xaml

This is the first in a series of explorations of breaking changes in the Release Candidate for Silverlight 2.  Please be sure to read Tim's blog entry about Silverlight RC0 for context and directions.

Summary

There are 2 significant breaking changes in how you handle the file generic.xaml when creating a Skinnable custom control in Silverlight 2.  This is a change from my blog post on 21st of September and the 12th of September and will be reflected in the forthcoming videos and tutorial.  The first change is that the file itself has a new location inside the Themes folder. The second is that the syntax has changed from declaring storyboards as resources and the referring to them in the declaration of Visual States, to combining the declaration of visual state with defining the associated storyboards.

The first change makes Silverlight more WPF-like.

The second change may or may not have been 100% intentional but will be in the release and I personally find it totally inoffensive.

Location, Location, Location

The file generic.xaml  has moved in RC0 from the root of the class library to a folder named Themes:

ThemesFolder

(Right click on Class Library and choose New Folder – name it Themes. Right click on the folder to choose New Item and create the user control or if you've already created it, drag it into the folder).

Syntax

The syntactic change is to remove the <Grid.Resources> section and to combine the definition of the Storyboards with the definition of the States. Here is the old syntax:

   1: <ResourceDictionary
   2:  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     &lt;Style TargetType="controls:StatusControl">
   4:         <Setter Property="Template">
   5:             <Setter.Value>
   6:                 <ControlTemplate TargetType="controls:StatusControl">
   7:                     <Grid x:Name="LayoutRoot">
   8:                          <Grid.Resources>
   9:                             <Storyboard x:Key="MouseOverState">
  10:                             </Storyboard>
  11:                             <Storyboard x:Key="OnState">
  12:                             </Storyboard>
  13:                         </Grid.Resources>
  14:                         <vsm:VisualStateManager.VisualStateGroups>
  15:                             <vsm:VisualStateGroup x:Name="CommonStates">
  16:                                 <vsm:VisualState x:Name="Normal" />
  17:                                 <vsm:VisualState x:Name="MouseOver" 
  18:                                       Storyboard="{StaticResource MouseOverState }" />
  19:                             </vsm:VisualStateGroup>
  20:                             <vsm:VisualStateGroup x:Name="StatusStates" >
  21:                                       Storyboard="{StaticResource OnState }" />
  22:                             </vsm:VisualStateGroup>
  23:                         </vsm:VisualStateManager.VisualStateGroups>
  24:                         <Ellipse x:Name="Core" >
  25:                             <Ellipse.RenderTransform>
  26:                             </Ellipse.RenderTransform>
  27:                             <Ellipse.Fill>
  28:                             </Ellipse.Fill>
  29:                         </Ellipse>
  30:                     </Grid>
  31:                 </ControlTemplate>
  32:             </Setter.Value>
  33:         </Setter>
  34:     </Style>
  35: </ResourceDictionary>   

 

 

 

 

I've left out a lot of detail, but what is essential here is that we were declaring the storyboards as resources, and then referring to them in the VisualStateManager.VisualStateGroups.  The new syntax is to combine the Visual State declarations with their associated storyboards, as we do when creating templates:

   1: <ResourceDictionary
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     &lt;Style TargetType="controls:StatusControl">
   4:         <Setter Property="Template">
   5:             <Setter.Value>
   6:                 <ControlTemplate TargetType="controls:StatusControl">
   7:                     <Grid x:Name="LayoutRoot">
   8:                       <vsm:VisualStateManager.VisualStateGroups>
   9:                             <vsm:VisualStateGroup x:Name="CommonStates">
  10:                                 <vsm:VisualState x:Name="Normal" />
  11:                                 <vsm:VisualState x:Name="MouseOver" >
  12:                                     <Storyboard >
  13:                                     </Storyboard>
  14:                                 </vsm:VisualState>
  15:                             </vsm:VisualStateGroup>
  16:                             <vsm:VisualStateGroup x:Name="StatusStates" >
  17:                                 <vsm:VisualState x:Name="OnState" >
  18:                                     <Storyboard >
  19:                                     </Storyboard>
  20:                                 </vsm:VisualState>
  21:                             </vsm:VisualStateGroup>
  22:                         </vsm:VisualStateManager.VisualStateGroups>
  23:                         <Ellipse x:Name="Core" >
  24:                     </Grid>
  25:                 </ControlTemplate>
  26:             </Setter.Value>
  27:         </Setter>
  28:     </Style>
  29: </ResourceDictionary>   

 

Notice there is no <Grid.Resources> area, as each <vsm:VisualState> is declared, if there is an associated storyboard it is placed within the VisualState definition.

Thanks.

-jesse

Published 27 September 2008 12:56 PM by jesseliberty
Filed under:

Comments

# scgm11 said on 27 September, 2008 07:23 PM

I cant see my custom controls in the silverlight app with the rc0, I changed generic.xaml to Themes on the same control library but didnt work (get a xaml parser error), if I put generic.xaml in Themes in my silverligt app assemlby it works fine, but nothing is shown. any idea??

# Community Blogs said on 28 September, 2008 12:36 PM

Bart Czernicki on RC0, Bill Reiss on RC0 Text, Shawn Wildermuth on WebClient, Expression Blend Team with

# Odegaard said on 28 September, 2008 01:12 PM

You >should< still be able to declare your storyboards as resources for your states. However a bug in RC0 prevents you from doing that. If you don't, you get an internal exception that's obviously a bug (and from what I've been told it's not intentional).

I do find this offensive - it's the pattern Microsoft had been promoting for some time, and it took me a great deal of reverse engineering of the Microsoft controls to find out what was different now.

# Silverlight news for September 29, 2008 said on 29 September, 2008 11:54 AM

Pingback from  Silverlight news for September 29, 2008

# Trackback from SilverlightShow.net said on 29 September, 2008 11:54 AM

Trackback from SilverlightShow.net

# Jesse Liberty - Silverlight Geek said on 02 October, 2008 01:39 PM

Today we wrap up dependency properties by talking about one of the most important, though typically invisible

# Microsoft Silverlight content said on 02 October, 2008 01:48 PM

Today we wrap up dependency properties by talking about one of the most important, though typically invisible

# Mirrored Blogs said on 02 October, 2008 02:38 PM

Today we wrap up dependency properties by talking about one of the most important, though typically invisible

# Jesse Liberty - Silverlight Geek said on 09 October, 2008 09:48 AM

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

# Microsoft Weblogs said on 09 October, 2008 10:10 AM

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

# Mirrored Blogs said on 09 October, 2008 10:38 AM

Over the past month I&#39;ve posted half a dozen min-articles about creating custom controls that can

# Custom Controls - the handling at Blog von J??rgen Ebner said on 10 October, 2008 06:15 AM

Pingback from  Custom Controls - the handling at Blog von J??rgen Ebner

# Silverlight made Simple - by Corey Schuman » Blog Archive » Video Slider update for RC0 said on 10 October, 2008 05:25 PM

Pingback from  Silverlight made Simple - by Corey Schuman  &raquo; Blog Archive   &raquo; Video Slider update for RC0

# Jesse Liberty - Silverlight Geek said on 05 December, 2008 03:01 PM

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

# Microsoft Weblogs said on 05 December, 2008 03:52 PM

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

# Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF & .NET said on 08 December, 2008 04:07 PM

Pingback from  Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF &amp; .NET

# Jesse Liberty - Silverlight Geek said on 22 December, 2008 05:34 PM

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

# Microsoft Weblogs said on 22 December, 2008 06:17 PM

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

# Templates & Custum Controls at Programming with Silverlight, WPF & .NET said on 23 December, 2008 08:30 AM

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Templates & Custum Controls at Programming with Silverlight, WPF & .NET said on 23 December, 2008 08:32 AM

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Creating Custom Controls ??? A Common Starter Application - Jesse Liberty - Silverlight Geek said on 01 January, 2009 06:20 PM

Pingback from  Creating Custom Controls ??? A Common Starter Application - Jesse Liberty - Silverlight Geek

# bradutz01 said on 27 April, 2009 11:57 AM

Hi, at www.xamltemplates.net/sl you can find a complete theme for all the Silverlight 2.0 controls, check it out.

# Jesse Liberty - Silverlight Geek said on 29 May, 2009 04:57 PM

&#160; &#160; I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and

# Microsoft Weblogs said on 29 May, 2009 05:42 PM

I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and then again

# Programming with Silverlight, WPF & .NET » Was ist neu in Silverlight 3 said on 05 June, 2009 06:26 AM

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in Silverlight 3

# Top-silverlight » Blog Archive » What???s New In Silverlight 3 said on 06 June, 2009 07:46 AM

Pingback from  Top-silverlight  &raquo; Blog Archive   &raquo; What???s New In Silverlight 3