<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael Shim &#187; XamlDom</title>
	<atom:link href="http://michaelshim.com/blog/tag/xamldom/feed/" rel="self" type="application/rss+xml" />
	<link>http://michaelshim.com/blog</link>
	<description>XAML, WPF &#38; Silverlight</description>
	<lastBuildDate>Thu, 18 Mar 2010 21:59:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XAML Toolkit CTP</title>
		<link>http://michaelshim.com/blog/2009/11/19/xaml-toolkit-ctp/</link>
		<comments>http://michaelshim.com/blog/2009/11/19/xaml-toolkit-ctp/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 22:00:37 +0000</pubDate>
		<dc:creator>Michael Shim</dc:creator>
				<category><![CDATA[XAML]]></category>
		<category><![CDATA[PDC]]></category>
		<category><![CDATA[XAML Toolkit]]></category>
		<category><![CDATA[XamlDom]]></category>

		<guid isPermaLink="false">http://michaelshim.com/blog/?p=11</guid>
		<description><![CDATA[At PDC, we&#8217;re announcing a CTP of the XAML Toolkit.&#160; This should make analysis of XAML for both .NET &#38; Silverlight very easy to do.&#160; We&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>At PDC, we&#8217;re announcing a CTP of the XAML Toolkit.&#160; This should make analysis of XAML for both .NET &amp; Silverlight very easy to do.&#160; We&#8217;re adding some preliminary FxCop integration for XAML and allowing you to write your own custom rules.</p>
<p>Our talk will be published <a href="http://microsoftpdc.com/Sessions/CL24">here</a> in the near future and you can go through our presentation.</p>
<p>The components that are shipping as part of the November 2009 CTP are:</p>
<ul>
<li>XamlDom &#8211; A XAML DOM that is LINQ friendly.&#160; Enables easy static analysis. </li>
<li>XAML FxCop integration &#8211; A BaseXamlRule implementation that allows you to write custom FxCop rules that target XAML.&#160; We&#8217;re also shipping a couple of simple ones including a ValidationRule that will validate your XAML. </li>
<li>SilverlightSchemaContext &#8211; A XamlSchemaContext that allows System.Xaml to parse Silverlight XAML for tools use.&#160; </li>
<li>UISchemaContext &#8211; A XamlSchemaContext that allows you to go between .NET &amp; Silverlight XAML.&#160; This allows you to write custom FxCop rules that can be written against one framework but works against both platforms. </li>
</ul>
<p>I’ll dive into more detail on each of the components in the next few weeks.</p>
<p>Here&#8217;s a simple example of going through a document and writing out all the types used in the file:</p>
<p>&#160;</p>
<pre class="code"><span style="color: #2b91af">XamlDomObject </span>rootObject = <span style="color: #2b91af">XamlDomServices</span>.Load(<span style="color: #a31515">&quot;Window1.xaml&quot;</span>);

<span style="color: blue">foreach </span>(<span style="color: #2b91af">XamlDomObject </span>domObject <span style="color: blue">in </span>rootObject.DescendantsAndSelf())
{
  <span style="color: #2b91af">Console</span>.WriteLine(domObject.Type);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<p>By calling XamlDomServices.Load, we get a XamlDomObject for the root object in the XAML document.&#160; From there, we can call DescendantsAndSelf on the root object which returns an IEnumerable&lt;XamlDomObject&gt; that you can loop through.</p>
<p>Here&#8217;s another example.&#160; Imagine you want to set Background on every single Control in your document but only the ones that don&#8217;t already have one set currently:</p>
<p>&#160;</p>
<pre class="code"><span style="color: #2b91af">XamlDomObject </span>rootObject = <span style="color: #2b91af">XamlDomServices</span>.Load(<span style="color: #a31515">&quot;Window1.xaml&quot;</span>);
<span style="color: blue">foreach </span>(<span style="color: #2b91af">XamlDomObject </span>objectNode <span style="color: blue">in
        from </span>control <span style="color: blue">in </span>rootObject.DescendantsAndSelf(<span style="color: blue">typeof</span>(<span style="color: #2b91af">Control</span>))
        <span style="color: blue">where </span>!control.HasMember(<span style="color: #a31515">&quot;Background&quot;</span>)
        <span style="color: blue">select </span>control)
{
  objectNode.SetMemberValue(<span style="color: #a31515">&quot;Background&quot;</span>, <span style="color: #a31515">&quot;Red&quot;</span>);
}
<span style="color: #2b91af">XamlDomServices</span>.Save(rootObject, <span style="color: #a31515">&quot;NewFile.xaml&quot;</span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><code></code></p>
<p>Like before, we&#8217;re calling DescendantsAndSelf but this time we&#8217;re passing in typeof(Control).&#160; This will limit it to return only things that are assignable to Control.&#160; We&#8217;re also using LINQ to be able to get the XamlDomObjects we want faster.&#160; After we call DescendantsAndSelf, we&#8217;ll then call &#8216;where !control.HasMember(&quot;Background&quot;)&#8217;.&#160; This limits it to only objects that don&#8217;t have a member called &quot;Background&quot; set on it currently. We select only those Controls.&#160; We then call SetMemberValue(&quot;Background&quot;, &quot;Red&quot;) to set the background property to Red.&#160; Finally, we call XamlDomServices.Save to save the file back out to XAML.&#160; This is a pretty simple example of a transformation but we can do much more complex transformations with our XamlDom.</p>
<p>Over the next few weeks, I&#8217;m going to try to post a couple of examples of using the XamlDom and FxCop.&#160; If you have any requests, please feel free to leave a comment/message.</p>
<p>The XAML Toolkit CTP can be found <a href="http://code.msdn.microsoft.com/XAML">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://michaelshim.com/blog/2009/11/19/xaml-toolkit-ctp/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
