<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Dev Girl's World</title><subtitle type="html">by Agata Staniak&lt;br/&gt;
&lt;br/&gt;
&lt;i&gt;Discovering the beauty of Silverlight development&lt;/i&gt;</subtitle><id>http://blogs.msdn.com/agatastaniak/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/agatastaniak/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/agatastaniak/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-12-21T04:50:00Z</updated><entry><title>Silverlight Basics #3: All roads lead to Rome - mixing XAML and C#</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/agatastaniak/archive/2008/12/30/Silverlight-Basics-3.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="5744" href="http://blogs.msdn.com/agatastaniak/attachment/9256600.ashx" /><id>http://blogs.msdn.com/agatastaniak/archive/2008/12/30/Silverlight-Basics-3.aspx</id><published>2008-12-30T07:37:00Z</published><updated>2008-12-30T07:37:00Z</updated><content type="html">&lt;P class=justified&gt;After having read &lt;A title="Silverlight Basics #2: The anatomy of a Hello world application" href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx" mce_href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx" alt="Silverlight Basics #2: The anatomy of a Hello world application"&gt;part 2 of this tutorial&lt;/A&gt; you know how to create a very simple Silverlight application in XAML, displaying some text in the browser. Let's see now how XAML actually works together with C#.&lt;/P&gt;
&lt;P&gt;After having read this part you will:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;know how to add content to your application both in XAML and in C# code, &lt;/LI&gt;
&lt;LI&gt;understand how XAML works together with C#, &lt;/LI&gt;
&lt;LI&gt;know when to use XAML and when C# in your application. &lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Let's get started!&lt;/H2&gt;
&lt;P class=justified&gt;One thing you will immediately notice during your Silverlight adventure is that you can very often do the same thing in many different ways and using different tools. This is great news because everyone have their habits and preferred programming methods, and it is important the tools allow everyone do their job the way they like.&lt;/P&gt;
&lt;H2&gt;XAML vs C#&lt;/H2&gt;
&lt;P class=justified&gt;Remember our &lt;A title=HelloWorld.zip href="http://blogs.msdn.com/agatastaniak/attachment/9245703.ashx" alt="HelloWorld.zip"&gt;"Hello World"&lt;/A&gt; application from the &lt;A title="Silverlight Basics #2: The anatomy of a Hello world application" href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx" mce_href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx" alt="Silverlight Basics #2: The anatomy of a Hello world application"&gt;the previous part&lt;/A&gt;? You didn't write a single line of C# code, everything was written in XAML. Open the &lt;STRONG&gt;Page.xaml&lt;/STRONG&gt; file. The code looks like this:&lt;/P&gt;
&lt;P class=code&gt;&amp;lt;UserControl x:Class="HelloWorld.Page" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Width="400" Height="300"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Grid x:Name="LayoutRoot" Background="Yellow"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;TextBlock Text="Hello World!" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VerticalAlignment="Center" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HorizontalAlignment="Center" /&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Grid&amp;gt; &lt;BR&gt;&amp;lt;/UserControl&amp;gt;&lt;/P&gt;
&lt;P class=justified&gt;Remove the TextBlock from XAML now:&lt;/P&gt;
&lt;P class=code&gt;&amp;lt;UserControl x:Class="HelloWorld.Page" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Width="400" Height="300"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Grid x:Name="LayoutRoot" Background="Yellow"&amp;gt;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Grid&amp;gt; &lt;BR&gt;&amp;lt;/UserControl&amp;gt; &lt;/P&gt;
&lt;P class=justified&gt;You'll notice that the "Hello World!" text is now gone in the designer preview in Visual Studio. And indeed, it is gone when you run the application. Switch to the &lt;STRONG&gt;Page.xaml.cs&lt;/STRONG&gt; file now, and put the following code in the Page constructor:&lt;/P&gt;
&lt;P class=code&gt;public Page() &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InitializeComponent(); &lt;BR&gt;&lt;SPAN class=marker&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TextBlock text = new TextBlock() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = "Hello World!", &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HorizontalAlignment = HorizontalAlignment.Center, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VerticalAlignment = VerticalAlignment.Center &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LayoutRoot.Children.Add(text);&lt;/SPAN&gt; &lt;BR&gt;}&lt;/P&gt;
&lt;P class=justified&gt;You just replaced some XAML with some equivalent C# code. You still cannot see the "Hello World!" text in the preview of the &lt;STRONG&gt;Page.xaml&lt;/STRONG&gt; file, but when you hit &lt;STRONG&gt;F5&lt;/STRONG&gt;, you can see the text is displayed as it was before. That's because the designer preview only parses XAML code and displays its preview. It doesn't execute the entire constructor of your control. But how is XAML actually parsed? Let's have a closer look at this.&lt;/P&gt;
&lt;H2&gt;XAML behind the scenes&lt;/H2&gt;
&lt;P class=justified&gt;Whenever you're dealing with a class that has both XAML and C# code, there's the &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; method that should be called in the very first line of the class's constructor. And indeed, it is there for you if you create a new class with the Visual Studio wizard. It is automatically generated for you by Visual Studio whenever you modify the XAML code. If you want to see where exactly this method is defined, turn on the &lt;STRONG&gt;Show All Files&lt;/STRONG&gt; option in your project. In the &lt;STRONG&gt;obj/Debug&lt;/STRONG&gt; subdirectory of the project you can find these two files: &lt;STRONG&gt;App.g.cs&lt;/STRONG&gt; and &lt;STRONG&gt;Page.g.cs&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P style="TEXT-ALIGN: center"&gt;&lt;IMG title="All files of the HelloWorld project, including the hidden ones" style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=422 alt="All files of the HelloWorld project, including the hidden ones" src="http://blogs.msdn.com/blogfiles/agatastaniak/WindowsLiveWriter/SilverlightBasics3Addingmorecontent_8AF/shot6_3.png" width=260 border=0 mce_src="http://blogs.msdn.com/blogfiles/agatastaniak/WindowsLiveWriter/SilverlightBasics3Addingmorecontent_8AF/shot6_3.png"&gt; &lt;/P&gt;
&lt;P class=justified&gt;These are auto generated files, one for each class that contains XAML. If you open &lt;STRONG&gt;Page.g.cs&lt;/STRONG&gt;, you will see it contains partial definition of the &lt;STRONG&gt;Page&lt;/STRONG&gt; class. The &lt;STRONG&gt;Page &lt;/STRONG&gt;class contains the definition of the &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; method. It also has a field for every element that has the &lt;STRONG&gt;x:Name&lt;/STRONG&gt; attribute in the &lt;STRONG&gt;Page.xaml&lt;/STRONG&gt; file. For example, it contains an internal field called &lt;STRONG&gt;LayoutRoot&lt;/STRONG&gt; that is of type &lt;STRONG&gt;Grid&lt;/STRONG&gt;, as defined in XAML:&lt;/P&gt;
&lt;P class=code&gt;&amp;lt;UserControl x:Class="HelloWorld.Page" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Width="400" Height="300"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Grid x:Name="LayoutRoot" Background="Yellow"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Grid&amp;gt; &lt;BR&gt;&amp;lt;/UserControl&amp;gt;&lt;/P&gt;
&lt;P class=justified&gt;What &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; does is: it takes all controls defined in XAML, creates instances of them and sets all the properties you set in XAML. It does all the same you would do if you wrote the C# code manually. In our example when an instance of the &lt;STRONG&gt;Page&lt;/STRONG&gt; class is created, &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; sets Page's &lt;STRONG&gt;Width&lt;/STRONG&gt; and &lt;STRONG&gt;Height&lt;/STRONG&gt; properties. It also initializes the &lt;STRONG&gt;LayoutRoot&lt;/STRONG&gt; field with a new instance of the &lt;STRONG&gt;Grid&lt;/STRONG&gt; class, and sets its background to yellow. And because the &lt;STRONG&gt;Grid&lt;/STRONG&gt; control is inside of the &lt;STRONG&gt;Page&lt;/STRONG&gt; control in XAML, the &lt;STRONG&gt;Grid&lt;/STRONG&gt; control is set as the &lt;STRONG&gt;Content&lt;/STRONG&gt; property of the &lt;STRONG&gt;Page&lt;/STRONG&gt; control.&lt;/P&gt;
&lt;P class=justified&gt;Now try commenting out the &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; method call in Page's constructor (in the &lt;STRONG&gt;Page.xaml.cs&lt;/STRONG&gt; file). When you run the application it crashes with a NullReferenceException at this line:&lt;/P&gt;
&lt;P class=code&gt;LayoutRoot.Children.Add(text);&lt;/P&gt;
&lt;P class=justified&gt;If you debug the code you can see it crashes because &lt;STRONG&gt;LayoutRoot&lt;/STRONG&gt; is &lt;STRONG&gt;null&lt;/STRONG&gt;. &lt;STRONG&gt;LayoutRoot &lt;/STRONG&gt;has never been initialized because &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; has never been called.&lt;/P&gt;
&lt;P class=justified&gt;If you want to get rid of all the XAML code and the &lt;STRONG&gt;InitializeComponent&lt;/STRONG&gt; method call in your Page control, you can translate all your XAML to C#. You can do it in the &lt;STRONG&gt;Page.xaml.cs&lt;/STRONG&gt; file like this:&lt;/P&gt;
&lt;P class=code&gt;&lt;SPAN class=marker&gt;public class Page : UserControl&lt;/SPAN&gt; &lt;BR&gt;{&amp;nbsp;&amp;nbsp; &lt;BR&gt;&lt;SPAN class=marker&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; internal Grid LayoutRoot;&lt;/SPAN&gt; &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Page() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp; &lt;BR&gt;&lt;SPAN class=marker&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Width = 400; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Height = 300; &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LayoutRoot = new Grid() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Background = new SolidColorBrush(Colors.Yellow) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Content = LayoutRoot;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TextBlock text = new TextBlock() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = "Hello World!", &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HorizontalAlignment = HorizontalAlignment.Center, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VerticalAlignment = VerticalAlignment.Center &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LayoutRoot.Children.Add(text); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/P&gt;
&lt;P class=justified&gt;Now everything is in C# and we don't use XAML at all (the content from the &lt;STRONG&gt;Page.xaml&lt;/STRONG&gt; file is never instantiated). Notice now the entire class is defined just in one place and is not marked as &lt;STRONG&gt;partial&lt;/STRONG&gt; anymore. When you used XAML, the class had to be defined as partial, because the other part of the class was defined in the auto generated &lt;STRONG&gt;Page.g.cs&lt;/STRONG&gt; file.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P class=justified&gt;&lt;EM&gt;If you want the above code to work in your project, you have to also comment out the partial Page class definition in the Page.g.cs file. Otherwise Visual Studio will complain the class is defined twice. Of course, if you modify the Page.xaml file, the Page.g.cs file will get regenerated and your code will stop working again. If you want to get rid of XAML permanently, you should remove the Page.xaml file from the project while keeping the Page.xaml.cs file. If you try to do it in Visual Studio, you'll notice these two files are "grouped" together and you cannot just remove the *.xaml file. To "ungroup" them, you have to open the project file HelloWorld.csproj in a text editor, find this part:&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;lt;Compile Include="Page.xaml.cs"&amp;gt; &lt;BR&gt;&amp;nbsp; &amp;lt;DependentUpon&amp;gt;Page.xaml&amp;lt;/DependentUpon&amp;gt; &lt;BR&gt;&amp;lt;/Compile&amp;gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;and delete the &amp;lt;DependentUpon&amp;gt; element:&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;lt;Compile Include="Page.xaml.cs"&amp;gt; &lt;BR&gt;&amp;lt;/Compile&amp;gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Now you should be able to reload the project in Visual Studio and delete the Page.xaml file while keeping the Page.xaml.cs file.&lt;/EM&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P class=justified&gt;As you probably noticed, some controls have &lt;STRONG&gt;Content&lt;/STRONG&gt; property (in our example &lt;STRONG&gt;Page&lt;/STRONG&gt;) and can only have one element set as their child. Other controls have &lt;STRONG&gt;Children&lt;/STRONG&gt; property (in our example &lt;STRONG&gt;Grid&lt;/STRONG&gt;) and can have entire collection of child controls. When you nest controls inside each other in XAML, the XAML parser acts smart and either sets the inner control as the &lt;STRONG&gt;Content&lt;/STRONG&gt; of the outer control, or adds the inner control to the &lt;STRONG&gt;Children&lt;/STRONG&gt; collection of the outer control. It all depends on which property is available in the outer control.&lt;/P&gt;
&lt;P class=justified&gt;Another thing you probably noticed is that the &lt;STRONG&gt;Background&lt;/STRONG&gt; property of &lt;STRONG&gt;Grid &lt;/STRONG&gt;is of type &lt;STRONG&gt;Brush&lt;/STRONG&gt; but in XAML it is initialized simply by color name. This is a shortcut.&lt;/P&gt;
&lt;P class=justified&gt;This XAML code:&lt;/P&gt;
&lt;P class=code&gt;&amp;lt;Grid x:Name="LayoutRoot" Background="Yellow"&amp;gt;&amp;nbsp; &lt;BR&gt;&amp;lt;/Grid&amp;gt;&lt;/P&gt;
&lt;P class=justified&gt;is equivalent to this one:&lt;/P&gt;
&lt;P class=code&gt;&amp;lt;Grid x:Name="LayoutRoot"&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Grid.Background&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;SolidColorBrush Color="Yellow" /&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Grid.Background&amp;gt; &lt;BR&gt;&amp;lt;/Grid&amp;gt;&lt;/P&gt;
&lt;P class=justified&gt;which in turn translates to this C# code:&lt;/P&gt;
&lt;P class=code&gt;LayoutRoot = new Grid(); &lt;BR&gt;LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);&lt;/P&gt;
&lt;P&gt;There are more shortcuts like this that make XAML code more compact than C# code :)&lt;/P&gt;
&lt;H2&gt;Which way to go?&lt;/H2&gt;
&lt;P class=justified&gt;At this point you are probably confused and don't know when you should use C# and when XAML. Remember, everything you write in XAML, you can write in C# too. The code above is an example. You removed the TextBlock control from XAML and instantiated it in C# instead, giving it the same properties as previously in XAML. The final result is exactly the same. There are however some differences: &lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV class=justified&gt;With XAML you usually write more compact code than with C# (approx. 90 characters vs approx. 180 characters in the example above).&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class=justified&gt;With XAML you can see the results in the designer preview without running the code and you cannot see the preview of the C# code.&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class=justified&gt;XAML is for static code only, you cannot create controls dynamically (for example create a random number of buttons) but you can do it with C#.&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class=justified&gt;XAML is parsed at runtime, so not all errors are caught when the code is compiled. For example, if you try to nest two &lt;STRONG&gt;Grid&lt;/STRONG&gt; controls in one &lt;STRONG&gt;Page&lt;/STRONG&gt; control, the code will compile and run, and generate a runtime exception: &lt;STRONG&gt;XmlParserException&lt;/STRONG&gt; with some obfuscated, not user friendly message. In C# it is simply not possible to make such a mistake. This makes XAML code more difficult to debug than C# code.&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P class=justified&gt;As you see, in some more advanced cases you need to write C# code, because you cannot do everything with XAML. But sometimes you have choice and it's up to you how you write your code: in XAML or C#. I think it is important you learn both. Of course, you can act like an old school programmer and stick with the "real" C# code, ignoring XAML completely, and you can write entire applications this way. However XAML usually allows you to code faster, making you more productive. If that doesn't convince you, your UI designer should convince you. Whoever they are, they won't be able to read your C# code. But they can open your XAML code in Expression Blend, and make your application's UI look cool with a few clicks. I will explain how to do that later on.&lt;/P&gt;
&lt;H2&gt;Silverlight project build process - the big picture&lt;/H2&gt;
&lt;P class=justified&gt;I mentioned the mysterious XAP file before so let's have one last look at what happens when you rebuild your project. You have a bunch of C# files (both written by you and automatically generated for you by Visual Studio), and some XAML files. They are all compiled into a DLL file and the &lt;STRONG&gt;AppManifest.xaml&lt;/STRONG&gt; file is generated. You can find them in the &lt;STRONG&gt;Bin/Debug&lt;/STRONG&gt; subdirectory of your project. These files are then gathered together and compressed to one ZIP file.. that has the XAP extension. Yes, a ZIP file. If you take the &lt;STRONG&gt;HelloWorld.xap&lt;/STRONG&gt; file and rename it to &lt;STRONG&gt;HelloWorld.zip&lt;/STRONG&gt;, you can open it by double clicking and see its contents. The &lt;STRONG&gt;TestPage.html&lt;/STRONG&gt; file is also generated. In our case the local machine acts as the server that contains &lt;STRONG&gt;TestPage.html&lt;/STRONG&gt; and &lt;STRONG&gt;HelloWorld.xap&lt;/STRONG&gt;, but you can copy these two files to a real remote HTTP server. Now that you have your Silverlight application published, either locally or on a real server, you can request the TestPage.html from a browser. It references the &lt;STRONG&gt;HelloWorld.xap&lt;/STRONG&gt; file so the browser will run the Silverlight plugin which will download the XAP file. When the file is downloaded, Silverlight will extract it and read the &lt;STRONG&gt;AppManifest.xaml&lt;/STRONG&gt; file. It contains information which DLL Silverlight needs to load and which class to instantiate first (by default it's the&lt;STRONG&gt; App&lt;/STRONG&gt; class). Once an instance of the &lt;STRONG&gt;App&lt;/STRONG&gt; class is created, the Silverlight application starts to execute.&lt;/P&gt;
&lt;H2&gt;Summary&lt;/H2&gt;
&lt;P class=justified&gt;At this point you should feel comfortable with XAML and C#, and using both in your Silverlight projects. You also have the big picture of what happens when you hit &lt;STRONG&gt;F5&lt;/STRONG&gt; - from compiling your code to executing it by the Silverlight plugin.&lt;/P&gt;
&lt;P class=justified&gt;The source code of the&amp;nbsp;modified "Hello World" application (written in C# without using XAML): &lt;A title=HelloWorldWithoutXaml.zip href="http://blogs.msdn.com/agatastaniak/attachment/9256600.ashx" mce_href="http://blogs.msdn.com/agatastaniak/attachment/9256600.ashx" alt="HelloWorldWithoutXaml.zip"&gt;HelloWorldWithoutXaml.zip&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9256600" width="1" height="1"&gt;</content><author><name>agaace</name><uri>http://blogs.msdn.com/members/agaace.aspx</uri></author><category term="Silverlight 2 Tutorial" scheme="http://blogs.msdn.com/agatastaniak/archive/tags/Silverlight+2+Tutorial/default.aspx" /></entry><entry><title>Silverlight Basics #2: The anatomy of a “Hello world” application</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="5416" href="http://blogs.msdn.com/agatastaniak/attachment/9245703.ashx" /><id>http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx</id><published>2008-12-21T22:06:00Z</published><updated>2008-12-21T22:06:00Z</updated><content type="html">After having read this part you will:
know where to find everything you need to get started with Silverlight, 
know how to create a "Hello world" application and understand what is inside, 
know how to embed the "Hello world" application in a website....(&lt;a href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-2.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9245703" width="1" height="1"&gt;</content><author><name>agaace</name><uri>http://blogs.msdn.com/members/agaace.aspx</uri></author><category term="Silverlight 2 Tutorial" scheme="http://blogs.msdn.com/agatastaniak/archive/tags/Silverlight+2+Tutorial/default.aspx" /></entry><entry><title>Silverlight Basics #1: Introduction for dummies</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-1.aspx" /><id>http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-1.aspx</id><published>2008-12-21T07:50:00Z</published><updated>2008-12-21T07:50:00Z</updated><content type="html">You have probably heard a lot about Silverlight, right? It's been a trendy buzzword recently and a technology that is cool to play with. So if you're looking for a gentle introduction to the Silverlight world, you're in the right place!
After having read this part you will:
know what other technologies were used before Silverlight, 
know what Silveright is and how it fits among other technologies, 
know when it is best to use Silverlight over other technologies....(&lt;a href="http://blogs.msdn.com/agatastaniak/archive/2008/12/21/Silverlight-Basics-1.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9244986" width="1" height="1"&gt;</content><author><name>agaace</name><uri>http://blogs.msdn.com/members/agaace.aspx</uri></author><category term="Silverlight 2 Tutorial" scheme="http://blogs.msdn.com/agatastaniak/archive/tags/Silverlight+2+Tutorial/default.aspx" /></entry></feed>