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 Saturday, September 27, 2008 12:56 PM by jesseliberty
Filed under:

Comments

# re: Skinnable Custom Controls – Change to generic.xaml

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

Saturday, September 27, 2008 7:23 PM by scgm11

# Silverlight Cream for September 28, 2008 -- #381

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

Sunday, September 28, 2008 12:36 PM by Community Blogs

# re: Skinnable Custom Controls – Change to generic.xaml

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.

Sunday, September 28, 2008 1:12 PM by Odegaard

# Silverlight news for September 29, 2008

Pingback from  Silverlight news for September 29, 2008

Monday, September 29, 2008 11:54 AM by Silverlight news for September 29, 2008

# Trackback from SilverlightShow.net

Trackback from SilverlightShow.net

Monday, September 29, 2008 11:54 AM by Trackback from SilverlightShow.net

# Dependency Properties - Precedence

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

Thursday, October 02, 2008 1:39 PM by Jesse Liberty - Silverlight Geek

# Dependency Properties - Precedence

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

Thursday, October 02, 2008 1:48 PM by Microsoft Silverlight content

# Dependency Properties - Precedence

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

Thursday, October 02, 2008 2:38 PM by Mirrored Blogs

# Custom Controls – The Denouement

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

Thursday, October 09, 2008 9:48 AM by Jesse Liberty - Silverlight Geek

# Custom Controls – The Dénouement

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

Thursday, October 09, 2008 10:10 AM by Microsoft Weblogs

# Custom Controls – The Dénouement

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

Thursday, October 09, 2008 10:38 AM by Mirrored Blogs

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

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

Friday, October 10, 2008 6:15 AM by Custom Controls - the handling at Blog von J??rgen Ebner

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

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

# Creating Skinnable Custom Controls - Video

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

Friday, December 05, 2008 3:01 PM by Jesse Liberty - Silverlight Geek

# Creating Skinnable Custom Controls - Video

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

Friday, December 05, 2008 3:52 PM by Microsoft Weblogs

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

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

# Application Set: Templates & Custom Controls

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

Monday, December 22, 2008 5:34 PM by Jesse Liberty - Silverlight Geek

# Application Set: Templates & Custom Controls

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

Monday, December 22, 2008 6:17 PM by Microsoft Weblogs

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

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

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

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

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

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

# re: Skinnable Custom Controls – Change to generic.xaml

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

Monday, April 27, 2009 11:57 AM by bradutz01

# What’s New In Silverlight 3

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

Friday, May 29, 2009 4:57 PM by Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3

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

Friday, May 29, 2009 5:42 PM by Microsoft Weblogs

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

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

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

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