At PDC, we’re announcing a CTP of the XAML Toolkit. This should make analysis of XAML for both .NET & Silverlight very easy to do. We’re adding some preliminary FxCop integration for XAML and allowing you to write your own custom rules.
Our talk will be published here in the near future and you can go through our presentation.
The components that are shipping as part of the November 2009 CTP are:
- XamlDom – A XAML DOM that is LINQ friendly. Enables easy static analysis.
- XAML FxCop integration – A BaseXamlRule implementation that allows you to write custom FxCop rules that target XAML. We’re also shipping a couple of simple ones including a ValidationRule that will validate your XAML.
- SilverlightSchemaContext – A XamlSchemaContext that allows System.Xaml to parse Silverlight XAML for tools use.
- UISchemaContext – A XamlSchemaContext that allows you to go between .NET & Silverlight XAML. This allows you to write custom FxCop rules that can be written against one framework but works against both platforms.
I’ll dive into more detail on each of the components in the next few weeks.
Here’s a simple example of going through a document and writing out all the types used in the file:
XamlDomObject rootObject = XamlDomServices.Load("Window1.xaml"); foreach (XamlDomObject domObject in rootObject.DescendantsAndSelf()) { Console.WriteLine(domObject.Type); }
By calling XamlDomServices.Load, we get a XamlDomObject for the root object in the XAML document. From there, we can call DescendantsAndSelf on the root object which returns an IEnumerable<XamlDomObject> that you can loop through.
Here’s another example. Imagine you want to set Background on every single Control in your document but only the ones that don’t already have one set currently:
XamlDomObject rootObject = XamlDomServices.Load("Window1.xaml"); foreach (XamlDomObject objectNode in from control in rootObject.DescendantsAndSelf(typeof(Control)) where !control.HasMember("Background") select control) { objectNode.SetMemberValue("Background", "Red"); } XamlDomServices.Save(rootObject, "NewFile.xaml");
Like before, we’re calling DescendantsAndSelf but this time we’re passing in typeof(Control). This will limit it to return only things that are assignable to Control. We’re also using LINQ to be able to get the XamlDomObjects we want faster. After we call DescendantsAndSelf, we’ll then call ‘where !control.HasMember("Background")’. This limits it to only objects that don’t have a member called "Background" set on it currently. We select only those Controls. We then call SetMemberValue("Background", "Red") to set the background property to Red. Finally, we call XamlDomServices.Save to save the file back out to XAML. This is a pretty simple example of a transformation but we can do much more complex transformations with our XamlDom.
Over the next few weeks, I’m going to try to post a couple of examples of using the XamlDom and FxCop. If you have any requests, please feel free to leave a comment/message.
The XAML Toolkit CTP can be found here.
Pingback: XAML UI (WPF + Silverlight) Momentum shown at PDC09! - Rob Relyea - XAMLified
Trackback: Identity Interface
Pingback: Microsoft PDC09 and Silverlight Round-up
Pingback: XAML Toolkit CTP = FxCopXaml + XamlDom + System.Xaml.dll support for Silverlight XAML - Rob Relyea - XAMLified
Pingback: TWC9: XAML tools, Silverlight for Live Writer, Surface SDK, | CHARGED's Digital Lifestyle at Work or Play
Pingback: Une absence prolongée prenant fin en ce premier jour des Techdays 2010 « John's Technical Weblog
#1 by Notre on March 1, 2010 - 3:29 pm
Quote
I don’t know if you’re still taking request, but if you are….
I’d like see an example of an FxCop rule that uses XAML toolkit to check a part of a custom extension to see if that part exists. This probably sounds a bit vague, but I think it’s much more clearly explained here:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/92a3b1c1-868e-4449-8b51-0f7b98f8ff97
Thanks! (Bonus points: if this could be done from compilation within the VS build process, that’d be even better than a FxCop rule
#2 by WP Themes on March 13, 2010 - 11:33 am
Quote
Nice brief and this enter helped me alot in my college assignement. Thank you on your information.