XAML Schema Language
As mentioned in previous post WF v4.0 is enhanced in many ways and one of those is “Complete workflow/activity authoring in XAML”.
A key enhancement to support this feature is the addition of Schema support in XAML. While XAML is used to represent objects but there was no way to specify type information in XAML. Schema support provides exactly this functionality using XAML vocabulary. Let’s explore this feature. In the PDC bits XAML Schema functionality is available in System.XAML.Runtime.dll assembly in (System.Runtime.Xaml.Schema) namespace.
Let’s create a simple type in c#.
public class Employee
{
public string Email { get; set; }
public bool SendEmail() { return true; }
public event EventHandler NewMessage;
}
Now this same type can be created in XAML using XAML Schema language. But here I will use a slightly different approach and will convert above type to its XAML schema representation using following simple code.
static void Main(string[] args)
{
1. var resolver = new XamlSchemaTypeResolver();
2. SchemaType st =resolver.GenerateSchemaTypes(Assembly.GetExecutingAssembly()).First();
3. string xaml = XamlServices.Save(st);
}
What I get back is this:
<p:SchemaType BaseType="p:Object" Name="p1:Employee"
xmlns:p="http://schemas.microsoft.com/netfx/2008/xaml/schema"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<p:SchemaProperty Name="Email" Type="p:String" />
<p:SchemaMethod Name="SendEmail">
<p:Signature ReturnType="p:Boolean" />
</p:SchemaMethod>
<p:SchemaEvent Name="NewMessage">
<p:SchemaEvent.Signature>
<p:Signature ReturnType="{x:Null}">
<p:SignatureParameter Direction="In" Name="sender" Type="p:Object" />
<p:SignatureParameter Direction="In" Name="e" Type="s:EventArgs" />
</p:Signature>
</p:SchemaEvent.Signature>
</p:SchemaEvent>
</p:SchemaType>
The SchemaType object on line 2 has an attached property which link it back to the type from which this object was generated.
This exactly same language is used to declare arguments/data flow when defining workflow/activities in XAML. Schema language provided the solution to construct new types in XAML without falling back to code which was one of major hurdle in the authoring of XAML only workflows in V1.