RSS Feed


Keeping XAML Clean

One of the early goals we had we Family.Show was to make the XAML files easy to read and understandable. I want to share the list of some of the little things that we did to keep our XAML clean.  I will be using the MainWindow.xaml page as an example.

Clean <Window> Tag

We placed the xmlns (xml namespace references) attributes first and in separate lines. Like the "using" section in .cs files, this keeps them organized.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Microsoft.FamilyShowLib="clr-namespace:Microsoft.FamilyShowLib;assembly=FamilyShowLib"
xmlns:local="clr-namespace:Microsoft.FamilyShow"
x:Class="Microsoft.FamilyShow.MainWindow"
Title="Family.Show" Height="728" Width="960"
Background="{DynamicResource MainBackgroundBrush}"
ResizeMode="CanResizeWithGrip">

Extensive Comments

Almost every code section will have a corresponding comment above it.  We did this for two reasons. Visual Studio display comments as green text, which makes it really easy to scan the XAML for the tag that we want to modify.  People new to the code will have an easier time understanding the XAML structure from looking the comments instead of the XAML tags.

x:Name

Although we will not reference all of the controls in our code behind, we would still name all of our controls.  We always use x:Name instead of the Name attribute for consistency as not all controls support the Name property. We keep the x:Name as the first attribute again for consistency.

The naming help describes their function and why the control is there.  This is especially helpful when it comes to picking the right controls to style in Expression Blend using the Objects and Timeline panel with the Property panel.

image

Use User Controls

User Controls are not only great for code-reuse; They are a great organization tool. With User Controls, we abstracted functionality into their own XAML file.  It's like refactoring code into methods.

      <!-- New User Control -->
      <local:NewUserControl x:Name="NewUserControl" ... />

      <!-- Welcome User Control -->
      <local:Welcome x:Name="WelcomeUserControl" ... />

      <!-- Person Info Control -->
      <local:PersonInfo x:Name="PersonInfoControl" ... />

      <!-- Family Data Control -->
      <local:FamilyData x:Name="FamilyDataControl" .. />

Resource Dictionaries

We kept brushes, styles, and templates in separate resource dictionary files.  Think of it as using a separate cascading style sheet in HTML. The look and feel is separated from the content.  We could've done a little better by removing all inline styling in Family.Show.

By using Resource Dictionaries we were also able to implement the Skinning functionality by simply replacing the Resource Dictionary in code.

Structured indents and spacing

Like HTML and code, indents and spacing are a great way to keep XAML clean.  The auto-format feature in Visual Studio really helps with this.

Well, that about sums it up

Hope this helps,

Alan

 
Posted by Alan Le | 14 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Friday, 8 Aug 2008 05:03 by zvzdmjme
<div class=ExternalClass3E4FFA1A1C554D9C953C61F247FE161C> [URL=http://zrmbciya.com]epauwfuf[/URL] hhevxkbk http://rchuwktd.com llxamhvf yjltzoxt <a href="http://txjpiait.com">ysaayuns</a> </div>

Sunday, 10 Aug 2008 06:23 by hoodia online
<div class=ExternalClass964659951CD6407D80B23E7B79A40ED9>Sorrow was like the wind. It came in gusts.</div>

Sunday, 10 Aug 2008 10:14 by redoubling
<div class=ExternalClassD31D0E6216EC4423ACF9249E24CA73DF>What makes the engine go? Desire, desire, desire.</div>

Monday, 11 Aug 2008 01:53 by order diazepam casing mangold
<div class=ExternalClassE58D375A0CAF4701AE8F80DDA732EA46>The only difference between a rut and a grave... is in their dimensions.</div>

Monday, 11 Aug 2008 05:49 by buy wellbutrin
<div class=ExternalClassD9F8C32BD3CA4990AB92E43A23FE4151>Famous remarks are very seldom quoted correctly.</div>

Monday, 11 Aug 2008 09:19 by buy viagra
<div class=ExternalClass3773BC46E2D34DE0B7A48BD9B2D8D9EC>Having a holiday weekend without a family member felt like putting on a sweater that had an extra arm.</div>

Monday, 11 Aug 2008 01:24 by premarin
<div class=ExternalClassB145696D21974C1C972B5054D0E467E6>So little time and so little to do.</div>

Monday, 11 Aug 2008 05:39 by zocor
<div class=ExternalClassFD0881BB23104C788D84B9BCC5971366>Reality is that which, when you stop believing in it, doesn't go away.</div>

Monday, 11 Aug 2008 10:01 by zopiclone
<div class=ExternalClass3883613CC418434F9253BB58917EF719>Man is the only animal that can remain on friendly terms with the victims he intends to eat until he eats them.</div>

Tuesday, 12 Aug 2008 02:07 by acumen
<div class=ExternalClass9EEEC5C0127848769C64B9FBD6F50ED2>Things won are done; joy's soul lies in the doing.</div>

Tuesday, 12 Aug 2008 06:00 by fust
<div class=ExternalClass1F2E6280843C47B0988468C2CC3FC106>We succeed only as we identify in life, or in war, or in anything else, a single overriding objective, and make all other considerations bend to that one objective.</div>

Tuesday, 12 Aug 2008 09:19 by souesite
<div class=ExternalClass7AC4707BA4324875987A443523503C81>Skiing combines outdoor fun with knocking down trees with your face.</div>

Friday, 19 Sep 2008 05:22 by Casino
<div class=ExternalClass556B3E80B2BE4F7790E139509EC904A1> The New Dress summary and study guide, with notes, essays, quotes, and pictures. </div>

Saturday, 20 Sep 2008 09:26 by Casino
<div class=ExternalClass66DB3FCF5EF64475BC0D9E3637716C14> The New Dress summary and study guide, with notes, essays, quotes, and pictures. </div>

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation