<?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>Corey Schuman &#187; XAML</title>
	<atom:link href="http://www.85turns.com/tag/xaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.85turns.com</link>
	<description>Silverlight Consultant</description>
	<lastBuildDate>Tue, 26 Apr 2011 20:57:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>XAML guidelines and best practices</title>
		<link>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/</link>
		<comments>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 05:33:57 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[UI]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/</guid>
		<description><![CDATA[Everyone has their own opinion on how source code should be structured and formatted.&#160; What really matters when it comes to code is not saving the CPU a couple cycles, rather enabling others to read and understand what you’ve written.&#160; This comes from Martin Fowler’s classic book Refactoring: There is another user of your source [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone has their own opinion on how source code should be structured and formatted.&#160; What really matters when it comes to code is not saving the CPU a couple cycles, rather enabling others to read and understand what you’ve written.&#160; This comes from Martin Fowler’s classic book <a href="http://books.google.com/books?id=1MsETFPD3I0C&amp;printsec=frontcover">Refactoring</a>:</p>
<blockquote><p><font style="background-color: #ffffff">There is another user of your source code.&#160; Someone will try to read your code in a few month&#8217;s’ time to make some changes.&#160; We easily forget that extra user of the code, yet that user is actually the most important.&#160; Who cares if the computer takes a few more cycles to compile something?&#160; It does matter if it takes a programmer a week to make a change that would have taken only an hour if she had understood your code.</font></p>
</blockquote>
<p>When it comes to XAML, there aren’t too many opinions/articles talking about best practices (compared to other languages/markup).&#160; In fact, the definitive guide for XAML guidelines is Jaime Rodriguez’s (<a href="http://blogs.msdn.com/jaimer/default.aspx">site</a>) best practice web cast series and the resulting whitepaper (<a href="http://www.cookingwithxaml.com/content/xamlguidelines/xamlGuidelinesDraft0.pdf">pdf</a>).</p>
<blockquote><p><a href="http://blogs.msdn.com/jaimer/archive/2008/12/16/xaml-practices-series-part1.aspx">XAML practices series, part1</a></p>
<p><a href="http://channel9.msdn.com/shows/Continuum/XAML-Guidelines-Part-2/">XAML practices series, part2</a></p>
<p><a href="http://blogs.msdn.com/jaimer/archive/2009/01/13/xaml-guidelines-part-3-draft.aspx">XAML practices series, part3</a></p>
</blockquote>
<p>Jaime’s whitepaper is a must read for anyone in the Silverlight space.&#160; </p>
<p>Since my blog is less formal than a whitepaper, I’m able to give a stronger opinion on what I think are the best practices for XAML.&#160; So, without further ado here are my thoughts on XAML:</p>
<h2>Formatting</h2>
<p>First and foremost, I think formatting is the number one concern.&#160; </p>
<ul>
<li>Attributes should be on a separate line,</li>
<li>x:Name should come first,</li>
<li>and related attributes (such as HorizontalAlignment and VerticalAlignment) should be grouped.</li>
</ul>
<p>Take the below code blocks, Figure-1 and Figure-2, show the difference between having all the attributes on the same line versus on different lines.&#160; In my opinion, Figure-2 is much easier to digest than Figure-1.&#160; By having the x:Name first, you can quickly identify what the element is.&#160; Once you find the object you are looking for, you can then quickly identify related objects since they are grouped together (such as <font face="Courier New">RadiusX</font> and RadiusY, Width and Height, and HorizontalAlignment and VerticalAlignment).</p>
<p>The biggest complaint about Figure-2 is “wasted space”.&#160; The code inflates from 8 lines to 30; however, to Martin Fowler’s point, it’s not about you or the computer, it’s about other people being able to read and quickly assimilate to your code.</p>
<p><font face="Times New Roman" color="#c0c0c0" size="2">Figure-1</font>    <br /><a href="http://www.85turns.com/blog/wp-content/uploads/2009/02/image1.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="143" alt="image" src="http://www.85turns.com/blog/wp-content/uploads/2009/02/image-thumb.png" width="596" border="0" /></a>&#160;</p>
<p><font face="Times New Roman" color="#c0c0c0" size="2">Figure-2</font>    <br /><a href="http://www.85turns.com/blog/wp-content/uploads/2009/02/image2.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="527" alt="image" src="http://www.85turns.com/blog/wp-content/uploads/2009/02/image-thumb1.png" width="363" border="0" /></a> </p>
<h2>Naming conventions</h2>
<h3>Name everything</h3>
<p>Try to specify an x:Name attribute for every element in XAML.&#160; If nothing else, this will make you think if you really need the object.&#160; For example, at times it’s tempting to embed a panel within a panel to achieve a specific layout.&#160; In many instances I have caught myself doing this, mainly out of laziness.&#160; After refactoring I find that if I tweaked a margin or used a different kind of panel, there would have been no need for the extra panel.&#160; Forcing an x:Name on an object really makes you think of the purpose of that element.</p>
<h3>Describe elements</h3>
<p>Instead of pre or post-fixing element names, give them a descriptive name.&#160; Instead of:</p>
<ul>
<li>btnSubmit use SubmitButton,</li>
<li>grdContent use ContentPanel,</li>
<li>stkPnlNav use NavigationPanel.</li>
</ul>
<p>Programmers like using the pre-fix because you can quickly see all of the same type when using intellisense, and you can easily identify the type of an element.&#160; Admittedly, I come from this camp; however it’s time to change.&#160; A couple reasons for the change: 1.) designers will increasing be&#160; working in Blend and they are not going to be geeks like most of us and pre-fix with the type 2.)descriptive names are much easier to read and understand the cryptic typed names (think stkPnl=StackPanel or me=MediaElement).</p>
<p>Lastly, give all panels the same post-fix because you never know if you will have to change the type of a panel.&#160; I assure you, it’s an enormous pain, and large risk, to change the x:Name in a project.&#160; For example imagine having to change “BufferGrid” to “BufferCanvas” to “BufferStackPanel”.&#160; Not only do you have to change the XAML, you have to ensure all instances in the code are changed too.&#160; Better to go with the generic “BufferPanel” and be done with it.</p>
<h2>Disclaimer</h2>
<p>Best practices should be followed to the T; however, there are times when we’re in a crunch and the guidelines fall the way side.&#160; Best practices are designed to eliminate maintenance time and promote consistency.&#160; I am certainly guilty of not following the above guidelines, however it’s my goal to follow these consistently as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.85turns.com/2009/02/09/xaml-guidelines-and-best-practices/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Positioning in Silverlight 2</title>
		<link>http://www.85turns.com/2008/03/19/positioning-in-silverlight-2/</link>
		<comments>http://www.85turns.com/2008/03/19/positioning-in-silverlight-2/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 21:08:34 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Transforms]]></category>
		<category><![CDATA[x]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[y]]></category>

		<guid isPermaLink="false">http://simplesilverlight.wordpress.com/?p=38</guid>
		<description><![CDATA[A common mistake in Silverlight is for people to use the Canvas.Left and Canvas.Top property when positioning elements.  In Silverlight 2, there is a push towards a relative positioning model.  This makes the Margin, HorizontalAlignment, and VerticalAlignment very important (for all you CSS people the Margin starts on the Left and goes clockwise).  So, how [...]]]></description>
			<content:encoded><![CDATA[<p>A common mistake in Silverlight is for people to use the Canvas.Left and Canvas.Top property when positioning elements.  In Silverlight 2, there is a push towards a relative positioning model.  This makes the Margin, HorizontalAlignment, and VerticalAlignment very important (for all you CSS people the Margin starts on the Left and goes clockwise).  So, how do you position, move, or animate elements?  Where is the X and Y?  You can find them in the <b>TranslateTransform</b> which is part of the <b>TransformGroup</b> which is part of the <b>RenderTransform</b>.</p>
<p><b>XAML </b>(see below for this code to copy)<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/transforms.png" alt="Transform group" /><br />
This is a pain to access from C#.  You have to access the TranslateTransform through the Children collection using the index.  What happens if you switch the positions of the Transforms in XAML?  Your code will fail.  Here is the code to access X and Y through C# (again, see below for code to copy).<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/transforms_cs.png" alt="C Sharp translate transform" /><br />
So my question: Why not add the X, Y, ScaleX, and ScaleY to the UIElement?  This would eliminate the need to go through multiple castings in C#, it would prevent index failures if you change your transforms in XAML, and it would eliminate the misuse of the Canvas.Left and Canvas.Top properties.</p>
<p>Code to copy:</p>
<p>&lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;<br />
&lt;Grid.RenderTransform&gt;<br />
&lt;TransformGroup&gt;<br />
&lt;TranslateTransform X=&#8221;0&#8243; Y=&#8221;0&#8243; /&gt;<br />
&lt;ScaleTransform ScaleX=&#8221;1&#8243; ScaleY=&#8221;0&#8243; /&gt;<br />
&lt;RotateTransform Angle=&#8221;0&#8243; /&gt;<br />
&lt;SkewTransform AngleX=&#8221;0&#8243; AngleY=&#8221;0&#8243; /&gt;<br />
&lt;/TransformGroup&gt;<br />
&lt;/Grid.RenderTransform&gt;<br />
&lt;/Grid&gt;</p>
<p><img src="/Users/corey/AppData/Local/Temp/moz-screenshot.jpg" />             ((LayoutRoot.RenderTransform as TransformGroup).Children[0] as TranslateTransform).X;<br />
((LayoutRoot.RenderTransform as TransformGroup).Children[0] as TranslateTransform).Y;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.85turns.com/2008/03/19/positioning-in-silverlight-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Downloader object</title>
		<link>http://www.85turns.com/2007/10/11/downloader-object/</link>
		<comments>http://www.85turns.com/2007/10/11/downloader-object/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 14:25:09 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Downloader object]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://simplesilverlight.wordpress.com/2007/10/11/downloader-object/</guid>
		<description><![CDATA[What is the Downloader object and what can it do? This is one of the first questions I ran into while exploring Silverlight.  This object will retrieve objects (images, video, xaml) asynchronously, much like AJAX.  There is a gaping limitation being that it cannot go cross domain.  To get around this take a look at [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What is the Downloader object and what can it do?</strong></p>
<p>This is one of the first questions I ran into while exploring Silverlight.  This object will retrieve objects (images, video, xaml) asynchronously, much like AJAX.  There is a gaping limitation being that it cannot go cross domain. </p>
<p>To get around this take a look at Peter Kellner&#8217;s article below.  He develops a web service to go cross domain to get images, then his Silverlight application calls the web service running on the same machine.</p>
<p>An interesting feature of the Downloader object is the ability to get XAML asynchronously.  This gives us the ability to create module Silverlight applications and code XAML Plug-ins.</p>
<p>Links:<br />
For more information see the MSDN page <a href="http://msdn2.microsoft.com/en-us/library/bb232904.aspx">Using a Downloader Object</a>in Silverlight 1.0<br />
Peter Kellner&#8217;s use of the Downloder object to get images (<a href="http://peterkellner.net/2007/07/03/silverlightdownloader/">http://peterkellner.net/2007/07/03/silverlightdownloader/</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.85turns.com/2007/10/11/downloader-object/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

