Microsoft XAML Toolkit CTP – July 2010: Silverlight Features

This post describes the features supporting Silverlight in the XAML Toolkit CTP – July 2010, announced here and downloadable here.

Silverlight Schema Context

The Silverlight Schema Context provided in the XAML toolkit allows inspection of Silveright XAML by tools that process XAML .Net 4.0 node streams.  For example: XAML validation in FxCop or loading into a XAML DOM with the XamlDomWriter.  The Silverlight Schema Context is not suitable for loading Silverlight content into the .Net 4.0 XamlObjectWriter().  A Silverlight runtime on .Net 4.0 is not available at this time.

Why is there a special Silverlight Schema Context?

The differences between Silverlight and standard XAML are small but problematic.  There are several differences.  One example is the internal implementation of “Binding” or “StaticResource” are different enough that the standard XAML Schema Context would be confused.  So when loading Silverlight content you should use a SilverlightSchemaContext.

Note: “standard XAML” is defined as XAML that System.Xaml would understand using the base class System.Xaml XamlSchemaContext.  By that standard WPF XAML has differences from standard XAML as well.

How does one create a Silverlight Schema Context?

Suppose you are writing an application and you are given some XAML to load and a collection of application assemblies that make up some application, and you want to load the assembly XAML into a XamlDOM.  The normal XAML code idiom is to create a schema context, a reader, a writer and transform the reader into the writer.

   1: public XamlDomNode LoadUiXaml(XmlReader xml, IList<Assembly> applicationAssemblies)
   2: {
   3:     XamlSchemaContext schema = GetRightSchemaContext(applicationAssemblies);
   4:     XamlXmlReader reader = new XamlXmlReader(xml, schema);
   5:     XamlDomWriter writer = new XamlDomWriter(schema);
   6:     XamlServices.Transform(reader, writer);
   7:     return writer.RootNode;
   8: }

In this example the application assemblies could be WPF or Silverlight so the XAML Toolkit provides a few APIs that help you create the correct Schema Context.

IsSilverlightAssembly()” will tell you if an assembly is a Silverlight Assembly.

When you have Silverlight application assemblies you should use a SilverlightSchemaContext.  To construct a SilverlightSchemaContext you will need have a complete list of reference assemblies including the Silverlight system reference assemblies.  Finding the right version of the Silverlight runtime reference assemblies requires knowledge of where Silverlight SDK installs things. The Toolkit provides another API to help with that.

CreateSilverlightSchemaContext()” will load the correct Silverlight system reference assemblies based on the application assemblies you provide.  The system assemblies are combined with your application assemblies and the Silverlight Schema Context is created for you:

   1: public XamlSchemaContext GetRightSchemaContext(IList<Assembly> applicationAssemblies)
   2: {
   3:     XamlSchemaContext schema = null;
   4:     if (SilverlightAssemblyHelper.IsSilverlightAssembly(applicationAssemblies[0]))
   5:     {
   6:         // Create a Silverlight Schema Context
   7:         schema = SilverlightAssemblyHelper.CreateSilverlightSchemaContext(applicationAssemblies);
   8:     }
   9:     else
  10:     {
  11:         // Use the WPF Schema Context.
  12:         schema = System.Windows.Markup.XamlReader.GetWpfSchemaContext();
  13:     }
  14:     return schema;
  15: }

Currently we support Silverlight versions: 3, 4 and Phone7.

This feature supersedes the UISchemaContext feature in the previous version of the toolkit.