Page view counter

Jesse Liberty - Silverlight Geek

More Signal Less Noise

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

Comments

scgm11 said:

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??

# September 27, 2008 7:23 PM

Community Blogs said:

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

# September 28, 2008 12:36 PM

Odegaard said:

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.

# September 28, 2008 1:12 PM

Silverlight news for September 29, 2008 said:

Pingback from  Silverlight news for September 29, 2008

# September 29, 2008 11:54 AM

Trackback from SilverlightShow.net said:

Trackback from SilverlightShow.net

# September 29, 2008 11:54 AM

Jesse Liberty - Silverlight Geek said:

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

# October 2, 2008 1:39 PM

Microsoft Silverlight content said:

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

# October 2, 2008 1:48 PM

Mirrored Blogs said:

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

# October 2, 2008 2:38 PM

Jesse Liberty - Silverlight Geek said:

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

# October 9, 2008 9:48 AM

Microsoft Weblogs said:

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

# October 9, 2008 10:10 AM

Mirrored Blogs said:

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

# October 9, 2008 10:38 AM

Custom Controls - the handling at Blog von J??rgen Ebner said:

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

# October 10, 2008 6:15 AM

Silverlight made Simple - by Corey Schuman » Blog Archive » Video Slider update for RC0 said:

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

# October 10, 2008 5:25 PM