<?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"><title type="html">Charlie Calvert's Community Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/charlie/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/charlie/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-12-15T22:00:26Z</updated><entry><title>Short Windows 7 ‘How To’ Videos</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/07/01/short-windows-7-how-to-videos.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/07/01/short-windows-7-how-to-videos.aspx</id><published>2009-07-01T19:37:42Z</published><updated>2009-07-01T19:37:42Z</updated><content type="html">&lt;p&gt;If you haven’t tracked them down already, you might be interested in &lt;a href="http://windows.microsoft.com/en-US/windows7/products/videos"&gt;this set of short videos&lt;/a&gt; showing off fun new features in Microsoft’s upcoming Windows 7 operating system. I didn’t know most of these features existed, and I found a number of them to be quite useful. &lt;/p&gt;  &lt;p&gt;The features I liked the best involved simple techniques for handling multiple windows. My desktop is usually a crowded mess, and rather than pretending that we are all highly organized compulsive cleaners, the folks who built Windows 7 include lots of ways to help us clean up our chaos. &lt;/p&gt;  &lt;p&gt;You can grab the task bar at the top of a window, shake it, and watch all the other windows on your desktop automatically minimize. There are several cool features of this type, including an explanation of that funny looking block down at the bottom right of the Windows task bar. &lt;/p&gt;  &lt;p&gt;Most of these videos are about 1 minute in length, some as short as 30 seconds. I found watching them a useful way to pass a few minutes of my day.&lt;/p&gt;  &lt;p&gt;The videos are kept &lt;a href="http://windows.microsoft.com/en-US/windows7/products/videos"&gt;here&lt;/a&gt;: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://windows.microsoft.com/en-US/windows7/products/videos" href="http://windows.microsoft.com/en-US/windows7/products/videos"&gt;http://windows.microsoft.com/en-US/windows7/products/videos&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:44ff4ff1-e9f6-4e36-bd37-5ec2e77775cf" class="wlWriterEditableSmartContent"&gt;del.icio.us Tags: &lt;a href="http://del.icio.us/popular/Windows" rel="tag"&gt;Windows&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9811751" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="Utilities" scheme="http://blogs.msdn.com/charlie/archive/tags/Utilities/default.aspx" /><category term="Windows" scheme="http://blogs.msdn.com/charlie/archive/tags/Windows/default.aspx" /></entry><entry><title>Community Convergence LI</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/06/29/community-convergence-li.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/06/29/community-convergence-li.aspx</id><published>2009-06-29T21:03:19Z</published><updated>2009-06-29T21:03:19Z</updated><content type="html">&lt;p&gt;Welcome to the fifty-first edition of Community Convergence. In this post I’m simply going to highlight some of the great blogging activity produced by our team in recent weeks. Eric Lippert has been wonderfully productive. I’m beginning to wonder if we shouldn’t simply dedicate an entire section of the Internet to his work. I’ve also included a few pointers to the work of our friends in the F# world; both Luke and Luca played major roles in the C# world for many years, and it is fun to track what they are doing with F#.&lt;/p&gt;  &lt;h3&gt;Posts for the Team&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;Eric Lippert&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx"&gt;Iterators at the Summer Games&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx"&gt;C# Lambdas, Haskell and Currying&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/24/it-already-is-a-scripting-language.aspx"&gt;It Already Is A Scripting Language&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/22/why-doesn-t-c-implement-top-level-methods.aspx"&gt;Why Doesn't C# Implement &amp;quot;Top Level&amp;quot; Methods?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/18/use-your-legs-not-your-back.aspx"&gt;Lifting Raising and Hoisting in C#&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/15/making-it-easier.aspx"&gt;Making it easier&lt;/a&gt; (C# Design Philosophy) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx"&gt;What does the optimize switch do?&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Sam Ng&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2009/06/16/com-interop-in-c-4-0.aspx"&gt;COM Interop in C# 4.0&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;CSharp FAQ&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx"&gt;How to use LINQ methods to compare objects of custom types&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/csharpfaq/archive/2009/01/26/does-the-linq-to-objects-provider-have-built-in-performance-optimization.aspx"&gt;Does the “LINQ to Objects” provider have built-in performance optimization?&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Kirill Osenkov&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a name="article-15113062-58"&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;h5&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/06/22/visual-studio-2010-beta1-tfs-https-tf31001-the-servicepointmanager-does-not-support-proxies-with-the-https-scheme.aspx"&gt;Visual Studio 2010 Beta1 + TFS: The ServicePointManager &lt;/a&gt;&lt;/h5&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;h5&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/06/15/vs-project-c-and-editor-team-blogs.aspx"&gt;VS Project, C++ and Editor team blogs&lt;/a&gt;&lt;/h5&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Luke Hoban&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/lukeh/archive/2009/06/26/icfp-programming-contest-2009.aspx"&gt;ICFP Programming Contest 2009&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lukeh/archive/2009/06/26/f-in-silverlight.aspx"&gt;F# in Silverlight&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Luca Bolognese&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/12/lagent-an-agent-framework-in-f-part-iii-default-error-management.aspx"&gt;LAgent: an agent framework in F# – Part III – Default error management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/19/lagent-an-agent-framework-in-f-part-iv-custom-error-management.aspx"&gt;LAgent: an agent framework in F# – Part IV – Custom error management&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/26/lagent-an-agent-framework-in-f-part-v-timeout-management.aspx"&gt;LAgent: an agent framework in F# – Part V – Timeout management&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Charlie Calvert&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/06/11/community-convergence-l.aspx"&gt;Community Convergence L&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/06/10/dotfuscator-in-visual-studio-2010.aspx"&gt;Dotfuscator in Visual Studio 2010&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/06/09/community-convergence-xlix-il.aspx"&gt;Community Convergence XLIX (IL)&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f29%2fcommunity-convergence-li.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f29%2fcommunity-convergence-li.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9808941" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Community Convergence" scheme="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx" /><category term="C# 4.0" scheme="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx" /><category term="FSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/FSharp/default.aspx" /></entry><entry><title>Community Convergence L</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/06/11/community-convergence-l.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/06/11/community-convergence-l.aspx</id><published>2009-06-11T21:32:49Z</published><updated>2009-06-11T21:32:49Z</updated><content type="html">&lt;p&gt;Welcome to the 50th issue of Community Convergence. Now that Visual Studio 2010 Beta is out, I think it might be helpful to draw attention to the new dynamic programming and office development features from the C# team that appear in that release. The C# team defines the core syntax found in C# 4.0, and the IDE features that make it easy for developers to access those features. In this post, I’ll focus on language features, in upcoming post, I’ll focus on IDE features. &lt;/p&gt;  &lt;p&gt;There are, of course, many new tools and APIs that will appear in Visual Studio 2010. In this post I’m focusing exclusively on core language features from the C# team itself, as opposed to features that come from the WPF, WCF, Silverlight, or other teams. &lt;/p&gt;  &lt;p&gt;The text presented here is meant to provide a high level overview of these features. At the end of the article I will point you to more technical resources that will allow you to explore these features in more depth, and to read more precise, rigorous definitions of the new features in C# 4.0.&lt;/p&gt;  &lt;h3&gt;Language Features&lt;/h3&gt;  &lt;p&gt;Dynamic programming and enhanced interaction with Office are the main theme in C# 4.0. Dynamic programming is a means of writing code that does not rely on a static types linked at compile time. Traditionally C# has been a strongly typed language defined in large part by static types that must be declared explicitly at compile time. For instance, &lt;strong&gt;string&lt;/strong&gt;, &lt;strong&gt;int&lt;/strong&gt;, and &lt;strong&gt;object&lt;/strong&gt; are all strongly typed static types that are resolved at compile time. A dynamic type is not linked at compile time, but is instead resolved at run time. &lt;/p&gt;  &lt;p&gt;Why should C# developers care about dynamic programming? Isn’t the strongly typed nature of the C# language one of the language’s best features? The answer to this question is a resounding yes. Strong typing is one of the great advantages of the C# language, and the team expects most developers to continue to write strongly typed code nearly all the time. There is nothing in the new C# 4.0 features that prevents you from continuing to exclusively write strongly typed code, if that is what you prefer.&lt;/p&gt;  &lt;p&gt;However, there are occasions when it is convenient to write dynamic code. For instance, languages such as Python and Ruby are dynamically typed. It is awkward, and sometimes nearly impossible, to call into these languages from a strongly typed language such as C# 3.5. The Office API is also difficult to call from C# 3.5; it has long been awkward for C# developers to call into Office using the current set of language features. &lt;/p&gt;  &lt;p&gt;C# 4.0 is adding dynamic features so that it will be easier for developers to call into dynamic languages such as Python, and into more flexible APIs such as Office. You can think of the entire dynamic programming effort as a move by the C# team to fill in existing holes in the language. C# has always provided a great way to write strongly typed code, now we’re adding support for dynamic code, and for calling into Office applications.&lt;/p&gt;  &lt;p&gt;The team has broken out the new features in C# 4.0 into four categories, all but the last of which are directly related to dynamic programming or Office development:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Dynamic Lookup      &lt;ul&gt;       &lt;li&gt;A syntax for making calls into dynamic languages and APIs. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Named and Option Arguments      &lt;ul&gt;       &lt;li&gt;Allows developers to easily call into and create methods that support optional parameters. This is a big aid to Office developers since the Office API’s make heavy use of optional parameters. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;COM Interop      &lt;ul&gt;       &lt;li&gt;Language features that make it easier to call COM objects. When calling COM objects you can now omit the keyword &lt;strong&gt;ref&lt;/strong&gt;, you need to make fewer casts, and you no longer need depend on heavy-weight files called Primary Interop Assemblies, or PIAs. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Variance      &lt;ul&gt;       &lt;li&gt;Covariance and contravariance have nothing to do with dynamic programming or Office development. Instead, they represent a small, very technical enhancement to the language that makes it easier to use inheritance when writing generic code. In general, this feature ensures that the language behaves as expected, rather than forcing you to rewrite code that looks like it should compile. For instance, the following code has a natural syntax that looks like it should work, but it does not work in C# 3.5. The upcoming changes in C# 4.0 ensure that it will compile:          &lt;p&gt;IEnumerable&amp;lt;string&amp;gt; strings = new List&amp;lt;string&amp;gt;();            &lt;br /&gt;IEnumerable&amp;lt;object&amp;gt; objects = strings;&lt;/p&gt;       &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;That’s the end of our quick overview of the new features in the C# 4.0 language. Again, remember that there are many other new features of in Visual Studio, and many new enhancements to existing technologies such as WPF, WCF, Silverlight and LINQ. In this post, I’ve focused exclusively on changes to the basic syntax of the language that have been implemented by the C# team itself.&lt;/p&gt;  &lt;h3&gt;References&lt;/h3&gt;  &lt;p&gt;Here are few references for people who want to explore this subject in more depth. A guide for developers interested in all things related to C# 4.0 is the &lt;a href="http://code.msdn.microsoft.com/csharpfuture"&gt;C# Futures site&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;. There you will find a &lt;a href="http://code.msdn.microsoft.com/csharpfuture/Release/ProjectReleases.aspx?ReleaseId=1686"&gt;downloads page&lt;/a&gt; that contains the following key resources:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;An excellent and definitive &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=csharpfuture&amp;amp;DownloadId=3550"&gt;white paper&lt;/a&gt; on C# 4.0 language features. Mads Torgersen was the primary author of this document. &lt;/li&gt;    &lt;li&gt;The C# 4.0 &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=csharpfuture&amp;amp;DownloadId=5822"&gt;samples&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Other important resources include C# 4.0 sections Eric Lippert’s and Sam Ng’s blogs:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_+4.0/default.aspx"&gt;Eric Lippert’s Blog&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/default.aspx"&gt;Sam Ng’s Blog&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f11%2fcommunity-convergence-l.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f11%2fcommunity-convergence-l.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9727881" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Community Convergence" scheme="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx" /><category term="C# 4.0" scheme="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx" /></entry><entry><title>Dotfuscator in Visual Studio 2010</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/06/10/dotfuscator-in-visual-studio-2010.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/06/10/dotfuscator-in-visual-studio-2010.aspx</id><published>2009-06-10T23:08:00Z</published><updated>2009-06-10T23:08:00Z</updated><content type="html">&lt;P&gt;Those of you who are using Visual Studio 2010, Beta 1 have probably noticed the Dotfuscator Community edition listing in the VSTS Tools menu. Dotfuscator is made by the the developers at &lt;A href="http://www.preemptive.com/" mce_href="http://www.preemptive.com/"&gt;PreEmptive Solutions&lt;/A&gt;, and they have begun a &lt;A href="http://blogs.preemptive.com/" mce_href="http://blogs.preemptive.com/"&gt;blog&lt;/A&gt; detailing the features available in Dotfuscator, and how to use them. Here is a &lt;A href="http://blogs.preemptive.com/post/Go-Live-with-VS-2010-Dotfuscator-Functionality-with-Dotfuscator-MDE-Today.aspx" mce_href="http://blogs.preemptive.com/post/Go-Live-with-VS-2010-Dotfuscator-Functionality-with-Dotfuscator-MDE-Today.aspx"&gt;link&lt;/A&gt; to one of&amp;nbsp;their first posts, and here is &lt;A href="http://blogs.preemptive.com/post/Whate28099s-new-with-Dotfuscator-in-Visual-Studio-2010-Beta-1.aspx" mce_href="http://blogs.preemptive.com/post/Whate28099s-new-with-Dotfuscator-in-Visual-Studio-2010-Beta-1.aspx"&gt;another post&lt;/A&gt; you might find interesting. &lt;/P&gt;
&lt;P&gt;On their web site, PerEmptive says: “Dotfuscator Software Services – Community Edition is shipping within Visual Studio 2010 and it’s far more than an obfuscation tool. With our post-build, code injection platform, developers can track application and feature usage, defend against tampering, and force application expirations.” &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9725262" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author></entry><entry><title>Community Convergence XLIX (IL)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/06/09/community-convergence-xlix-il.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/06/09/community-convergence-xlix-il.aspx</id><published>2009-06-09T20:44:57Z</published><updated>2009-06-09T20:44:57Z</updated><content type="html">&lt;p&gt;Welcome to the 49th edition of Community Convergence. The big excitment of late has been the recent release of the &lt;a href="http://blogs.msdn.com/somasegar/archive/2009/05/18/visual-studio-2010-and-net-fx-4-beta-1-ships.aspx"&gt;Visual Studio 2010 Beta&lt;/a&gt;. If you have downloaded it yet, you should waste no time in heading over to the &lt;a href="http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx"&gt;product page&lt;/a&gt; and downloading the files. As usual for betas of this type, I would recommend running it inside a Virtual Machine, or on a machine that you can afford to entirely reformat.&lt;/p&gt;  &lt;p&gt;I’ve also included a number of links to articles from team members, many of whom discuss C# 4.0 programming issues that are related to the Visual Studio 2010 beta. There has been a lot of excellent blogging activity lately, so go ahead and get started digging into this great information.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Eric Lippert&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx"&gt;Understanding Out of Memory Errors&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/05/fabulous-adventures-in-russian.aspx"&gt;Fabulous Adventures In Russian&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/04/alas-smith-and-jones.aspx"&gt;Alas, Smith and Jones: Friend Assemblies&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/06/01/bug-psychology.aspx"&gt;Bug Psychology&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Kirill Osenkov&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/06/07/algorithms-in-c-shortest-path-around-a-polygon-polyline-routing.aspx"&gt;Algorithms in C#: shortest path around a polygon (polyline routing)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/06/02/yield-return-and-continuation-passing-style.aspx"&gt;yield return and Continuation-Passing Style&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/05/19/some-resources-about-visual-studio-extensibility.aspx"&gt;Some resources about Visual Studio Extensibility&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/05/19/should-call-hierarchy-display-compiler-generated-members.aspx"&gt;Should Call Hierarchy display compiler-generated member calls?&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/05/01/a-simple-sample-for-c-4-0-dynamic-feature.aspx"&gt;A simple sample for C# 4.0 ‘dynamic’ feature&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Luca Bolognese&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/05/14/i-talk-about-c-and-vb-co-evolution-on-channel9-and-some-f.aspx"&gt;Luca talk about C# and VB Co-Evolution on Channel9 (and some F# …)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/05/29/lagent-an-agent-framework-in-f-part-i-workers-and-parallelworkers.aspx"&gt;LAgent : an agent framework in F# – Part I – Workers and ParallelWorkers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2009/06/05/lagent-an-agent-framework-in-f-part-ii-agents-and-control-messages.aspx"&gt;LAgent : an agent framework in F# – Part II – Agents and control messages&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Sam Ng&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2009/04/17/named-and-optional-arguments-ties-and-philosophies.aspx"&gt;Named and Optional arguments – ties and philosophies&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2009/04/01/named-arguments-and-overload-resolution.aspx"&gt;Named arguments and overload resolution&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f09%2fcommunity-convergence-xlix-il.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f06%2f09%2fcommunity-convergence-xlix-il.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9718420" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Community Convergence" scheme="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx" /><category term="C# 4.0" scheme="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx" /></entry><entry><title>Silverlight Animation Part II: Sprites</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/21/silverlight-animation-part-ii-sprites.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/21/silverlight-animation-part-ii-sprites.aspx</id><published>2009-04-21T21:40:46Z</published><updated>2009-04-21T21:40:46Z</updated><content type="html">&lt;p&gt;In the &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/14/silverlight-simple-animation.aspx"&gt;previous post&lt;/a&gt; in this series, you learned how to create a simple animation with Silverlight. The next step is to learn how to create animated objects called sprites. This post will also explain how to ensure that a &lt;strong&gt;Sprite’s&lt;/strong&gt; movement is restricted to a bounded area, as shown below in Application 1. Logic like this can be used in simple games, or in programs that want to use animation to capture or focus the user’s attention.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5545"&gt;code&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;iframe style="width: 400px; height: 300px" src="http://silverlight.services.live.com/invoke/14488/AnimatedTimer02/iframe.html" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Application 1: Two sprites moving in a bounded area&lt;/strong&gt;&lt;/p&gt;  &lt;h2&gt;The XAML&lt;/h2&gt;  &lt;p&gt;As shown below in Listing 1, the XAML for this application is very simple. A light green &lt;strong&gt;Grid&lt;/strong&gt; hosts a &lt;strong&gt;TextBlock&lt;/strong&gt; and a dark green &lt;strong&gt;Canvas &lt;/strong&gt;named &lt;strong&gt;myCanvas&lt;/strong&gt;. The &lt;strong&gt;Canvas&lt;/strong&gt; contains a red and blue &lt;strong&gt;Rectangle&lt;/strong&gt; named &lt;strong&gt;myRect&lt;/strong&gt; and purple and blue gradient &lt;strong&gt;Image&lt;/strong&gt; named &lt;strong&gt;myImage&lt;/strong&gt;. The &lt;strong&gt;TextBlock&lt;/strong&gt;, shown at the top of the application, display a string showing the width and height of the &lt;strong&gt;Canvas&lt;/strong&gt;, and the current &lt;strong&gt;X&lt;/strong&gt; and &lt;strong&gt;Y&lt;/strong&gt; location of the &lt;strong&gt;Rectangle&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Listing 1: The XAML which serves as the starting point for the simple animation shown in above in Application 1.&lt;/strong&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;SilverlightAnimatedTimer01&lt;/strong&gt;.Page&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LightGreen&amp;quot;&amp;gt;            
        
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myTextBlock&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;TextAlignment&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Center&amp;quot; /&amp;gt;
        
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Canvas &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Green&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myCanvas&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;250&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;350&amp;quot; &lt;span style="color: red"&gt;Loaded&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;StartTimer&amp;quot;&lt;/span&gt;&amp;gt;             
            
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Rectangle &lt;/span&gt;&lt;span style="color: red"&gt;Stroke&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Red&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;StrokeThickness&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;5&amp;quot; 
                       &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;25&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;25&amp;quot; 
                       &lt;/span&gt;&lt;span style="color: red"&gt;Fill&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Blue&amp;quot; 
                       &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myRect&amp;quot; /&amp;gt;                            
            
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Image &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Images/MrBlue.png&amp;quot; 
                   &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myImage&amp;quot; /&amp;gt;
            
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Canvas&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The Canvas object has a &lt;strong&gt;Loaded&lt;/strong&gt; event associated with it, which is used to start a &lt;strong&gt;Timer&lt;/strong&gt;. Code of this type was described in the &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/14/silverlight-simple-animation.aspx"&gt;previous article&lt;/a&gt; in this series, so I will not discuss the implementation of the &lt;strong&gt;Timers&lt;/strong&gt; again in this post. I will however, reference the relevant code once near the end of this article. &lt;/p&gt;

&lt;p&gt;Note also that from a technical perspective it doesn’t matter which control control contains the &lt;strong&gt;Loaded&lt;/strong&gt; event. In this case I’ve chosen the &lt;strong&gt;Canvas&lt;/strong&gt;, but I could just as easily have moved it to the &lt;strong&gt;TextBlock&lt;/strong&gt;. The decision to associate the event with the &lt;strong&gt;Canvas&lt;/strong&gt; was an aesthetic, rather than a technical, decision.&lt;/p&gt;

&lt;h2&gt;C# Code &lt;/h2&gt;

&lt;p&gt;The code we create for this program is broken up into two files. The first file defines&amp;#160; the &lt;strong&gt;Sprites&lt;/strong&gt; that move across the surface of the &lt;strong&gt;Canvas, &lt;/strong&gt;and the second part defines the program’s very simple logic and structure. Again, for aesthetic, rather than technical reasons, I decided to put the &lt;strong&gt;Sprite&lt;/strong&gt; code in its own library. This helps to establish a design principle that I believe will prove useful as the program’s code grows more complex. However, from a technical perspective, the code could just as easily have been placed in the main program.&lt;/p&gt;

&lt;p&gt;Figure 1 gives an overview of the program’s structure. The main program logic is stored in the Silverlight project, primarily in the file called GameCore.cs. The logic for the Sprites is stored in the SilverlightGameLibrary, primarily in the file called SpritesLib.cs. Again, I do this not out of necessity, but simply because I feel it povides a neat division of labor that helps make the program easy to maintain. Notice also that since libraries form discrete reusable blocks of code, they could easily be transferred to a second project which required similar logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightAnimationPartIISprites_CB62/Figure00_2.png"&gt;&lt;img title="Figure00" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="387" alt="Figure00" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightAnimationPartIISprites_CB62/Figure00_thumb.png" width="307" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 1: The Solution Explorer provides an overview of the Solution’s structure. Note that there are three projects in this solution, one of the Silverlight interface, one for ASP.NET web code, and one of the library where shared code can be stored.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;The Sprites&lt;/h3&gt;

&lt;p&gt;In applications of this type, it is simplest to create a small &lt;strong&gt;Sprite&lt;/strong&gt; class that encapsulates the behavior of the objects that move across the screen. There are many different ways to compose such a class, but most people follow a pattern similar to the one shown in Figure 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightAnimationPartIISprites_CB62/Figure01_8.png"&gt;&lt;img title="Figure01" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="722" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightAnimationPartIISprites_CB62/Figure01_thumb_3.png" width="478" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 2: A Simple Sprite class with two descendants, one that encapsulates the Rectangle type, and one that encapsulates the Image type.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The structure of this class hierarchy is easy to discern. A simple &lt;strong&gt;Sprite&lt;/strong&gt; class has two descendants, one tailored for use with &lt;strong&gt;Rectangle&lt;/strong&gt; controls, and one for use with &lt;strong&gt;Image&lt;/strong&gt; controls. The common code which applies to both types of controls is stored in the base class, which is called &lt;strong&gt;Sprite&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With of some its lengthier methods foreshortened, the &lt;strong&gt;Sprite&lt;/strong&gt; class looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Sprite
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;internal enum &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType &lt;/span&gt;{ None, Up, Down, DownLeft, DownRight, UpLef

    &lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Rect &lt;/span&gt;containerBounds = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Rect&lt;/span&gt;();
    &lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FrameworkElement &lt;/span&gt;SpriteShape { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;Y { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;X { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;incValue = 1.0;
    &lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType &lt;/span&gt;ShapeMoveType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;Sprite()
    {
        ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.None;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;Sprite(&lt;span style="color: #2b91af"&gt;FrameworkElement &lt;/span&gt;initSpriteShape, &lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;initX, &lt;span style="color: #2b91af"&gt;Doubl
        &lt;/span&gt;: &lt;span style="color: blue"&gt;this&lt;/span&gt;()
    {
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.SpriteShape = initSpriteShape;
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.X = initX;
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.Y = initY;
    }

    &lt;span style="color: blue"&gt;internal void &lt;/span&gt;CheckBounds()
    {
       // Code omitted here        
    }

    &lt;span style="color: blue"&gt;virtual public void &lt;/span&gt;Move()
    {
        CheckBounds();
        // Code omitted here 
        SpriteShape.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty, X);
        SpriteShape.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty, Y);
    }    &lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    public void &lt;/span&gt;SetBounds(&lt;span style="color: blue"&gt;int &lt;/span&gt;initWidth, &lt;span style="color: blue"&gt;int &lt;/span&gt;initHeight)
    {
        containerBounds.X = 0;
        containerBounds.Y = 0;
        containerBounds.Height = initHeight;
        containerBounds.Width = initWidth;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The heart of the Sprite class are the &lt;strong&gt;SpriteShape&lt;/strong&gt;, &lt;strong&gt;X&lt;/strong&gt; and &lt;strong&gt;Y&lt;/strong&gt; fields:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FrameworkElement &lt;/span&gt;SpriteShape { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;Y { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;X { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

&lt;p&gt;These two points define the upper left corner of a sprite. The &lt;strong&gt;Y&lt;/strong&gt; field is the top bound, and the &lt;strong&gt;X&lt;/strong&gt; field defines the left bound of the sprite. The &lt;strong&gt;SpriteShape&lt;/strong&gt; field derives from &lt;strong&gt;FrameworkElement&lt;/strong&gt;, which is a base class for both the &lt;strong&gt;Image&lt;/strong&gt; and the &lt;strong&gt;Rectangle&lt;/strong&gt; type. This base type contains enough logic to allow us to use the type to animate the &lt;strong&gt;Sprite&lt;/strong&gt; as it moves across the canvas.&lt;/p&gt;

&lt;p&gt;A method called &lt;strong&gt;SetBounds&lt;/strong&gt; is used to define the range over which we want the Sprite to travel. In this program, the &lt;strong&gt;Rect&lt;/strong&gt; type called &lt;strong&gt;containerBounds&lt;/strong&gt; is initialized to values defining the &lt;strong&gt;Left&lt;/strong&gt;, &lt;strong&gt;Top&lt;/strong&gt;, &lt;strong&gt;Width&lt;/strong&gt; and &lt;strong&gt;Height&lt;/strong&gt; of the Canvas on which the sprites will live. The code states, in effect, that the sprites are not allowed to wander outside the bounds of the &lt;strong&gt;Canvas&lt;/strong&gt;. We will revisit this code in the next section of the program, where you will see exactly how the bounds are initialized.&lt;/p&gt;

&lt;p&gt;An enumeration captures the six basic movements of which our sprites are capable:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal enum &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType &lt;/span&gt;{ None, Up, Down, DownLeft, DownRight, UpLeft, UpRight };&lt;/pre&gt;

&lt;p&gt;In a more complex program, we could replace these simple movements with complex calculations, or even with code based on the physics of a particular object that you might want to encapsulate in your program. For instance, advanced mathematicians could describe the physics of a pool table, or of the waves on an ocean. In this case, however, the simple type defined here meets our needs.&lt;/p&gt;

&lt;p&gt;A lengthy but logically very simple method called &lt;strong&gt;CheckBounds&lt;/strong&gt; is used to compare the current &lt;strong&gt;X&lt;/strong&gt; and &lt;strong&gt;Y&lt;/strong&gt; location of the &lt;strong&gt;Sprite&lt;/strong&gt; with the known bounds of its container. If the &lt;strong&gt;Sprite&lt;/strong&gt; threatens to move outside these bounds, then is course is changed. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: Code I will show you later initializes the direction the Sprite is moving as the program begins. For instance, the &lt;strong&gt;Image&lt;/strong&gt; control is initialized to begin moving down and to the right (&lt;strong&gt;MoveType.DownRight&lt;/strong&gt;), while the &lt;strong&gt;Rectangle&lt;/strong&gt; control is initialized to move down and to the left (&lt;strong&gt;MoveType.DownLeft&lt;/strong&gt;). &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An abbreviated version of the &lt;strong&gt;CheckBounds&lt;/strong&gt; method is shown in the excerpt from the &lt;strong&gt;Sprite&lt;/strong&gt; class shown above. Here is the complete definition of the method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal void &lt;/span&gt;CheckBounds()
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;((Y + SpriteShape.ActualHeight) &amp;gt; containerBounds.Height)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(ShapeMoveType == &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownRight)
        {
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpRight;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpLeft;
        }
    }
    &lt;span style="color: blue"&gt;else if &lt;/span&gt;(Y &amp;lt; 0)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(ShapeMoveType == &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpRight)
        {
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownRight;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownLeft;
        }
    }
    &lt;span style="color: blue"&gt;else if &lt;/span&gt;(X + SpriteShape.ActualWidth &amp;gt; containerBounds.Width)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(ShapeMoveType == &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpRight)
        {
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpLeft;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownLeft;
        }
    }
    &lt;span style="color: blue"&gt;else if &lt;/span&gt;(X &amp;lt; 0)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(ShapeMoveType == &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownLeft)
        {
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownRight;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpRight;
        }
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The first line of code in this method checks if the sprite is about to wander off the lower edge of the &lt;strong&gt;Canvas&lt;/strong&gt;. If it is, then its trajectory is changed from a downward to an upward direction. In particular, if the shape is moving down and to the right, then it is told to start moving up and to the right, and so on.&lt;/p&gt;

&lt;p&gt;The first else block in the code checks if the &lt;strong&gt;Sprite&lt;/strong&gt; is about to move off the top of the canvas. If it is, then it is told to begin moving in a downward direction, and so on.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Move&lt;/strong&gt; method defines the engine the actually drives a &lt;strong&gt;Sprite&lt;/strong&gt; across the canvas. Again, an abbreviated version of this method was shown in the original version of the &lt;strong&gt;Sprite&lt;/strong&gt; class shown above. Here is the complete method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;virtual public void &lt;/span&gt;Move()
{
    CheckBounds();

    &lt;span style="color: blue"&gt;switch &lt;/span&gt;(ShapeMoveType)
    {
        &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.None:
            &lt;span style="color: blue"&gt;break&lt;/span&gt;;

        &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownRight:
            X += incValue;
            Y += incValue;
            &lt;span style="color: blue"&gt;break&lt;/span&gt;;

        &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownLeft:
            X -= incValue;
            Y += incValue;
            &lt;span style="color: blue"&gt;break&lt;/span&gt;;

        &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpRight:
            X += incValue;
            Y -= incValue;
            &lt;span style="color: blue"&gt;break&lt;/span&gt;;

        &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.UpLeft:
            X -= incValue;
            Y -= incValue;
            &lt;span style="color: blue"&gt;break&lt;/span&gt;;

        &lt;span style="color: blue"&gt;default&lt;/span&gt;:
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Bad move type&amp;quot;&lt;/span&gt;);
    }

    SpriteShape.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty, X);
    SpriteShape.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty, Y);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The code begins by calling the &lt;strong&gt;CheckBounds&lt;/strong&gt; method, to ensure that the &lt;strong&gt;Sprite&lt;/strong&gt; is not set on a course that will allow it to wander off the straight and narrow path of virtue. The program then enters a &lt;strong&gt;switch&lt;/strong&gt; statement, which defines how to move the sprite in five of the seven possible directions in which it can move. This program never moves the sprite straight up or straight down, though it would not be hard to see how to define the code to implement that logic. A value called &lt;strong&gt;incValue&lt;/strong&gt; is declared to define how far the sprite moves. In this version of the program, that value is set to one, which means that the sprite moves one pixel at a time. If you wanted the sprite to move faster, you could set this value to higher number, or you could change the rate at which the program’s timer is called.&lt;/p&gt;

&lt;p&gt;The logic for moving the sprite down and to the left and right, or up and to the left and right, is so simple that I will not enumerate it here. it is simpler for you to simply look at the code in the &lt;strong&gt;Move&lt;/strong&gt; method, and to derive your understanding of its logic from what you see there.&lt;/p&gt;

&lt;p&gt;If you look up at Figure 1, you can see that I create two simple descendants of the &lt;strong&gt;Sprite&lt;/strong&gt; class: one for the &lt;strong&gt;Rectangle&lt;/strong&gt; and one for the &lt;strong&gt;Image&lt;/strong&gt;. I do place a small amount of logic in these classes, but really, they are place holders in case we find some reason later on for customizing the behavior of the &lt;strong&gt;Rectangle&lt;/strong&gt; or of the &lt;strong&gt;Image&lt;/strong&gt;. For instance, if you wanted to change the color of the border of the &lt;strong&gt;Rectangle&lt;/strong&gt;, that logic would apply only to &lt;strong&gt;Rectangles&lt;/strong&gt;, and not to Images. Hence it would belong in the &lt;strong&gt;MrRect&lt;/strong&gt; class, and not in the base class.&lt;/p&gt;

&lt;p&gt;I’ll show you only one of these classes, since they are nearly identical:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MrBlue &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Sprite
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Image &lt;/span&gt;myImage { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;MrBlue()
        : &lt;span style="color: blue"&gt;base&lt;/span&gt;()
    {

    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;MrBlue(&lt;span style="color: #2b91af"&gt;Image &lt;/span&gt;initImage,
        &lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;initX, &lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;initY)
        : &lt;span style="color: blue"&gt;base&lt;/span&gt;(initImage, initX, initY)
    {
        myImage = initImage;
        ShapeMoveType = &lt;span style="color: #2b91af"&gt;MoveType&lt;/span&gt;.DownRight;
    }        
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You can see that this class initializes the direction the &lt;strong&gt;Image&lt;/strong&gt; moves in, setting it to &lt;strong&gt;MoveType.DownRight&lt;/strong&gt;.&amp;#160; I also save a copy of the original shape. Neither of these bits of logic really justifies the existence of this class, but I create it anyway because I feel – for the reasons outlined above -- that it is likely to be useful in later iterations of this type of program.&lt;/p&gt;

&lt;h3&gt;Program Logic&lt;/h3&gt;

&lt;p&gt;Now that the Sprite logic is defined, the program logic is very easy to write. I define a simple class called &lt;strong&gt;GameCore&lt;/strong&gt;, and create an instance of &lt;strong&gt;MrBlue&lt;/strong&gt; and &lt;strong&gt;MrRect&lt;/strong&gt; inside it. Also included in the class is a method called:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;MainLoop&lt;/strong&gt; for driving the movement of the sprites &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;ShowProgramData&lt;/strong&gt; for updating the user with an ongoing description of the program’s state &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;InitBounds&lt;/strong&gt; for defining the range inside which the Sprites can move. &lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GameCore
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MrBlue &lt;/span&gt;mrBlue = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MrRect &lt;/span&gt;mrRect = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Page &lt;/span&gt;page = &lt;span style="color: blue"&gt;null&lt;/span&gt;;        

    &lt;span style="color: blue"&gt;public &lt;/span&gt;GameCore(&lt;span style="color: #2b91af"&gt;Page &lt;/span&gt;page)
    {
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.page = page;
        mrBlue = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MrBlue&lt;/span&gt;(page.myImage, 0.0, 0.0);
        mrRect = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MrRect&lt;/span&gt;(page.myRect, 325.0, 0.0);
        InitBounds();
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;MainLoop()
    {
        mrRect.Move();
        mrBlue.Move();
        ShowProgramData();
    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;ShowProgramData()
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;xValue = &lt;span style="color: #2b91af"&gt;Strings&lt;/span&gt;.PadLeft(mrRect.X.ToString(), &lt;span style="color: #a31515"&gt;'0'&lt;/span&gt;, 3);
        &lt;span style="color: blue"&gt;string &lt;/span&gt;yValue = &lt;span style="color: #2b91af"&gt;Strings&lt;/span&gt;.PadLeft(mrRect.Y.ToString(), &lt;span style="color: #a31515"&gt;'0'&lt;/span&gt;, 3);
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Double&lt;/span&gt;.IsNaN(page.myCanvas.ActualHeight) || &lt;span style="color: #2b91af"&gt;Double&lt;/span&gt;.IsNaN(page.myCanvas.ActualWidth))
        {
            page.myTextBlock.Text = &lt;span style="color: #a31515"&gt;&amp;quot;Not a number&amp;quot;&lt;/span&gt;;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            page.myTextBlock.Text = &lt;span style="color: #a31515"&gt;&amp;quot;Height &amp;quot; &lt;/span&gt;+ page.myCanvas.ActualHeight.ToString()
                + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ &lt;span style="color: #a31515"&gt;&amp;quot;Width = &amp;quot; &lt;/span&gt;+ page.myCanvas.ActualWidth.ToString()
                + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ &lt;span style="color: #a31515"&gt;&amp;quot;X = &amp;quot; &lt;/span&gt;+ xValue
                + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ &lt;span style="color: #a31515"&gt;&amp;quot;Y = &amp;quot; &lt;/span&gt;+ yValue;                
        }
    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;InitBounds()
    {
        mrRect.SetBounds(&lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.ToInt32(page.myCanvas.ActualWidth), &lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.ToInt32(page.myCanvas.ActualHeight));
        mrBlue.SetBounds(&lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.ToInt32(page.myCanvas.ActualWidth), &lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.ToInt32(page.myCanvas.ActualHeight));
    }
}&lt;/pre&gt;

&lt;p&gt;The constructor is used simply to initialize the starting places for the &lt;strong&gt;Image&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt; controls, and to initializes the bounds over which they can range. &lt;/p&gt;

&lt;p&gt;The timer for the program is implemented in the file called by default &lt;strong&gt;Page.xaml.cs. &lt;/strong&gt;As described in &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/08/silverlight-creating-a-new-project.aspx"&gt;previous posts&lt;/a&gt;, this file is auto-generated by the Visual Studio IDE. It is used to drive the program logic by repeatedly calling the method called &lt;strong&gt;MainLoop&lt;/strong&gt;. Here is the implementation of the Timer:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Page &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;UserControl
&lt;/span&gt;{

    &lt;span style="color: #2b91af"&gt;GameCore &lt;/span&gt;gameCore = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;Page()
    {
        InitializeComponent();
        gameCore = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;GameCore&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;);                   
    }

    &lt;span style="color: blue"&gt;private void &lt;/span&gt;StartTimer(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
    {
        System.Windows.Threading.&lt;span style="color: #2b91af"&gt;DispatcherTimer &lt;/span&gt;myDispatcherTimer = 
            &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Windows.Threading.&lt;span style="color: #2b91af"&gt;DispatcherTimer&lt;/span&gt;();
        myDispatcherTimer.Interval = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;(0, 0, 0, 0, 10); 
        myDispatcherTimer.Tick += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventHandler&lt;/span&gt;(Each_Tick);
        myDispatcherTimer.Start();
    }        
    
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Each_Tick(&lt;span style="color: blue"&gt;object &lt;/span&gt;o, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;sender)
    {
        gameCore.MainLoop();
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The &lt;strong&gt;MainLoop&lt;/strong&gt; method calls the &lt;strong&gt;Move&lt;/strong&gt; methods of the two shape controls: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;MainLoop()
{
    mrRect.Move();
    mrBlue.Move();
    ShowProgramData();
}&lt;/pre&gt;

&lt;p&gt;It also calls a method named &lt;strong&gt;ShowProgramData&lt;/strong&gt;, which allows you to display any other information you want to share with the user. In this case, I use it to display in a &lt;strong&gt;TextBlock&lt;/strong&gt; the current location of the &lt;strong&gt;Rectangle&lt;/strong&gt; control.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this post, you have seen how to create a simple Silverlight animation which moves simple sprites inside a bounded rectangle. The post describes how to create simple &lt;strong&gt;Sprite&lt;/strong&gt; classes that know how to move in pre-defined directions across a drawing surface, and which know how to limit their movement to a bounded rectangle.&lt;/p&gt;

&lt;p&gt;There is quite a bit of code in this post, but most of it is extremely simple. In general, programs of this type are very easy to create, but they provide you with a framework from which you can build a wide variety of animations that can be used to decorate a standard program or to serve as the core engine for a simple game.&lt;/p&gt;

&lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5545"&gt;code&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f21%2fsilverlight-animation-part-ii-sprites.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f21%2fsilverlight-animation-part-ii-sprites.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9560423" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Visual Studio" scheme="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx" /><category term="Silverlight" scheme="http://blogs.msdn.com/charlie/archive/tags/Silverlight/default.aspx" /></entry><entry><title>Silverlight Layouts Part 1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/19/silverlight-layouts-part-1.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/19/silverlight-layouts-part-1.aspx</id><published>2009-04-20T05:35:00Z</published><updated>2009-04-20T05:35:00Z</updated><content type="html">&lt;p&gt;This is the third in a series of posts on Silverlight. The text focuses on creating a basic interface for a Silverlight application and explores simple ways to work with &lt;strong&gt;Grid&lt;/strong&gt; controls that are divided up into &lt;strong&gt;Rows&lt;/strong&gt; and &lt;strong&gt;Columns&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;So far, I’ve added the following posts to the Silverlight series:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/02/silverlight-revisited.aspx"&gt;Silverlight Post 1&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/08/silverlight-creating-a-new-project.aspx"&gt;Creating a Silverlight Project&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Silverlight Layouts Part I (This Post) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/14/silverlight-simple-animation.aspx"&gt;Silverlight Simple Animation&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Related Posts&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/08/publishing-an-asp-net-web-application-in-iis.aspx"&gt;Publishing an ASP.NET Web Application in IIS&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Source Code&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5495"&gt;source&lt;/a&gt; for this post from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;UserControls&lt;/h2&gt;  &lt;p&gt;Silverlight 2.0 projects typical use a combination of &lt;a href="http://en.wikipedia.org/wiki/XAML"&gt;XAML&lt;/a&gt; and C# or VB code. XAML stands for Extensible Application Markup Language. It is pronounced Zamel, and it rhymes with the word camel.&lt;/p&gt;  &lt;p&gt;When you create a new Silverlight project, the following XAML is produced:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;SilverlightApplication3.Page&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;

    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Here you see two controls:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A &lt;strong&gt;UserControl&lt;/strong&gt; &lt;/li&gt;

  &lt;li&gt;And inside it, a Grid &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;UserControl&lt;/strong&gt; is a standard Silverlight type from which developers can derive their own custom controls. You can derive any type of control you want from a &lt;strong&gt;UserControl&lt;/strong&gt;. However, the &lt;strong&gt;UserControl&lt;/strong&gt; shown in the code quoted above usually acts as a container for your interface. Typically, developers fill this container with a series of &lt;strong&gt;Buttons&lt;/strong&gt;, &lt;strong&gt;Textboxes&lt;/strong&gt; and other simple elements that define an interface of some type. &lt;/p&gt;

&lt;p&gt;You can draw a very rough analogy between a &lt;strong&gt;Form&lt;/strong&gt; in a Windows Forms application and this &lt;strong&gt;UserControl&lt;/strong&gt; in a Silverlight application. Just as you use a &lt;strong&gt;Form&lt;/strong&gt; to define the interface for Windows application, you use a &lt;strong&gt;UserControl&lt;/strong&gt; to define the interface for your Silverlight application. &lt;/p&gt;

&lt;p&gt;It’s important to remember, however, that this control is, by default, embedded inside an ASP.NET Web Application Project. You could, therefore, also view the control as an interface element inserted into an ASP.NET web application. In our case, however, we are focusing exclusively on the Silverlight portion of the application, so the Windows Forms analogy is not too inappropriate, so long as you don’t take it overly literally, and so long as you remember that &lt;strong&gt;UserControls&lt;/strong&gt; can be used for entirely different purposes in other contexts. You can create any type of custom control you want from a &lt;strong&gt;UserControl&lt;/strong&gt;, but in this context, the &lt;strong&gt;UserControl&lt;/strong&gt; is a container for your interface.&lt;/p&gt;

&lt;h2&gt;Grids&lt;/h2&gt;

&lt;p&gt;Inside the &lt;strong&gt;UserControl&lt;/strong&gt; is a &lt;strong&gt;Grid&lt;/strong&gt;. If you want, you can use this grid as the host for most of the controls in your program. You could, for instance, place a button inside it:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;SilverlightApplication11.Page&amp;quot; 
    &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;br /&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;OK&amp;quot;/&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This XAML code produces the following output:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure01_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure01" border="0" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure01_thumb.png" width="433" height="460" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 01: A Grid control hosting an OK button.&lt;/strong&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This seems like a good start, but if you try to add another button, you will see that things don’t work out as you might have expected. Here is the code for adding a second button:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;SilverlightApplication11.Page&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;OK&amp;quot;/&amp;gt; 
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Cancel&amp;quot;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The output from this code looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure02_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure02" border="0" alt="Figure02" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure02_thumb.png" width="433" height="460" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 02: A Grid control that hosts a Cancel button which obscures an OK button.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There is our &lt;strong&gt;Cancel&lt;/strong&gt; button, as big as life. But what happened to the &lt;strong&gt;OK&lt;/strong&gt; button? It appears to be obscured by the &lt;strong&gt;Cancel&lt;/strong&gt; button. In other words, the Grid control is not allowing us to display two buttons at once, but instead is taking one button, and allowing it fill the entire interface. In same situations, this might be exactly what we want, but in this case it is working at cross purposes to our goal.&lt;/p&gt;

&lt;p&gt;As you have no doubt surmised, the &lt;strong&gt;Grid&lt;/strong&gt; control, as it is currently configured, will not produce the behavior we want. There are, in fact many ways to get the grid control to display multiple buttons. Here is one way:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot;  &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;OK&amp;quot;/&amp;gt; 
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Cancel&amp;quot;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This code defines two rows in the grid: one fills the top half of the grid, and the second fills the bottom half. We have added code to our &lt;strong&gt;Button&lt;/strong&gt; declarations asking the &lt;strong&gt;Ok&lt;/strong&gt; button to be hosted in Row 0, and the &lt;strong&gt;Cancel&lt;/strong&gt; button to be hosted in Row 1. Here is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure03_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure03" border="0" alt="Figure03" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure03_thumb.png" width="433" height="460" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 3: A Grid control with two rows. The first row hosts an OK button, the second a Cancel button.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you need columns, rather than rows, then use this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot;  &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;OK&amp;quot;/&amp;gt; 
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Cancel&amp;quot;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Here is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure04_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure04" border="0" alt="Figure04" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure04_thumb.png" width="433" height="460" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 4: A Grid control with two columns, each of which hosts a button.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want both rows and columns, then write code like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;SilverlightApplication11.Page&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ColumnDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.ColumnDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;OK&amp;quot;/&amp;gt; 
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Cancel&amp;quot;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Image &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;0&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Images/BlueLight.png&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Image &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Images/RedLight.png&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This interface produces four sections inside the grid. We could have left the two new spaces empty, but I thought it would be less confusing if I put something inside them. To keep the code simple, I’ve added two &lt;strong&gt;Image&lt;/strong&gt; controls and placed two simple bitmaps in them. I then use both the &lt;strong&gt;Grid.Column&lt;/strong&gt; and &lt;strong&gt;Grid.Row&lt;/strong&gt; properties in the &lt;strong&gt;Button&lt;/strong&gt; and &lt;strong&gt;Image&lt;/strong&gt; controls to designate where I want the images and buttons to appear.&lt;/p&gt;

&lt;p&gt;Note that I store that &lt;strong&gt;Images&lt;/strong&gt; in a directory called &lt;strong&gt;Images&lt;/strong&gt;. You can create project directories of this type by writing clicking on your project in the Solution Explorer, and choosing &lt;strong&gt;Add | New Folder&lt;/strong&gt;. After you copy the bitmaps into the Images directory, click the Show All Files button at the top of the Solution Explorer to make the bitmaps visible. Then right click on the images and explicitly add them to your project. When you are done, your work Solution Explorer might look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure05_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure05" border="0" alt="Figure05" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure05_thumb_1.png" width="282" height="443" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 5: Creating an Images directory that contains two bitmaps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Taken together, all of the XAML code shown in the previous examples produces the following results, where the blue and orange areas are the bitmaps that I stored in the Images directory:&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure06_1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure06" border="0" alt="Figure06" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightLayouts_8D4D/Figure06_thumb_1.png" width="433" height="460" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 6: A Grid control with two rows and two columns. The control hosts two buttons and two images, one in each quadrant of the Grid.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;That’s probably enough for this post. There is obviously a great deal more to be said about &lt;strong&gt;Grid&lt;/strong&gt; controls in particular, and Silverlight interfaces in general, but this can serve as an introduction to the topic. The next article in this series will continue in this same vein, discussing, layouts, &lt;strong&gt;Grid&lt;/strong&gt; controls, and &lt;strong&gt;StackPanels&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.silverlightshow.net/items/Using-the-Grid-control-in-Silverlight-2-Beta-1.aspx"&gt;Grid Controls&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://silverlight.net/blogs/msnow/archive/2008/03/31/part-iv-building-the-game-interface-using-the-grid-control.aspx"&gt;Game Control&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5495"&gt;source&lt;/a&gt; for this post from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fsilverlight-layouts-part-1.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fsilverlight-layouts-part-1.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9539303" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Visual Studio" scheme="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx" /><category term="Silverlight" scheme="http://blogs.msdn.com/charlie/archive/tags/Silverlight/default.aspx" /></entry><entry><title>Silverlight Simple Animation</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/14/silverlight-simple-animation.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/14/silverlight-simple-animation.aspx</id><published>2009-04-14T07:10:16Z</published><updated>2009-04-14T07:10:16Z</updated><content type="html">&lt;p&gt;This post is one of &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/08/silverlight-creating-a-new-project.aspx"&gt;series&lt;/a&gt; on Silverlight. In this article the focus is on a technique that uses a timer to produce simple animations. I will follow this post with at least one additional article on Silverlight animation.&lt;/p&gt;  &lt;p&gt;Silverlight has several built in techniques for animating controls. Many of these technologies are particularly useful for creating simple animations meant to decorate a web page with eye catching movement that draws the reader’s attention. In this post I will skip over these decorative technologies, and instead show how to create a simple animation using a technique similar to those used in many games. &lt;/p&gt;  &lt;p&gt;Though Silverlight is a web technology, the technique I will focus on is very similar to the same technology you would use in most standard programming languages such as C++ or Delphi. Though the animation technology I will focus on is often used in game programming, there are many different reasons why you might want to create this kind animation, particular in scientific programming, or code that attempts to illustrate complex numeric data. &lt;/p&gt;  &lt;p&gt;You will find that Silverlight makes the type of animation I want to focus on very simple. As such, it makes a good place to start an exploration of Silverlight animation. I want to emphasize, however, that there are other Silverlight techniques that use &lt;strong&gt;Storyboards&lt;/strong&gt; and the &lt;strong&gt;DoubleAnimation&lt;/strong&gt; and &lt;strong&gt;PointAnimation&lt;/strong&gt; controls that might be more appropriate if you just want to add a bit of color to a web page. You can add a bit more complexity to those technologies by exploring key frames. See, for instance, the &lt;strong&gt;DoubleAnimationUsingKeyFrames&lt;/strong&gt; control. &lt;/p&gt;  &lt;p&gt;In the example shown in this post I will create two simple “sprites” that I will animate by moving them smoothly and rapidly at an angle across a web page, as shown in Figure 1. In Figure 1, the blue &lt;strong&gt;Rectangle&lt;/strong&gt; with the red border started its brief but illustrious career near the upper right corner of the green field, and is moving down toward the bottom left corner. The purple and blue gradient image started at the upper left corner and is moving at an angle toward the bottom right of the green &lt;strong&gt;Canvas&lt;/strong&gt; control.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightSimpleAnimation_14341/Figure01.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure01" border="0" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightSimpleAnimation_14341/Figure01_thumb.png" width="406" height="306" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 1: Two simple sprites moving at angles across a green field.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Below you can see a live version of this application. Because the animation is quite brief, you will need to press the refresh button to see it in action.&lt;/p&gt; &lt;iframe style="width: 400px; height: 300px" src="http://silverlight.services.live.com/invoke/14488/AnimatedTimer01/iframe.html" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;  &lt;p&gt;The sprites in this example move smoothly and at a reasonable pace across the green surface. This is very important, as users frequently denigrate applications that have jerky or overly slow animations.&lt;/p&gt;  &lt;p&gt;In the next post in this series I will show how to get more control over the sprites, and how to get them to “bounce” off the edges of a window so that they stay confined in a defined space, such as the green field shown here.&lt;/p&gt;  &lt;h2&gt;Creating the XAML&lt;/h2&gt;  &lt;p&gt;Silverlight’s reliance on WPF frequently leaves developers with a choice between implementing their logic in XAML or in standard C# code, or in some combination of the two. Perhaps because my background is a C# developer, I tend to write only a minimal amount of XAML, and to implement most of my logic in C# code.&lt;/p&gt;  &lt;p&gt;The XAML for this example is extremely simple:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;SilverlightAnimatedTimer01.Page&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;White&amp;quot;&amp;gt;            
        
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Canvas &lt;/span&gt;&lt;span style="color: red"&gt;Background&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Green&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myCanvas&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Loaded&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;StartTimer&amp;quot;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Rectangle &lt;/span&gt;&lt;span style="color: red"&gt;Fill&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Blue&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Stroke&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Red&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;StrokeThickness&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;5&amp;quot; 
                       &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;25&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;25&amp;quot;  &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myRect&amp;quot; /&amp;gt;
            
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Image &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Images/MrBlue.png&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myImage&amp;quot; /&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Canvas&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The code for the &lt;strong&gt;UserControl&lt;/strong&gt; and the &lt;strong&gt;Grid&lt;/strong&gt; is boilerplate Silverlight code produced by the IDE when any new Silverlight project is created. I’ve added only three items:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A &lt;strong&gt;Canvas&lt;/strong&gt; called &lt;strong&gt;myCanvas&lt;/strong&gt; which has an event called &lt;strong&gt;StartTimer&lt;/strong&gt; that is called when the &lt;strong&gt;Canvas&lt;/strong&gt; is first loaded &lt;/li&gt;

  &lt;li&gt;A blue and red &lt;strong&gt;Rectangle&lt;/strong&gt; control that is 25 pixels square &lt;/li&gt;

  &lt;li&gt;An &lt;strong&gt;Image&lt;/strong&gt; control which is also 25 pixels square. It is referenced inside the program as &lt;strong&gt;myImage&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The XAML puts both controls in the upper left corner of the &lt;strong&gt;Canvas&lt;/strong&gt;, which means that in design mode one will be hidden behind the other. The C# code I show in the next section moves the &lt;strong&gt;Rectangle&lt;/strong&gt; off to the right of the &lt;strong&gt;Canvas&lt;/strong&gt;, so that it has a unique location at run time.&lt;/p&gt;

&lt;h2&gt;The Timer&lt;/h2&gt;

&lt;p&gt;Animations of the kind shown here are usually run off a timer which produces a kind of miniature application loop that is called at regular intervals and which acts as engine to drive the animation forward. The loop is started when the &lt;strong&gt;Canvas&lt;/strong&gt; is loaded:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;StartTimer(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{
    System.Windows.Threading.&lt;span style="color: #2b91af"&gt;DispatcherTimer &lt;/span&gt;myDispatcherTimer = 
        &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Windows.Threading.&lt;span style="color: #2b91af"&gt;DispatcherTimer&lt;/span&gt;();
    &lt;span style="color: green"&gt;// Call the timer once every 10 milliseconds
    &lt;/span&gt;myDispatcherTimer.Interval = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;(0, 0, 0, 0, 10); 
    myDispatcherTimer.Tick += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventHandler&lt;/span&gt;(MoveShapes);
    myDispatcherTimer.Start();
}        &lt;/pre&gt;

&lt;p&gt;This code is pulled nearly verbatim from the Silverlight documentation. It begins by creating a timer, then asks that the timer be fired once every ten milliseconds. A method called &lt;strong&gt;MoveShapes&lt;/strong&gt; will be called whenever the timer is fired. Finally the code &lt;strong&gt;Starts&lt;/strong&gt; the timer.&lt;/p&gt;

&lt;p&gt;Here is the &lt;strong&gt;MoveShapes&lt;/strong&gt; method, which is called by the timer once every ten milliseconds:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;imageX = 0.0;
&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;imageY = 0.0;
&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;rectX = 275.0;
&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;rectY = 1.0;
&lt;span style="color: #2b91af"&gt;Double &lt;/span&gt;incValue = 1.0;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;MoveShapes(&lt;span style="color: blue"&gt;object &lt;/span&gt;o, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;sender)
{
    imageX += incValue;
    imageY += incValue;
    rectX -= incValue;
    rectY += incValue;            

    myRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty, rectX);
    myRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty, rectY);

    myImage.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty, imageX);
    myImage.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty, imageY);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This method is passed two parameters. The first is copy of the timer. You can use this object to disable or change the traits of the timer.&lt;/p&gt;

&lt;p&gt;This code in the &lt;strong&gt;MoveShapes&lt;/strong&gt; covers several lines, but its structure is very simple. I’ve declared values to track the current X and Y location of the upper left hand corner of the &lt;strong&gt;Image&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt; controls. Note that I initialize the &lt;strong&gt;rectX&lt;/strong&gt; field to 275.0. It is this value that moves the &lt;strong&gt;Rectangle&lt;/strong&gt; control over to he right of the &lt;strong&gt;Canvas&lt;/strong&gt; at program start up, so that it is no longer hidden behind &lt;strong&gt;Image&lt;/strong&gt; control. Needless to say, in the next article in this series I will create separate objects for each sprite. At this stage however, I’m focusing on showing you the basic animation techniques in the least possible amount of code.&lt;/p&gt;

&lt;p&gt;The last lines show how to call the SetValue method of the &lt;strong&gt;Rectangle&lt;/strong&gt; and &lt;strong&gt;Image&lt;/strong&gt; control in order to move the control across the surface of the &lt;strong&gt;Canvas&lt;/strong&gt;. Silverlight provides us with a nice bonus feature here by automatically handling this transformation. In particular, it erases the original image of the control and then repaints it in the new location. In most computer languages, I would have had to manually write the code to erase the old image.&lt;/p&gt;

&lt;p&gt;Note that the &lt;strong&gt;MoveShapes&lt;/strong&gt; method begins by incrementing or decrementing the fields that specify the location of the controls. This allows us to define how we move the controls across the surface of the &lt;strong&gt;Canvas. &lt;/strong&gt;It is, however, the call to &lt;strong&gt;SetValue&lt;/strong&gt; that actually transforms the location of the controls. The end result is a smooth animation of the controls across the surface of their container. &lt;/p&gt;

&lt;p&gt;The best way to see this animation in action is to download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5416"&gt;code&lt;/a&gt; and try it yourself. Remember that you need to first install the Silverlight integration for Visual Studio 2008, as described in the Get Started section of the Silverlight web site. &lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;The technique for animating controls that I’ve described in this article is very easy to use. Silverlight provides all the tools we need to set up simple animation without requiring any real effort on our part. As a result we can focus our energy on simply describing the path that we want our controls to follow. In particular, you need only:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Set up a timer that calls a method you define at discreet intervals. &lt;/li&gt;

  &lt;li&gt;Define the method called by the timer, and ensure that it contains logic for moving your controls. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As mentioned above, I’ll follow this post with at least one additional article that describes additional techniques for animating sprites. Those techniques require us to write a bit more code than that shown here, but at heart they are also very simple and easy to understand.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Download the &lt;strong&gt;SilverlightAnimatedTimer01.zip &lt;/strong&gt;file containing the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5416"&gt;sample code&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f14%2fsilverlight-simple-animation.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f14%2fsilverlight-simple-animation.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9548074" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author></entry><entry><title>Creating a New Silverlight Project in Visual Studio 2008</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/08/silverlight-creating-a-new-project.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/08/silverlight-creating-a-new-project.aspx</id><published>2009-04-09T00:23:22Z</published><updated>2009-04-09T00:23:22Z</updated><content type="html">&lt;p&gt;This post is part of a &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/02/silverlight-revisited.aspx"&gt;series of articles&lt;/a&gt; on Silverlight. The purpose of this post is describe how to start a new Silverlight project or how to add a new or existing Silverlight project to an existing ASP.NET Web Application. &lt;/p&gt;  &lt;p&gt;To be able to follow the discussion presented in this post you will need to install Silverlight support to Visual Studio. I will not discuss the details of that install, other than to say that you can download the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=c22d6a7b-546f-4407-8ef6-d60c8ee221ed&amp;amp;displaylang=en"&gt;necessary files&lt;/a&gt; from the &lt;a href="http://silverlight.net/GetStarted/"&gt;Get Started&lt;/a&gt; page of the &lt;a href="http://silverlight.net/default.aspx"&gt;Silverlight site&lt;/a&gt;. Note that you need Visual Studio 2008 Service Pack 1 or the install will fail. If the various options for creating Silver projects discussed in this post are not available in your copy of Visual Studio 2008, then the likely cause is that you have not yet installed Silverlight support for Visual Studio.&lt;/p&gt;  &lt;h2&gt;Creating a New Silverlight Project&lt;/h2&gt;  &lt;p&gt;You have several options when creating a new Silverlight project. In this post, I will focus on creating a Silverlight project embedded in an ASP.NET application, but I will discuss the other options available to you. I’m choosing to embed the Silverlight project in an ASP.NET application because that is the default option, because ASP.NET applications are popular, and because the &lt;a href="http://blogs.msdn.com/charlie/archive/2009/04/08/publishing-an-asp-net-web-application-in-iis.aspx"&gt;deployment options&lt;/a&gt; for these kinds of projects are simpler for beginners.&lt;/p&gt;  &lt;p&gt;You will end up creating a solution that owns two projects. Conceptually, that will look something like this:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The Solution File, which will own two projects      &lt;ol&gt;       &lt;li&gt;The first project is the ASP.NET host application. &lt;/li&gt;        &lt;li&gt;The second is the Silverlight project which will end up being embedded as an interface element inside the ASP.NET application. &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Begin by choosing&lt;strong&gt; File | New Project&lt;/strong&gt; from the Visual Studio menu. The default hotkey is &lt;strong&gt;Ctrl + Shift + N&lt;/strong&gt;. The dialog shown in Figure 1 appears. &lt;/p&gt;  &lt;p&gt;In the &lt;strong&gt;Project Types&lt;/strong&gt; window in the upper left section of Figure 1, choose Silverlight. In the &lt;strong&gt;Templates&lt;/strong&gt; window on the upper right of Figure 1 choose &lt;strong&gt;Silverlight Application&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Name&lt;/strong&gt; field in the &lt;strong&gt;New Project&lt;/strong&gt; dialog lets you specify the name of your Silverlight project, which in our case will be &lt;strong&gt;StarterApp&lt;/strong&gt;. The &lt;strong&gt;Solution Name&lt;/strong&gt; field lets you specify the name for the solution that will own both your Silverlight project and its ASP.NET host. Again, lets call this solution &lt;strong&gt;StarterApp&lt;/strong&gt;. You can now click the OK button.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure01_5.png"&gt;&lt;img title="Figure01" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="501" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure01_thumb_1.png" width="687" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 1: The New Project Dialog is reached by choosing &lt;u&gt;File | New Project&lt;/u&gt; from the Visual Studio menu. Select Silverlight from Project types tree view, and Silverlight Application from the Templates list view control.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;After closing the New Project dialog by pressing the OK button, the dialog shown in Figure 2 appears. This dialog is designed to allow you to create a solution that combines a Silverlight project with a host project, which in our case is an ASP.NET application. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure02_6.png"&gt;&lt;img title="Figure02" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="403" alt="Figure02" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure02_thumb_2.png" width="447" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 2: The default selections in the Add Silverlight Application dialog allow you to specify an ASP.NET project type and its name.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There are several different &lt;strong&gt;Project Types&lt;/strong&gt; available when you create an ASP.NET Web project that hosts your Silverlight project. These include the following three options:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The default ASP.NET Web Application Project shown in Figure 2 &lt;/li&gt;    &lt;li&gt;An ASP.NET Web Site &lt;/li&gt;    &lt;li&gt;An ASP.NET MVC Web Project &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Since we are focusing on Silverlight, this is not the place to go into a discussion of the relative values of these three options. Lets just keep things simple and assume that you want to create an Application Project. If you want to choose one of the other options, that will not materially effect the design of your Silverlight project. In other words, you can host the same Silverlight project in all three of these types of ASP.NET Web Projects.&lt;/p&gt;  &lt;p&gt;If you choose to “Automatically generate a test page to host Silverlight at Build time,” then no host project is created. Hence, there are no options for configuring your solution. In other words, the &lt;strong&gt;Options&lt;/strong&gt; section shown in the bottom half of Figure 2 is empty. When you press the OK button in the &lt;strong&gt;Add Silverlight Application&lt;/strong&gt; dialog, the solution created contains only the one Silverlight project, and no host project. When you run the project, a file called &lt;strong&gt;TestPage.html&lt;/strong&gt; is automatically generated to host the Silverlight project. This file is not included in your project, but you can browse to the &lt;strong&gt;Bin\Debug&lt;/strong&gt; directory for your project to view it. You an also go online and view &lt;a href="http://msdn.microsoft.com/en-us/library/cc189089(VS.95).aspx"&gt;the official Microsoft description&lt;/a&gt; of how to embed a Silverlight project in an HTML page. There you will find a description of the code found in &lt;strong&gt;TestPage.html&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Assuming that you chose to embed your Silverlight project in an ASP.NET application, you will end up with a solution that contains two projects, as shown in Figure 3. The first project, called StarterApp, contains the Silverlight code that we will focus on in this series of posts. The second project, called StarterApp.Web, is an ASP.NET application that will host our Silverlight code. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure03_2.png"&gt;&lt;img title="Figure03" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="354" alt="Figure03" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightCreatingaNewProject_990B/Figure03_thumb.png" width="225" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 3: The Solution Explorer shows that a typical default Silverlight solution contains both an ASP.NET Web project and a Silverlight project.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you look at the bottom half of Figure 3 you see the list of files in &lt;strong&gt;StarterApp.Web. &lt;/strong&gt;You can see that this ASP.NET project has been modified to host a Silverlight project. The code that hosts the Silverlight project is strictly boilerplate code which performs its job without any need for modification on your part. &lt;/p&gt;  &lt;p&gt;As an aside, I’ll mention that the file &lt;strong&gt;Page.xaml&lt;/strong&gt;, which is highlighted in Figure 3, contains the key Silverlight code that will focus on in this series of posts. The posts in this series that succeed this one will describe the code in that file, and how you can modify it to create an interface for your Silverlight project.&lt;/p&gt;  &lt;h2&gt;Adding Silverlight to an Existing ASP.NET Web Project&lt;/h2&gt;  &lt;p&gt;There are two options to cover when discussing how to add a Silverlight project to an existing ASP.NET Web Application. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;One involves adding a &lt;strong&gt;new&lt;/strong&gt; Silverlight project to your Web Application &lt;/li&gt;    &lt;li&gt;The other involves adding an &lt;strong&gt;existing&lt;/strong&gt; Silverlight project to your Web Application. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here is a description of both options:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;If you have an ASP.NET Web Project and you want to add a new Silverlight project to it, you can simply right click on the solution file in the Solution Explorer, and choose Add New Project. A dialog appears that allows you to choose to add a Silverlight project to your solution. Select that option and a new Silverlight project will be created and added to the solution. At the same time, code for hosting the Silverlight project will be add to the Web project. &lt;/li&gt;    &lt;li&gt;If you want to add an existing Silverlight project to your existing ASP.NET Web application, then you should first open the Web project. Now right click on the solution file and choose to Add your existing Silverlight project. Finally, open up the &lt;strong&gt;Properties&lt;/strong&gt; for your Web application, and turn to the &lt;strong&gt;Silverlight Applications&lt;/strong&gt; page. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Summary&lt;/h2&gt;  &lt;p&gt;In this post you have learned a little bit about how to create a new Silverlight project in Visual Studio 2008. Clearly it is possible to go into more detail to cover special cases. However, the material covered here should hopefully satisfy the needs of most newcomers looking for basic information on creating Silverlight projects. The highlights are as follows. You learned:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;That you need to install a special add on to enable Silverlight development in Visual Studio &lt;/li&gt;    &lt;li&gt;How to create a new Silverlight project hosted in a new ASP.NET application. &lt;/li&gt;    &lt;li&gt;How to add a new or existing Silverlight project to an existing ASP.NET application. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;You can download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5401"&gt;source&lt;/a&gt; for the StarterApp from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; in &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;.&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fsilverlight-creating-a-new-project.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fsilverlight-creating-a-new-project.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9539211" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author></entry><entry><title>Publishing an ASP.NET Web Application in IIS</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/08/publishing-an-asp-net-web-application-in-iis.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/08/publishing-an-asp-net-web-application-in-iis.aspx</id><published>2009-04-08T17:11:42Z</published><updated>2009-04-08T17:11:42Z</updated><content type="html">&lt;p&gt;I had a need to publish a Web Application from Visual Studio 2008 into IIS, rather than into the default Visual Studio Development Server. I already had IIS installed, but I wasn’t quite sure what to do next. Here are some notes that I took on what turned out to be a three step process to publish an ASP.NET Web Application to my IIS Web Server:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Run Visual Studio as an Administrator and go to the Web Page in the Properties for the Web Application. I did that by right clicking on the Properties node in the Solution Explorer. On the Web page in the Properties dialog I switched from “Use Visual Studio Development Server” to “Use IIS Web Server.” &lt;/li&gt;    &lt;li&gt;As shown in Figure 1, I then went to the Control Panel and opened this page &lt;strong&gt;Control Panel\Programs\Programs and Features &lt;/strong&gt;and choose:       &lt;ol&gt;       &lt;li&gt;Internet Information Services          &lt;ol&gt;           &lt;li&gt;Web Management Tools              &lt;ol&gt;               &lt;li&gt;IIS 6 Management Compatibility                  &lt;ol&gt;                   &lt;li&gt;IIS Metabase and IIS 6 configuration compatibility &lt;/li&gt;                 &lt;/ol&gt;               &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Still in the Control, I want to &lt;strong&gt;Control Panel\Programs\Programs and Features &lt;/strong&gt;and choose:       &lt;ol&gt;       &lt;li&gt;World Wide Web Services          &lt;ol&gt;           &lt;li&gt;Application Development Features              &lt;ol&gt;               &lt;li&gt;ASP.NET &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Steps 2 and 3 are illustrated in Figure 1. Note that &lt;strong&gt;IIS Metabase and IIS 6 configuration compatibility&lt;/strong&gt; and &lt;strong&gt;ASP.NET&lt;/strong&gt; are both selected. When I selected ASP.NET, a number of other options were turned on automatically.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/PublishingaWebApplicationinIIS_10833/Figure01_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure01" border="0" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/PublishingaWebApplicationinIIS_10833/Figure01_thumb.png" width="622" height="528" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;After taking these steps, the &lt;strong&gt;Create Virtual Directory &lt;/strong&gt;button in the Web Page of the Properties for my Web Application worked. I was then able to reach the application by typing a URL such as the following into IIS:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;http://localhost/WebApplication2/&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The actual application with my source code remained in the directory where I did my development. The connection between the Virtual Directory and the directory where the application is actually stored can be viewed and configured with the IIS Manager. You can access the Manager here: &lt;strong&gt;Control Panel\System and Maintenance\Administrative Tools. &lt;/strong&gt;In the &lt;strong&gt;Connections &lt;/strong&gt;window on the left of the Manager, browse to &lt;strong&gt;Sites\Default Site &lt;/strong&gt;and click on one of the IIS options such as &lt;strong&gt;Authentication&lt;/strong&gt;.&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In the &lt;strong&gt;Actions&lt;/strong&gt; window on the right of the Manager, choose View Applications, then right click on your application and choose &lt;strong&gt;Basic Settings. &lt;/strong&gt;The dialog shown in Figure 2 appears. You can use this dialog to modify the path to your application. If you then want to switch back to developing your source code in the original location inside Visual Studio, you may have to use the IIS Manager to remove the application.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/PublishingaWebApplicationinIIS_10833/Figure02_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure02" border="0" alt="Figure02" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/PublishingaWebApplicationinIIS_10833/Figure02_thumb.png" width="526" height="360" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Handling IIS security and set up issues is a subject that I sometimes find a bit challenging, but the information I’ve outlined here solved my problems, and allowed my to proceed with my work.&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fpublishing-an-asp-net-web-application-in-iis.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f08%2fpublishing-an-asp-net-web-application-in-iis.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9538574" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="Visual Studio" scheme="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx" /><category term="Web" scheme="http://blogs.msdn.com/charlie/archive/tags/Web/default.aspx" /></entry><entry><title>Silverlight Revisited</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/04/02/silverlight-revisited.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/04/02/silverlight-revisited.aspx</id><published>2009-04-03T01:56:05Z</published><updated>2009-04-03T01:56:05Z</updated><content type="html">&lt;p&gt;I’ve written about Silverlight in previous posts published on this blog. Recently, however, I’ve found my thoughts coming back to this subject. This is hardly unusual: many people are interested in Silverlight.&lt;/p&gt;  &lt;p&gt;The reason for the attraction to this technology is not hard to discover. Silverlight allows us to build visually appealing web applications with a sophisticated, well designed programming language such as C#. Though there are now many sophisticated tools for creating web applications, but most of them still depend on fragmented models that force us to integrate multiple, hard to use technologies into a patchwork design that is poorly integrated. Applications built on these models can be hard to debug, and hard to maintain. They also lack the ability to easily support rich interfaces that would be taken for granted in a desktop application.&lt;/p&gt;  &lt;p&gt;I’ve spent most of my programming career working in either Delphi or C#, and I have a huge respect for the elegance of these languages, and in the power of object-oriented design to help us break complex problems into manageable, easy to maintain solutions. It is, of course, easy to create poorly designed C# programs. Nevertheless, I believe that C# in particular and Silverlight in general has the capacity, the potential, to support elegant solutions in a way that many web technologies do not.&lt;/p&gt;  &lt;p&gt;In short, I’m interested in Silverlight for two reasons:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It allows me to create a rich interface with sophisticated visual techniques including animation. &lt;/li&gt;    &lt;li&gt;It provides a thoroughly modern programming model, and a set of rich API’s for performing common programming tasks. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So there, whether one agrees with me or not, is my motivation to revisit Silverlight. Once I had found my inspiration, my next step was to visit the &lt;a href="http://silverlight.net/default.aspx"&gt;Silverlight site&lt;/a&gt;, and &lt;a href="http://silverlight.net/GetStarted/"&gt;download&lt;/a&gt; the tools to integrate Silverlight into Visual Studio 2008. The installation worked smoothly on a copy of XP that I had running in a virtual machine. Though I haven’t done it yet, I also intend to download Expression Blend.&lt;/p&gt;  &lt;p&gt;Once I had a set of Silverlight tools integrated into Visual Studio, I set about experimenting with them. After a bit, I came up with the application shown in Figure 1. Not bound by such quotidian obligations as having a practical purpose, or a pleasing interface, it didn’t take me long to put this program together. It’s only redeeming feature was that it gave me a chance to experiment with several Silverlight technologies including:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;XAML and WPF &lt;/li&gt;    &lt;li&gt;A tool called a StackPanel, which helped me organize the interface, which consists primarily of simple controls such as buttons and text areas. &lt;/li&gt;    &lt;li&gt;Two different types of Canvases on which I could I draw.&amp;#160; &lt;/li&gt;    &lt;li&gt;Several different graphical shapes and paths, as well as the Ink technology handed down from Tablet PC. &lt;/li&gt;    &lt;li&gt;A simple LINQ query. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightRevisited_96E3/Silverlight01.png"&gt;&lt;img title="Silverlight01" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="396" alt="Silverlight01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightRevisited_96E3/Silverlight01_thumb.png" width="324" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 1: A simple Silverlight program, running in the Internet Explorer, which demonstrates how to use several key technologies.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In my next post, I’ll talk some about the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5305"&gt;code&lt;/a&gt; for this application, and describe how its various parts fit together. Overall, I found putting this code together was blessedly simple, and overall quite a bit of fun. At least from what I’ve seen so far, one of Silverlight’s great strengths is its ease of use.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5305"&gt;SilverlightDrawingTest&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f02%2fsilverlight-revisited.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f04%2f02%2fsilverlight-revisited.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9530039" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="LinqFarm" scheme="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx" /><category term="Silverlight" scheme="http://blogs.msdn.com/charlie/archive/tags/Silverlight/default.aspx" /></entry><entry><title>Essential LINQ Published</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/03/21/essential-linq-published.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/03/21/essential-linq-published.aspx</id><published>2009-03-21T07:38:32Z</published><updated>2009-03-21T07:38:32Z</updated><content type="html">&lt;p&gt;There was a knock on my door this morning. By the time I could make it to the front of the house, the delivery man was gone, but he had left behind a box full red and white books, each with the title Essential LINQ. On the cover was my name, and the name of my good friend &lt;a href="http://blogs.msdn.com/dinesh.kulkarni/archive/2009/03/10/essential-linq-book-to-be-published-tomorrow.aspx"&gt;Dinesh Kulkarni&lt;/a&gt;. This was, of course, a box full of the first copies of my &lt;a href="http://www.elvenware.com/charlie/books/index.html"&gt;most recent book&lt;/a&gt;. They had a arrived at my house on a most propitious date: the first day of spring.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.amazon.com/Essential-LINQ-Microsoft-NET-Development/dp/0321564162/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1237620099&amp;amp;sr=8-1#"&gt;&lt;img border="0" src="http://www.diesel-ebooks.com/mas_assets/full/0321564189.jpg" width="120" height="165" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Holding a copy of my new book in my hands was most pleasurable. Dinesh and I have been working on this text for so many months that I long ago ceased to believe that it would ever actually be published. Instead, I began to think of it simply as a device for stealing my weekends, and for keeping me up late at night when I should be sleeping. And of course, on those rare occasions when I was sleeping, it would bring me awake with a start, my brain repeating the anxious mantra: “The book is not done yet, and chapters are past due!”&lt;/p&gt;  &lt;p&gt;We worked hard on this book, and did our best to make it readable. I’m particular proud of the many chapters by Dinesh Kulkarni, the Program Manager who created LINQ to SQL. Dinesh is not only a wonderful human being, but also a brilliant and very hard working engineer. The chapters on LINQ to SQL that he contributed to this book are the best commentaries on the subject that I have ever read. He also contributed a chapter on LINQ to Entities, and an invaluable chapter on LINQ Best Practices. This latter chapter I consider essential reading for everyone who cares about LINQ.&lt;/p&gt;  &lt;p&gt;Both Dinesh and I believe in LINQ, and we both wanted to create a text that would be helpful, thorough and easy to understand. It is my hope that we make a good team, and that our skills complement one another. Certainly I enjoyed the opportunity to work closely with Dinesh. We met once a week for a nearly a year to discuss the book. Afterwards, we would often go to lunch, where we would eat vegetarian Indian food and talk about computers, politics, economics, and anything else that came into our head. I always enjoyed those discussions, and consider them one of the great benefits I derived from having the opportunity to help write Essential LINQ.&lt;/p&gt;  &lt;p&gt;Here are some links to the book:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.informit.com/store/product.aspx?isbn=0321564162"&gt;Publisher’s Site&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;On &lt;a href="http://www.amazon.com/Essential-LINQ-Microsoft-NET-Development/dp/0321564162/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1237620099&amp;amp;sr=8-1#"&gt;Amazon&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://my.safaribooksonline.com/9780321604750"&gt;Safari Books&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f03%2f21%2fessential-linq-published.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f03%2f21%2fessential-linq-published.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9495007" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author></entry><entry><title>Community Convergence XLVIII</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2009/01/13/community-convergence-xlviii.aspx" /><id>http://blogs.msdn.com/charlie/archive/2009/01/13/community-convergence-xlviii.aspx</id><published>2009-01-14T05:03:16Z</published><updated>2009-01-14T05:03:16Z</updated><content type="html">&lt;p&gt;Welcome to the 48th Community Convergence. The C# team continues to work hard to get out the next version of C#, and to add improvements to the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&amp;amp;displaylang=en"&gt;Visual Studio 2010&lt;/a&gt; IDE. Working long, fruitful hours on these rainy Washington State January days and nights, the engineers on our team are doing a great job putting together a set of features that will improve both C#, and the experience C# developers have in the Visual Studio IDE. They are also working to lay the foundation for some big improvements which you will see only after the next version of C# ships.&lt;/p&gt; &lt;p&gt;To keep up with the team's plans, you should:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Continue to follow LINQ  &lt;li&gt;Learn about the Dynamic Features planned for Visual Studio 2010  &lt;li&gt;Learn about the Code Focused IDE features planned for Visual Studio 2010&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Here are a few listings that will help you gain deeper insight into all of these important technology:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Videos&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://www.informit.com/podcasts/episode.aspx?e=ff719d1a-67c8-47c9-86f4-0d31f2723e6f"&gt;Anders Hejlsberg, Mads Torgersen and Eric Lippert discuss the new Features in C# 4.0, Part I&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://www.informit.com/podcasts/episode.aspx?e=4b0da588-4e09-4f4a-bb8a-2c1d7d2ff1f6"&gt;Anders Hejlsberg, Mads Torgersen and Eric Lippert discuss the new Features in C# 4.0, Part II&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Sam Ng&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2008/12/24/dynamic-in-c-vii-phantom-method-semantics.aspx"&gt;Dynamic in C# VII: Phantom Method Semantics&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx"&gt;Dynamic in C# VI: What Dynamic Does not Do&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Luca Bolognese&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2008/11/20/expression-tree-serialization-code-posted-on-code-gallery.aspx"&gt;Expression Tree Serialization and LINQ&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Eric Lippert&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/01/12/book-news.aspx"&gt;Eric has put together a short, but helpful description of how his fellow team members wrote about the C# Spec in the new book, &lt;strong&gt;The C# Programming Language Language&lt;/strong&gt;, Third Edition.&lt;/a&gt; The authors are Anders Hejlsberg, Scott Wiltamuth, Peter Golde, and Mads Torgersen. Other notable luminaries contributed annotations to the printed version of this text. All the authors are very talented and work hard, but In my opinion, Mads deserves a special note of thanks for the great work he did to help pull this edition of the book together.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Kirill Osenkov&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/01/10/call-hierarchy-navigation-in-visual-studio-2010.aspx"&gt;Call Hierarchy Navigation in Visual Studio 2010.&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2008/12/31/reflections-on-coding-style.aspx"&gt;Reflections on Coding Style&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Soma&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/somasegar/archive/2008/12/19/code-focused-development-in-vs-2010.aspx"&gt;New Features in Visual Studio 2010&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Matt Warren&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/mattwar/archive/2008/11/21/linq-iqtoolkit-on-codeplex.aspx"&gt;LINQ IQ Toolkit on Codeplex&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f01%2f13%2fcommunity-convergence-xlviii.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f01%2f13%2fcommunity-convergence-xlviii.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318012" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="Community Convergence" scheme="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx" /><category term="LINQ" scheme="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx" /><category term="Visual Studio" scheme="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx" /><category term="C# 4.0" scheme="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx" /></entry><entry><title>10-4 Episode 1: Working with the Visual Studio 2010 CTP</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2008/12/17/10-4-episode-1-working-with-the-visual-studio-2010-ctp.aspx" /><id>http://blogs.msdn.com/charlie/archive/2008/12/17/10-4-episode-1-working-with-the-visual-studio-2010-ctp.aspx</id><published>2008-12-18T01:58:51Z</published><updated>2008-12-18T01:58:51Z</updated><content type="html">&lt;p&gt;A new series of videos on the Visual Studio 2010 September CTP has been launched. The first episode covers installing and running the CTP, which comes in the form of a VPC.&lt;/p&gt; &lt;p&gt;The video is &lt;a href="http://channel9.msdn.com/shows/10-4/10-4-Episode-1-Working-with-the-Visual-Studio-2010-CTP-VPC/"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;URL's referenced in this episode:&lt;br&gt;&lt;a href="http://tinyurl.com/GetCTP"&gt;http://tinyurl.com/GetCTP&lt;/a&gt;&lt;br&gt;&lt;a href="http://tinyurl.com/VSFeedback"&gt;http://tinyurl.com/VSFeedback&lt;/a&gt;&lt;br&gt;&lt;a href="http://tinyurl.com/CTPActivation"&gt;http://tinyurl.com/CTPActivation&lt;/a&gt;  &lt;li&gt;For more 10-4 episodes, be sure to visit:&lt;br&gt;&lt;a href="http://channel9.msdn.com/Shows/10-4/"&gt;http://channel9.msdn.com/Shows/10-4/&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f12%2f17%2f10-4-episode-1-working-with-the-visual-studio-2010-ctp.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f12%2f17%2f10-4-episode-1-working-with-the-visual-studio-2010-ctp.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9233535" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author></entry><entry><title>Query Data with Parallel LINQ</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/charlie/archive/2008/12/15/query-data-with-parallel-linq.aspx" /><id>http://blogs.msdn.com/charlie/archive/2008/12/15/query-data-with-parallel-linq.aspx</id><published>2008-12-16T06:00:26Z</published><updated>2008-12-16T06:00:26Z</updated><content type="html">&lt;p&gt;This post shows a simple way to write code that takes advantage of multiple processors. You will see that LINQ queries can allow you to side step the difficult tasks normally involved in writing multi-threaded code. To get started, all you need is a little basic knowledge of how to write simple LINQ queries. &lt;p&gt;The code shown in this post uses a pre-release version of PLINQ called the Microsoft Parallel Extensions to .NET Framework 3.5. When PLINQ finally ships, it will run only on .NET 4.0 or later. The version I'm using that runs on top of 3.5 is for evaluation purposes only. There will never be a shipping version that runs on .NET 3.5.  &lt;p&gt;This LINQ provider is being created at Microsoft by the Parallel Computing team; it is not the work of the C# team that created LINQ to Objects and LINQ to SQL. Here is the website for the Parallel Computing team:  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/concurrency/"&gt;http://msdn.microsoft.com/en-us/concurrency/&lt;/a&gt;  &lt;p&gt;At the time of this writing, these extensions were available only in pre-release form. You could download them either as Visual Studio 2008 compatible extensions to .NET 3.5, or as part of the pre-release version of Visual Studio 2010. Since the download sites might change over the coming months, I suggest that you find these resources by going to the Parallel Computing site, or to the Visual Studio site:  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vs2008"&gt;http://msdn.microsoft.com/en-us/vs2008&lt;/a&gt;  &lt;p&gt;Parallel LINQ, or PLINQ, is only a small part of the Parallel Extensions to the .NET Framework. It is, however, an important part. Since it is a simple and natural extension of the LINQ syntax, I think developers familiar with that technology will find it easy to use.  &lt;p&gt;Consider this code:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = &lt;span style="color: rgb(43,145,175)"&gt;Enumerable&lt;/span&gt;.Range(1, 10000);

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; q = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list.AsParallel()
        &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x &amp;lt; 3300
        &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; x;

&lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; q)
{
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(x);
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;These lines look nearly identical to the code you have seen in many simple LINQ samples. The only significant difference is the call to &lt;strong&gt;AsParallel&lt;/strong&gt; at the end of the first line. Though we have often used type inference to hide the return type of a LINQ query, I'm going to pause and take a second look at this instance. Rather than returning IEnumerable&amp;lt;T&amp;gt;, this version of PLINQ returns &lt;strong&gt;IParallelEnumerable&lt;/strong&gt;&amp;lt;&lt;strong&gt;int&lt;/strong&gt;&amp;gt;:&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;IParallelEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; q = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list.AsParallel() etc….&lt;/pre&gt;
&lt;p&gt;In the near future, PLINQ queries of this type will probably return &lt;strong&gt;ParallelQuery&lt;/strong&gt;&amp;lt;&lt;strong&gt;int&lt;/strong&gt;&amp;gt;. Because this product is still evolving, it might be simplest to use var, at least during the pre-release phase, and let the compiler choose the type. That way you can save typing, avoid problems with anonymous types, and you need not concern yourself about changes in the API as the product develops. It is almost always appropriate to use &lt;strong&gt;var&lt;/strong&gt; to designate the return type of a LINQ query, and there are only special circumstances when you would do otherwise. 
&lt;p&gt;Here are the results from this first PLINQ query: 
&lt;div&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;2
1
3
4
6
512
5
7
513
8
12
514
9
13
515
10
14
516
11
15
517
16
72
518
17&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The numbers shown here are in a relatively random order because they are being returned from different threads. It is important to remember that the sequence of values returned by LINQ is not always guaranteed to be presented in a particular order. If Order is important in your code, you can add a call to &lt;strong&gt;AsOrdered&lt;/strong&gt; to the query after the call to &lt;strong&gt;AsParallel&lt;/strong&gt;. Alternatively, you could insert a &lt;strong&gt;GroupBy&lt;/strong&gt; clause to establish the desired ordering. Otherwise developers should assume that the ordering from a PLINQ query will be entirely random 
&lt;p&gt;Now that you understand the basics of Parallel LINQ, let’s move on to look at a more interesting example. Improved performance is the main reason to write code that can run in parallel. The program shown in this post uses a timer to demonstrate how PLINQ can improve performance in a program. 
&lt;p&gt;Performance improvements become more evident when our code has access to more processors. The code I show here runs faster on a two processor machine, but it really starts to come into its own on a four processor machine. Moving up to even more processors yields more powerful results. Here, for instance, are the results showing an improvement of 1.33 times when using two processors, and almost two times when using 4 processors: 
&lt;div&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;2 Processors = 1.44 x improvement:
Linear: 00:00:13.15
Parallels: 00:00:09.10

4 Processors = 1.96 x improvement:
Linear: 00:00:15.00
Parallel: 00:00:07.68&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These tests are being running against pre-release software, so these numbers are almost certain to change before release, and of course different machines will yield different results. Furthermore, the degree of improvement that you see is likely to change depending on the type of algorithm you run, the number of cores on your machine, the architecture of the machine, how many caches there are and how they’re laid out, etc. Though it is rare, some queries show superlinear performance enhancements. In other words, there is a greater than 4x speedup on a 4-core box. An improvement of 2 times, such as the one shown, or even a 3 time improvement, is common. 
&lt;p&gt;This sample program is called FakeWeatherData, and it is available for download from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;. It features a simple LINQ to XML query run against a file with 10,000 records in it. The data I'm querying is not real, but consists of random dates and temperatures generated by a simple algorithm included in the &lt;strong&gt;FakeWeatherData&lt;/strong&gt; program. 
&lt;p&gt;The XML file is structured like this:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;"&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;"&lt;span style="color: rgb(0,0,255)"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Samples&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1973&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;May&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;15&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;10&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1970&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;Feb&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;10&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;14&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1970&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;Jan&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;15&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;11&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;  ... Many lines of code omitted here&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Samples&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
&lt;/span&gt;&lt;font color="#0000ff"&gt;&lt;/font&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;There is also a simple C# class used by the program to encapsulate the data from the XML file: &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&lt;/span&gt;{
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; Year { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&lt;/span&gt;; }
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; Month { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&lt;/span&gt;; }
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; Day { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&lt;/span&gt;; }
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; Temperature { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&lt;/span&gt;; }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The parallel version of the query in the program looks like this: &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; NUM_REPS; i++)
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; doc.Root.Elements(&lt;span style="color: rgb(163,21,21)"&gt;"Sample"&lt;/span&gt;).AsParallel()
                &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"1973"&lt;/span&gt; &amp;amp;&amp;amp;
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"Apr"&lt;/span&gt; &amp;amp;&amp;amp;
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"15"
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&lt;/span&gt;                {
                    Day = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value,
                    Month = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value,
                    Temperature = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Temperature"&lt;/span&gt;).Value,
                    Year = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value
                }).ToList();

}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Accompanying this code is a similar LINQ query that does not use PLINQ &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; NUM_REPS; i++)
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; doc.Root.Elements(&lt;span style="color: rgb(163,21,21)"&gt;"Sample"&lt;/span&gt;)
                &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"1973"&lt;/span&gt; &amp;amp;&amp;amp; 
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"Apr"&lt;/span&gt; &amp;amp;&amp;amp; 
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"15"
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&lt;/span&gt;                {
                    Day = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value,
                    Month = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value,
                    Temperature = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Temperature"&lt;/span&gt;).Value,
                    Year = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value
                }).ToList();

}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The program queries the data in the XML file first using the Parallel code, then using standard LINQ. By comparing the time it takes each block of code to execute you can get a sense of the relative improvement available through PLINQ. I'll show you how to make such comparisons in just a moment. I will also discuss some tools that will become available to help profile code of this type. 
&lt;p&gt;You can see that the PLINQ query contains a call to &lt;strong&gt;AsParallel&lt;/strong&gt;, while the other query does not. Other than that the two queries are identical. The fact that the two queries look so much alike points to a primary strength of PLINQ: very little specialized knowledge is necessary in order to begin using it. This does not mean that the subject is trivial, but only that the barrier to entry is low. This is not the case with most concurrent programming models. 
&lt;p&gt;LINQ queries are designed to be read-only, working with immutable data. This is a good model for parallelism, because it makes it unlikely that data will mutate, thereby setting up the potential for a race condition. You should note, however, that PLINQ does nothing to prevent this from happening, it is simply that LINQ is designed to make it unlikely. 
&lt;p&gt;Note also that the declarative LINQ programming style ensures that developers specify what they want done, rather than how it should be done. This leaves PLINQ free to ensure that concurrent LINQ queries run in the safest manner possible. If LINQ had been defined more strictly, such that it had to process each element in a certain order, then the PLINQ team would have had a much more difficult task. 
&lt;p&gt;The code in both these queries pulls out only the records from the XML file that have their date set to April 15, 1973. Because of deferred execution, the query would not do anything if I did not call ToList(). As a result, I added that call and converted the result into a List&amp;lt;WeatherData&amp;gt;. Though hardly earthshaking in import, these calls ensure that the code actually does something, and thus gives PLINQ scope to take advantage of the multiple processers on your system. 
&lt;p&gt;Simple timers are created to measure the difference between the standard LINQ query and the PLINQ query. I've also used a method used in many of Parallel LINQ team's samples for displaying the time elapsed during a test run: &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunTest()
{
    &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt; doc = &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt;.Load(&lt;span style="color: rgb(163,21,21)"&gt;"XMLFile1.xml"&lt;/span&gt;);

    &lt;span style="color: rgb(43,145,175)"&gt;Stopwatch&lt;/span&gt; sw = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Stopwatch&lt;/span&gt;();

    sw.Start();
    LinqOrdinarie(doc);
    sw.Stop();
    ShowElapsedTime(&lt;span style="color: rgb(163,21,21)"&gt;"Ordinaire"&lt;/span&gt;, sw.Elapsed);

    sw.Reset();

    sw.Start();
    ParallelLinq(doc);
    sw.Stop();
    ShowElapsedTime(&lt;span style="color: rgb(163,21,21)"&gt;"Parallels"&lt;/span&gt;, sw.Elapsed);
}
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,128,0)"&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;TimeSpan&lt;/span&gt; ShowElapsedTime(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; caption, &lt;span style="color: rgb(43,145,175)"&gt;TimeSpan&lt;/span&gt; ts)
{
    &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; elapsedTime = &lt;span style="color: rgb(43,145,175)"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163,21,21)"&gt;"{0}: {1:00}:{2:00}:{3:00}.{4:00}"&lt;/span&gt;,
        caption, ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(elapsedTime, &lt;span style="color: rgb(163,21,21)"&gt;"RunTime"&lt;/span&gt;);
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; ts;
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;At least with the pre-release version of PLINQ that I've played with, I've found it very useful to set up timers to confirm that PLINQ is actually able to speed up an operation. My record at guessing which code will benefit from running in parallel is not good, and so I find that confirming the effectiveness of the code by explicitly measuring it is worthwhile. You can either use the simple StopWatch class from the System.Diagnostics namespace, as shown here, or else you can use a profiler. Note that a thread aware profiler might ship with some versions of Visual Studio 2010. 
&lt;p&gt;I've found that the advantages of concurrent LINQ become more obvious the longer the operation I'm timing lasts. As a result, I've placed the query inside a loop, and added a variable to the program called NUM_REPS. By setting NUM_REPS to a large number, say 500, you can clearly see the benefits that can be accrued when you run LINQ queries in parallel on multiple processors. Note that the first time PLINQ is used, its assembly will need to be loaded, the relevant types will need to be JIT compiled, and new threads will need to be spun up, etc. As a result, many developers see improved performance after they get past the initial warm-up time. 
&lt;p&gt;Though it is very easy to get started with PLINQ, there are still complexities inherent in the subject that you need to consider. For instance, PLINQ will sometimes develop a different partitioning scheme for your data depending on whether you are working with an Enumerable or an Array. To learn more about this subject, see the following post from the Parallel Programming team: 
&lt;p&gt;&lt;a href="http://blogs.msdn.com/pfxteam/archive/2007/12/02/6558579.aspx"&gt;http://blogs.msdn.com/pfxteam/archive/2007/12/02/6558579.aspx&lt;/a&gt; 
&lt;p&gt;The simple PLINQ examples shown in this post should help you get started with this powerful and interesting technology. Parallel LINQ is still in its infancy, but already it provides means of greatly simplifying tasks that are not normally easy to perform.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f12%2f15%2fquery-data-with-parallel-linq.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f12%2f15%2fquery-data-with-parallel-linq.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9224144" width="1" height="1"&gt;</content><author><name>ccalvert</name><uri>http://blogs.msdn.com/members/ccalvert.aspx</uri></author><category term="CSharp" scheme="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx" /><category term="LinqFarm" scheme="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx" /><category term="Parallel" scheme="http://blogs.msdn.com/charlie/archive/tags/Parallel/default.aspx" /></entry></feed>