<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Coding4Fun : Silverlight</title><link>http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx</link><description>Tags: Silverlight</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>bugs on your screen?</title><link>http://blogs.msdn.com/coding4fun/archive/2009/12/07/9932678.aspx</link><pubDate>Mon, 07 Dec 2009 18:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932678</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9932678.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9932678</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9932678</wfw:comment><description>&lt;p&gt;&lt;a href="http://adamkinney.wordpress.com/2009/12/03/bugcamsmash-motion-detection-with-silverlight-4-beta/"&gt;Adam Kinney created a fun example squishing bugs to show off Silverlight 4.0’s webcam support&lt;/a&gt;.&amp;#160; He created simple motion detection using a diff calculation on multiple threads to figure out where his finger was to smooth the bugs!&amp;#160; Here is a video showing off the project.&lt;/p&gt;  &lt;p&gt;&lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="384"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_11_30.xap" /&gt; &lt;param name="initParams" value="deferredLoad=true,duration=0,m=http://ecn.channel9.msdn.com/o9/ch9/3/1/5/0/1/5/BugCamSmash_ch9.wmv,autostart=false,autohide=true,showembed=true, postid=510513" /&gt; &lt;param name="background" value="#00FFFFFF" /&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /&gt; &lt;/a&gt; &lt;/object&gt;&lt;/p&gt;  &lt;p&gt;If you want to check out the &lt;a href="http://adamkinney.wordpress.com/2009/12/03/bugcamsmash-motion-detection-with-silverlight-4-beta/"&gt;source code for BugCamSmash&lt;/a&gt;, Adam has that along with a few other helpful hints at his blog post.&lt;/p&gt;  &lt;p&gt;In addition, Adam is posting articles on how he created the application as well.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://adamkinney.wordpress.com/2009/12/04/bugcamsmash-dissected-part-1-smash-a-bug/"&gt;Smash a bug&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://adamkinney.wordpress.com/2009/12/05/bugcamsmash-dissected-part-2-make-the-bugs-crawl/"&gt;Make the bugs crawl&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://adamkinney.wordpress.com/2009/12/07/bugcamsmash-dissected-part-3-give-the-bugs-brains/"&gt;Giving the bugs Brains&lt;/a&gt;&lt;/li&gt; &lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932678" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Beta/default.aspx">Beta</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight+4.0/default.aspx">Silverlight 4.0</category></item><item><title>Creating a Pinball Game in Silverlight: Using the Physics Helper Library + Farseer Physics</title><link>http://blogs.msdn.com/coding4fun/archive/2009/11/30/9896181.aspx</link><pubDate>Mon, 30 Nov 2009 22:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9896181</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9896181.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9896181</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9896181</wfw:comment><description>&lt;p&gt;In this tutorial, we'll create a Silverlight pinball game using &lt;u&gt;Behaviors&lt;/u&gt;&lt;b&gt;, &lt;/b&gt;a new addition to Expression Blend 3 &amp;amp; Silverlight that allows you to create interactivity with little or no coding. &lt;/p&gt;  &lt;p&gt;Below you'll find a video and step-by-step walkthrough. &lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="157"&gt;Andy Beaulieu          &lt;br /&gt;&lt;a title="http://www.andybeaulieu.com/" href="http://www.andybeaulieu.com/"&gt;http://www.andybeaulieu.com/&lt;/a&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" width="481"&gt;&lt;strong&gt;Run It: &lt;/strong&gt;&lt;a href="http://www.andybeaulieu.com/silverlight/3.0/pinballgame/Coding4FunPinball.htm"&gt;View The Demo&lt;/a&gt;           &lt;br /&gt;&lt;strong&gt;Code It: &lt;/strong&gt;&lt;a href="http://physicshelper.codeplex.com/Release/ProjectReleases.aspx"&gt;Download the Source&lt;/a&gt;           &lt;br /&gt;          &lt;br /&gt;&lt;strong&gt;Difficulty: &lt;/strong&gt;Intermediate           &lt;br /&gt;&lt;b&gt;Time Required:&lt;/b&gt; 4 Hour           &lt;br /&gt;&lt;strong&gt;Cost: &lt;/strong&gt;Free!           &lt;br /&gt;&lt;strong&gt;Software Needed: &lt;/strong&gt;&lt;a href="http://www.microsoft.com/expression/products/Blend_Overview.aspx"&gt;Microsoft Expression Blend 3&lt;/a&gt; (Free Trial), Microsoft Visual Studio 2008 or &lt;a href="http://www.microsoft.com/express/vcsharp/"&gt;Visual C# Express&lt;/a&gt;, &lt;a href="http://www.silverlight.net/GetStarted"&gt;Visual Studio Tools for Silverlight&lt;/a&gt;, &lt;a href="http://physicshelper.codeplex.com"&gt;Physics Helper Library&lt;/a&gt;.&amp;#160; To help get setup quicker, try &lt;a href="http://www.microsoft.com/web"&gt;Web Platform Installer&lt;/a&gt;.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt; &lt;iframe height="500" src="http://www.coding4fun.net/media/slpinball/slpinball.html" frameborder="0" width="100%"&gt;&lt;/iframe&gt;&lt;a href="http://www.andybeaulieu.com/silverlight/3.0/pinballgame/coding4funtutorial.htm#_Toc237838546"&gt;&lt;/a&gt;  &lt;br /&gt;  &lt;h3 align="center"&gt;&lt;a href="http://www.coding4fun.net/media/slpinball/slpinball.html" target="_blank"&gt;Open the video player in a new window/tab&lt;/a&gt;&lt;/h3&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;&lt;a name="_Toc237838538"&gt;Setup and Prerequisites&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;First you have to setup Physics Helper controls into Expression Blend. Follow these steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download the Physics Helper ZIP file from &lt;a href="http://physicshelper.codeplex.com/Release/ProjectReleases.aspx"&gt;http://physicshelper.codeplex.com/Release/ProjectReleases.aspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Extract the ZIP file contents and dump them into a folder. &lt;/li&gt;    &lt;li&gt;Run &lt;b&gt;install.bat&lt;/b&gt;. (Note: this copies a few required assemblies to a folder in Blend 3.&lt;b&gt;)&lt;/b&gt;       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image001.jpg" /&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;&lt;a name="_Toc237838539"&gt;Getting Started &amp;amp; Making the Ball&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;Next, you create the controller and the pinball. Here's how.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Open Expression Blend 3 and create a new “Silverlight 3 Application + Website” named PinballGame.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image002.jpg" width="400" height="324" /&gt; &lt;/li&gt;    &lt;li&gt;Change your default layout container to a Canvas. You can do this in the Objects and Timeline panel by right-clicking LayoutRoot and selecting Change Layout Type/Canvas. Canvas layout containers are better for games because they allow for positioning of elements at absolute (x,y) coordinates.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image003.jpg" width="290" height="266" /&gt; &lt;/li&gt;    &lt;li&gt;Give the application a bigger default size. Select the UserControl element in the Objects and Timeline panel. Then, in the Properties panel, change the Width to 800 and the Height to 600.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image004.png" width="269" height="190" /&gt; &lt;/li&gt;    &lt;li&gt;Add a &lt;b&gt;Physics Controller Behavior&lt;/b&gt; to our main game Canvas. From the Asset Panel, find the PhysicsController Behavior and drag it to the Objects and Timeline Panel. Then, drop it on the LayoutRoot Canvas.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image005.jpg" width="233" height="253" /&gt;       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image006.jpg" width="233" height="122" /&gt; &lt;/li&gt;    &lt;li&gt;Change a few of the PhysicsController's properties, to make it easier to use. In the Objects and Timeline Panel, select the PhysicsControllerBehavior assigned to LayoutRoot. Then, set the highlighted properties as follows in the Properties Window:      &lt;br /&gt;1.)&amp;#160; Decrease the vertical gravity to 250       &lt;br /&gt;2.)&amp;#160; Increase the iterations to 200. This will help with collision detection in a fast-action game.       &lt;br /&gt;3.)&amp;#160; Set MousePickEnabled to true. This will allow us to manipulate objects with the mouse during development and testing.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image007.png" width="261" height="218" /&gt; &lt;/li&gt;    &lt;li&gt;Create a &amp;quot;playing field&amp;quot; for the pinball. To do this, draw out a Rectangle onto the artboard, just off the bottom of the user control bounds. Set the properties of the Rectangle as follows:      &lt;br /&gt;      &lt;br /&gt;&lt;b&gt;Name&lt;/b&gt;: rectPlatform       &lt;br /&gt;&lt;b&gt;Fill&lt;/b&gt;: select a color of your choosing (light blue is selected below).       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image008.jpg" width="364" height="317" /&gt; &lt;/li&gt;    &lt;li&gt;To make “rectPlatform” a physics element, we need to add a PhysicsObjectBehavior to it. Find the PhysicsObjectBehavior in the Assets panel and drag/drop it onto rectPlatform on the Artboard.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image009.png" width="343" height="369" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, select the PhysicsObjectBehavior belonging to rectPlatform. Then in the Properties Panel, set the IsStatic property to True so that the platform will stay in place.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image010.png" width="264" height="173" /&gt; &lt;/li&gt;    &lt;li&gt;Next we’ll create the ball. Draw out an Ellipse near the very top of the UserControl and set the following properties:      &lt;br /&gt;&lt;b&gt;       &lt;br /&gt;Name: &lt;/b&gt;ellBall&lt;b&gt;        &lt;br /&gt;Height&lt;/b&gt;: 50       &lt;br /&gt;&lt;b&gt;Width&lt;/b&gt;: 50       &lt;br /&gt;&lt;b&gt;Fill&lt;/b&gt;: A color of your choosing.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image011.jpg" width="378" height="326" /&gt; &lt;/li&gt;    &lt;li&gt;To make the ball a Physics Object, we need to add a PhysicsObjectBehavior to it. From the Asset Library, drag and drop a PhysicsObjectBehavior onto ellBall. &lt;/li&gt;    &lt;li&gt;Run the project by clicking F5. The ball should fall and hit the platform. Use the mouse to manipulate the ball. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;&lt;a name="_Toc237838540"&gt;Making the Flippers&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;In a pinball game, the Flippers are controlled by the player and are used to move the ball up the playfield. Standard pinball games have one right and one left flipper, but many games have three or more flippers located in various locations on the playfield. We’ll create two user controls to represent the flippers – a right flipper and a left flipper – that way we can easily add as many flippers as we want to our main playfield.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;In the Projects Panel, right-click the PinballGame (Silverlight) project and select Add New Item. Select UserControl and name the control LeftFlipper.xaml.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image012.jpg" width="222" height="241" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, right-click LayoutRoot and select Change Layout Type/Canvas. This is required for the Physics Helper to work with nested User Controls.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image013.jpg" width="256" height="281" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, select the UserControl element and set its Width and Height to 200 x 200 pixels in the Properties Panel. &lt;/li&gt;    &lt;li&gt;Draw out a Rectangle that is about 200 x 50 pixels big, centered in the user control. Round the corners of the Rectangle.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image014.png" width="235" height="185" /&gt; &lt;/li&gt;    &lt;li&gt;From the main Blend menu, select Object/Path/Convert to Path. This will create a new Path element from the Rectangle primitive. Then select the Direct Selection tool from the Toolbox, which will allow you to manipulate the individual points on the Path.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image015.png" width="37" height="84" /&gt; &lt;/li&gt;    &lt;li&gt;Select the top, left two points on the Path and move them down so that you end up with a more “flipper like” shape:      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image016.png" width="224" height="171" /&gt; &lt;/li&gt;    &lt;li&gt;In the Toolbox, select the Selection arrow and rotate the flipper 45 degrees so that it looks similar to this (be sure the flipper is still within the bounds of the user control):      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image017.jpg" width="196" height="186" /&gt; &lt;/li&gt;    &lt;li&gt;Right-click the flipper Path and select Group Into/Canvas. Name the Canvas &lt;b&gt;cnvLeftFlipper&lt;/b&gt; and resize it so that it surrounds the entire flipper Path. Note that you could “embellish” the cnvLeftFlipper with additional elements if you wish. &lt;/li&gt;    &lt;li&gt;Next we’ll create a Joint so that the Flipper can rotate on an axis. We’ll need something to anchor the joint to, so first let’s add a small Static object to hold the Joint in place. Draw out a Rectangle at the upper left of the flipper and name the Rectangle &lt;b&gt;rectHolder&lt;/b&gt;.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image018.jpg" width="196" height="197" /&gt; &lt;/li&gt;    &lt;li&gt;To add a Joint, we will need a UI element to visually represent where we want the joint created. You can use any element you want for this, but an Ellipse works pretty well. Draw out an Ellipse on top of the rectHolder.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image019.jpg" width="196" height="197" /&gt; &lt;/li&gt;    &lt;li&gt;Now we can add all of the required Physics Behaviors to the User Control.      &lt;ol&gt;       &lt;li&gt;From the Asset Library, drag a PhysicsObjectBehavior to the rectHolder Rectangle. Set its IsStatic property to True. Since we don’t really want to see the rectHolder in our UI, just send it to the back of the elements (Right-click, then select Order/Send to Back). &lt;/li&gt;        &lt;li&gt;Drag a PhysicsObjectBehavior to the cnvLeftFlipper Canvas. Set the RestitutionCoefficent property to “0.1”. (This makes the flipper a bit more “bouncy” on collisions) &lt;/li&gt;        &lt;li&gt;Drag a PhysicsJointBehavior to the Ellipse. Set the Body1 property to “cnvLeftFlipper” and the Body2 property to “rectHolder”. Set the CollisionGroup to “1” and the AngleLimitLower and AngleLimitUpper to 0 and 50 respectively. Also enable the AngleSpring:          &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Drag a PhysicsApplyTorqueBehavior to cnvLeftFlipper. We want to apply Torque when the user presses the Left arrow key, so we’ll create a Trigger for this. Select the Behavior and in the Properties Panel, click the New Trigger button.          &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image021.png" width="267" height="191" /&gt;&amp;#160; &lt;br /&gt;          &lt;br /&gt;Select the PhysicsKeyTrigger type from the popup.           &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image022.jpg" width="197" height="296" /&gt;           &lt;br /&gt;Set the Key property for the Trigger to “Left” and the TorqueValue to -100000           &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image023.png" width="264" height="187" /&gt; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Build the Project so that the LeftFlipper user control is available. &lt;/li&gt;    &lt;li&gt;Open MainPage.xaml and drag an instance of LeftFlipper from the Asset Library onto the Artboard. Position it near the bottom of the user control and slightly to the Left.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image024.jpg" width="373" height="321" /&gt; &lt;/li&gt;    &lt;li&gt;Run the project and try the flipper with the Left arrow key. &lt;/li&gt;    &lt;li&gt;In this step, we’ll create a copy of “LeftFlipper” to make&amp;#160; a “RightFlipper”. In the Projects Panel, right-click LeftFlipper.xaml and select Copy. Then right-click the PinBallGame Project and select Paste.      &lt;br /&gt;You now have a new User Control named “Copy of LeftFlipper.xaml”. Rename this to RightFlipper.xaml. Then in the code-behind file, make sure the Class and Constructor are also renamed to “RightFlipper”.       &lt;br /&gt;Change to XAML view for RightFlipper.xaml and change the x:Class attribute so that it inherits from RightFlipper:&amp;#160; &lt;br /&gt;      &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:x&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:d&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:mc&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;mc:Ignorable&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;d&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:i&lt;/span&gt;=&amp;amp;&lt;span style="color: #ff0000"&gt;amp&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;quotclr-namespace:System&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Windows&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Interactivity&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;assembly&lt;/span&gt;=&lt;span style="color: #ff0000"&gt;System&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Windows&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Interactivity&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;    xmlns:pb=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;clr-namespace:Spritehand&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;PhysicsBehaviors&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;assembly&lt;/span&gt;=&lt;span style="color: #ff0000"&gt;Spritehand&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;PhysicsBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;x:Class=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;PinballGame&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;RightFlipper&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;d:DesignWidth=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;640&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; d:DesignHeight=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;480&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; Height=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;200&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; Width=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;200&lt;/span&gt;&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
Open RightFlipper.xaml and find the pathLeftFlipper element. Rename this to pathRightFlipper. Then, rename “cnvLeftFlipper” to “cnvRightFlipper”. Rename “rectHolder” to “rectRightHolder” and position the rectHolder and joint ellipse so that they are in the correct location for the flipper: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image026.png" width="163" height="159" /&gt; 

    &lt;br /&gt;Modify the PhysicsJointBehavior for the joint in the Objects and Timeline Panel. Change BodyOne to “cnvRightFlipper”, BodyTwo to “rectRightHolder” and change the Angle limits so they are appropriate for the opposite flipper: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image027.png" width="264" height="219" /&gt; 

    &lt;br /&gt;Select the PhysicsApplyTorqueBehavior for cnvRightFlipper in the Objects and Timeline Panel. Change the Key property for the trigger to “Right” so that it will initiate on the Right Arrow key down. Also change the TorqueValue so that it will apply torque clockwise: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image028.png" width="265" height="190" /&gt; 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;Add an instance of RightFlipper onto the MainPage.xaml and position it to the right of LeftFlipper. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image029.jpg" width="372" height="325" /&gt; &lt;/li&gt;

  &lt;li&gt;Test the project by clicking F5. You can now click left and right arrows keys to manipulate the paddles. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838541"&gt;Creating the Playfield + Scrolling&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Now let’s create more of a playfield for our Pinball game and add a Camera control so that the game will scroll and always follow the pinball. Feel free to tweak the design as you wish.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open MainPage.xaml in Expression Blend and zoom out by using Ctrl plus minus (Ctrl -). &lt;/li&gt;

  &lt;li&gt;Draw out a Path using the Pen tool for the right border. Note that you can extend above the upper bounds of the User Control. Group this into a Canvas named “cnvLeftBorder” (right click the Path and select Group Into/Canvas). &lt;/li&gt;

  &lt;li&gt;Drag a PhysicsObjectBehavior from the Asset Library onto cnvLeftBorder and set the IsStatic property to True. Also set the CollisionGroup property to “1” so that collisions will not occur between the border and the Flippers. &lt;/li&gt;

  &lt;li&gt;Repeat Steps 2 and 3 above for the right border, naming that cnvRightBorder. You should have something similar to the following: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image030.jpg" width="281" height="267" /&gt; &lt;/li&gt;

  &lt;li&gt;Continue adding border controls and applying physics behaviors until you are happy with your playfield. Make the playfield completely closed in so that the ball cannot escape (except by the bottom trap of course!) &lt;/li&gt;

  &lt;li&gt;You can also add more LeftFlipper and RightFlipper controls so that the player can more easily move the ball up the playfield. Here is one example playfield design: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image031.jpg" width="238" height="336" /&gt; &lt;/li&gt;

  &lt;li&gt;To enable scrolling, we can add a Camera Controller to the ball, causing the camera to follow the ball around the screen. Drag a PhysicsCameraBehavior from the Asset Library onto the ellBall element. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838542"&gt;Creating a Kicking Target&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Pinball games have lots of different types of targets – some are simple sensors which give points on contact, others are little toys that gobble up the ball for a few seconds. In this step, we’ll create a Kicking Target, which gives points when hit by the ball but also kicks the ball back in the opposite direction.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a new User Control named KickingTarget. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image032.jpg" width="203" height="220" /&gt; &lt;/li&gt;

  &lt;li&gt;Change the LayoutRoot container to a Canvas by right-clicking and selecting Change Layout Type/Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image033.jpg" width="255" height="234" /&gt; &lt;/li&gt;

  &lt;li&gt;Select the [UserControl] in the Objects and Timeline Panel and set its Width and Height to 100 x 100. &lt;/li&gt;

  &lt;li&gt;Draw out an Ellipse that is 90x90 pixels, and positioned at Left, Top 5,5 (so that it is centered in the control). 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image034.png" width="484" height="141" /&gt; &lt;/li&gt;

  &lt;li&gt;Draw out a second Ellipse that is 70x70 pixels and positioned and Left, Top 15, 15. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image035.png" width="131" height="130" /&gt; &lt;/li&gt;

  &lt;li&gt;Group the two Ellipses into a Canvas by selecting them both in the Objects and Timeline Panel and selecting Group Into/Canvas. Name the Canvas “cnvKicker”. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image036.png" width="281" height="204" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag a PhysicsObjectBehavior from the Asset Library onto cnvKicker. Set the IsStatic and RestitutionCoefficient properties as shown below. Setting the RestitutionCoefficient above a value of 1 will make the object “kick” things back. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image037.png" width="265" height="174" /&gt; &lt;/li&gt;

  &lt;li&gt;Build the project so that the new user control is available. Then open MainPage.xaml and add a few instance of “KickingTarget” to the page. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image038.jpg" width="208" height="296" /&gt; &lt;/li&gt;

  &lt;li&gt;Run the project and try out the Kicking Targets. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838543"&gt;Optimizing Performance&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Our pinball game is looking up, but the performance could be a lot better. The startup time for the game is taking quite awhile because the Physics Helper Library is determining the outline of all of the shapes. Also, the frame rate is too low.&lt;/p&gt;

&lt;p&gt;By default, Silverlight has a target frame rate of 60 frames per second. This is great for a lot of casual games, but Pinball requires a bit more speed. Additionally, Silverlight 3 introduces GPU Acceleration which can greatly increase the performance of our game by offloading graphics operations to the Video Card.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open the Default.html page in the website project. This page hosts the Silverlight control and exposes the plugin parameters. Add the following parameters to the plug in to increase the default frame rate and enable GPU Acceleration for our game. 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;object&lt;/span&gt; &lt;span style="color: #ff0000"&gt;data&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;data:application/x-silverlight,&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;application/x-silverlight-2&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;800&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;600&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;source&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;ClientBin/PinballGame.xap&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;onerror&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;onSilverlightError&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;background&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;#010141&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;minRuntimeVersion&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;3.0.40624.0&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;MaxFrameRate&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;160&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;EnableGPUAcceleration&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;EnableCacheVisualization&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;autoUpgrade&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt; &lt;span style="color: #ff0000"&gt;href&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://go.microsoft.com/fwlink/?LinkID=149156&amp;amp;ampv=3.0.40624.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;style&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;text-decoration: none;&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;img&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://go.microsoft.com/fwlink/?LinkId=108181&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;alt&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Get Microsoft Silverlight&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;style&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;border-style: none&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;object&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Apply the CacheMode attribute to &lt;b&gt;&lt;i&gt;all elements&lt;/i&gt;&lt;/b&gt; in the game that are static. Since our pinball game’s elements are not animated, we can apply the CacheMode to each of them. (If the objects contained animations within them, then the Cache would be invalidated during animation). 

    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Rectangle&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;rectPlatform&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;CacheMode&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;BitmapCache&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Fill&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;#FF8AD0C9&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;70&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;800&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Canvas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Left&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;-14&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Canvas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Top&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;2172&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Opacity&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;i:Interaction.Behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;pb:PhysicsObjectBehavior&lt;/span&gt; &lt;span style="color: #ff0000"&gt;IsStatic&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;i:Interaction.Behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Rectangle&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Now we’ll speed up the startup time by adding in a pre-calculated Point Cache. The Physics Helper Library echoes out the calculated points at runtime. Open the project in Visual Studio, and run with debugging on. In the Output Window, Find the ReadBoundaryCache method that was echoed out and Copy the entire method. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image041.jpg" width="523" height="218" /&gt; &lt;/li&gt;

  &lt;li&gt;Note that the copied code can contain multiple rows of the same points. You can trim out any rows that are duplicated, for example cnvRightFlipper_1 is a copy of cnvRightFlipper, so you can remove that from the list of points. &lt;/li&gt;

  &lt;li&gt;In order to load the list of boundary outlines into the Physics Controller, we need to get a reference to the controller through code. Open up MainPage.xaml.cs and add the highlighted code: 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPage : UserControl &lt;br /&gt;{ &lt;br /&gt;    PhysicsControllerMain _physicsController; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage() &lt;br /&gt;    { &lt;br /&gt;        &lt;span style="color: #008000"&gt;// Required to initialize variables &lt;/span&gt;&lt;br /&gt;        InitializeComponent(); &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Loaded += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RoutedEventHandler(MainPage_Loaded); &lt;br /&gt;    } &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MainPage_Loaded(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e) &lt;br /&gt;    { &lt;br /&gt;        _physicsController = LayoutRoot.GetValue( PhysicsControllerMain.PhysicsControllerProperty) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; PhysicsControllerMain; &lt;br /&gt;        BoundaryCache.ReadBoundaryCache(_physicsController); &lt;br /&gt;    } &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Run the project and test the performance. Note that items that have a tint applied to them (Red, Blue, etc.) are NOT being cached and GPU accelerated. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838544"&gt;Scoring&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;In this section, we’ll embellish our game with a Score.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We’ll create a Canvas that overlays our main Canvas to contain a High Score TextBlock. This is so that the Score TextBlock will not scroll with the rest of the Playfield. Open MainPage.xaml and the select LayoutRoot in the Objects and Timeline Panel. Right-click LayoutRoot and select Group Into/Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image043.jpg" width="227" height="200" /&gt; &lt;/li&gt;

  &lt;li&gt;Add a new StackPanel into this outside Canvas that contains two TextBlock aligned Horizontally. The first TextBlock should contain the Text “Score:” and the second TextBlock should contain the Text “0”. Set the Font Size to a larger value, around 12 pt. Position the TextBlock at the Top, Left portion of the Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image044.jpg" width="367" height="273" /&gt; &lt;/li&gt;

  &lt;li&gt;Next we’ll handle the Collision event for the Physics Controller so we can add to the score. Open MainPage.xaml.cs and add in a Property wrapper for the current&amp;#160; score: 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Score &lt;br /&gt;{ &lt;br /&gt;    get &lt;br /&gt;    { &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Convert.ToInt32(txtScore.Text); &lt;br /&gt;    } &lt;br /&gt;    set &lt;br /&gt;    { &lt;br /&gt;        txtScore.Text = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;.ToString(); &lt;br /&gt;    } &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Handle the Collision event for the Physics Controller and increment the Score. 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MainPage_Loaded(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e) &lt;br /&gt;{ &lt;br /&gt;    _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; PhysicsControllerMain; &lt;br /&gt;    BoundaryCache.ReadBoundaryCache(_physicsController); &lt;br /&gt;        _physicsController.Collision += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PhysicsControllerMain.CollisionHandler(_physicsController_Collision); &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; _physicsController_Collision(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite1, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite2) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (sprite1 == &amp;quot;ellBall&lt;span style="color: #006080"&gt;&amp;quot; &amp;amp;&amp;amp; sprite2.StartsWith(&amp;quot;ellKicker&amp;quot;&lt;/span&gt;)) &lt;br /&gt;        Score += 10; &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838545"&gt;Tracking Lives&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;When the ball collides with the “rectPlatform” obstacle at the bottom, the ball has been lost and we should launch a new ball. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Add a new ChildWindow to the project named “LostTheBall.xaml”. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image047.jpg" width="269" height="292" /&gt; &lt;/li&gt;

  &lt;li&gt;Set the “Cancel” button’s Visibility to Collapsed. &lt;/li&gt;

  &lt;li&gt;Add a TextBlock and set its Text to a message for the user stating the ball was lost. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image048.jpg" width="320" height="239" /&gt; &lt;/li&gt;

  &lt;li&gt;Add code in MainPage.xaml.cs to display the dialog and reset the ball. 
    &lt;br /&gt;LostTheBall _lostTheBall; 

    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; _physicsController_Collision(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite1, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite2) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (sprite1 == &lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; sprite2.StartsWith(&lt;span style="color: #006080"&gt;&amp;quot;cnvKicker&amp;quot;&lt;/span&gt;)) &lt;br /&gt;    { &lt;br /&gt;        Score += 10; &lt;br /&gt;    } &lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_lostTheBall == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;ampsprite1 == &lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; sprite2 == &lt;span style="color: #006080"&gt;&amp;quot;rectPlatform&amp;quot;&lt;/span&gt;) &lt;br /&gt;    { &lt;br /&gt;        _lostTheBall = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; LostTheBall(); &lt;br /&gt;        _lostTheBall.Closed += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; EventHandler(dialog_Closed); &lt;br /&gt;        _lostTheBall.Show(); &lt;br /&gt;    } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; dialog_Closed(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, EventArgs e) &lt;br /&gt;{ &lt;br /&gt;    _lostTheBall = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;; &lt;br /&gt;    PhysicsSprite ball = _physicsController.PhysicsObjects[&lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt;]; &lt;br /&gt;    ball.BodyObject.Position = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Vector2(460, 430); &lt;br /&gt;    }&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838546"&gt;Adding Sound Effects&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;We can easily add buffered sound effects using the PhysicsSoundBehavior.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;In Visual Studio, Import two sound files into the project, score.wma and click.wma. Set their Build Action to Content. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image050.jpg" width="220" height="315" /&gt; &lt;/li&gt;

  &lt;li&gt;Open MainPage.xaml in Expression Blend. Drag a PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image051.png" width="268" height="288" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag another PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image052.png" width="265" height="263" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag another PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image053.png" width="267" height="263" /&gt; &lt;/li&gt;

  &lt;li&gt;Run the project. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;About the Author&lt;/h3&gt;

&lt;p&gt;Andy Beaulieu is a software developer and trainer who is well versed in many Microsoft technologies including Silverlight, ASP.NET, ADO.NET and WindowsForms. &lt;a href="http://www.andybeaulieu.com"&gt;Visit Andy's Blog&lt;/a&gt; for more fun and games with Silverlight.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9896181" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Silverlight Development</title><link>http://blogs.msdn.com/coding4fun/archive/2009/11/18/9922257.aspx</link><pubDate>Wed, 18 Nov 2009 23:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9922257</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9922257.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9922257</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9922257</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/SilverlightDevelopment_9377/sliverPlusLight_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="sliverPlusLight" border="0" alt="sliverPlusLight" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/SilverlightDevelopment_9377/sliverPlusLight_thumb.jpg" width="483" height="164" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;With Silverlight 4.0 just announced, here are some great posts / helpful tips on getting started with Silverlight in general.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://r.ch9.ms/sl4cl"&gt;Channel9 has a training course&lt;/a&gt;.&lt;/li&gt;    &lt;li&gt;Tim Heuer has a 7-part series “&lt;a href="http://timheuer.com/blog/articles/getting-started-with-silverlight-development.aspx"&gt;Getting Started with Silverlight&lt;/a&gt;”.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.shinedraw.com/"&gt;Shine Draw is an awesome “Flash vs Silverlight” repository&lt;/a&gt; that can show you how to do an event in either technology.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://silverlight.net/"&gt;Silverlight.net&lt;/a&gt; has an abundance of information.&amp;#160; They also have &lt;a href="http://silverlight.net/community/samples/silverlight-samples"&gt;Community Samples&lt;/a&gt; as well.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9922257" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Getting+Started/default.aspx">Getting Started</category></item><item><title>Facebook SDK</title><link>http://blogs.msdn.com/coding4fun/archive/2009/11/11/9920962.aspx</link><pubDate>Wed, 11 Nov 2009 18:26:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9920962</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9920962.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9920962</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9920962</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/FacebookSDK_92C5/facebook_logo%5B1%5D_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="facebook_logo[1]" border="0" alt="facebook_logo[1]" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/FacebookSDK_92C5/facebook_logo%5B1%5D_thumb.png" width="240" height="240" /&gt;&lt;/a&gt;Microsoft just released a Facebo ok client library to help make it easier for everyone to create some interesting applications.&amp;#160; The SDK is broken into a few core DLLs, each serving a core purpose.&amp;#160; You can download the SDK from &lt;a title="Download the SDK" href="http://facebooktoolkit.codeplex.com/"&gt;Download the SDK&lt;/a&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Facebook.dll&lt;/strong&gt;: This is the main assembly that will be used by all applications. This has all the logic to handle communication with the Facebook application. This assembly also has specific support of XAML applications (Silverlight and WPF) to enhance the Facebook platform to make databinding and data caching easier. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Facebook.Silverlight.dll&lt;/strong&gt;: This is the Silverlight version of the main assembly that will be used by all Silverlight applications. This has all the logic to handle communication with the Facebook application. This assembly also has specific support of XAML applications to enhance the Facebook platform to make databinding and data caching easier. The REST API in this assembly is Asynchronous only. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Facebook.Web.dll&lt;/strong&gt;: This assembly should be used by Canvas applications. The main functionality supported in this assembly is to encapsulate the handshake between the Facebook application and a canvas application (both FBML and IFrame) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Facebook.Web.Mvc.dll&lt;/strong&gt;: Provide a support building canvas applications using ASP.NET MVC. Separated from Facebook.Web.dll to avoid all developers from needing to install the MVC bits. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Facebook.Winforms.dll&lt;/strong&gt;: This assembly provides support for writing Facebook applications using Winform technology. This provides a Component that wraps the API to make it easier to use from Winforms. This also contains some user controls to help display Facebook data easily.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;If you want some helpful starting points, the Facebook Developer Wiki has some help too.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://wiki.developers.facebook.com/index.php/Anatomy_of_a_Facebook_App"&gt;Anatomy_of_a_Facebook_App&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://wiki.developers.facebook.com/index.php/Platform_Core_Components"&gt;Platform_Core_Components&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://wiki.developers.facebook.com/index.php/How-to_Guides"&gt;How-to_Guides&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://wiki.developers.facebook.com/index.php/Creating_a_Platform_Application"&gt;Creating_a_Platform_Application&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9920962" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Facebook/default.aspx">Facebook</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/ASP.Net+MVC/default.aspx">ASP.Net MVC</category></item><item><title>Fluids and edge detection in Silverlight</title><link>http://blogs.msdn.com/coding4fun/archive/2009/09/30/9900361.aspx</link><pubDate>Wed, 30 Sep 2009 20:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9900361</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9900361.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9900361</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9900361</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/FluidsandedgedetectioninSilverlight_9684/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/FluidsandedgedetectioninSilverlight_9684/image_thumb.png" width="240" height="125" /&gt;&lt;/a&gt; Rick Barraza has 3 amazing examples in Silverlight.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.cynergysystems.com/blogs/page/rickbarraza?entry=advanced_render_techniques_with_writeablebitmaps"&gt;Writable Bitmaps&lt;/a&gt;       &lt;br /&gt;This example shows how to use pixel shader displacement with line drawing to create some amazing effects. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cynergysystems.com/blogs/page/rickbarraza?entry=fluid_dynamics_in_silverlight"&gt;Fluid Dynamics&lt;/a&gt;       &lt;br /&gt;Rick talks about how to do dynamic visuals, why they matter. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cynergysystems.com/blogs/page/rickbarraza?entry=rendering_vector_fields_in_silverlight"&gt;Vector Fields&lt;/a&gt;       &lt;br /&gt;This example shows how to gray scale an image, do edge detection then use that information to draw vectors on the image for additional effects. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If you want additional information with Rick, &lt;a href="http://blogs.msdn.com/coding4fun/archive/2009/09/21/9896902.aspx"&gt;Brian Peek did an interview with him regarding Silverlight advance rendering techniques as well.&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9900361" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Coding4Fun Show – Episode 3 with Rick Barraza</title><link>http://blogs.msdn.com/coding4fun/archive/2009/09/21/9896902.aspx</link><pubDate>Mon, 21 Sep 2009 12:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9896902</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9896902.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9896902</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9896902</wfw:comment><description>&lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" height="375" width="500"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_06_22.xap"&gt; &lt;param name="initParams" value="m=http://ecn.channel9.msdn.com/o9/ch9/2/6/3/2/9/4/C4FShowE3Barraza_2MB_ch9.wmv,autostart=false,autohide=true,showembed=true, thumbnail=http://ecn.channel9.msdn.com/o9/ch9/2/6/3/2/9/4/C4FShowE3Barraza_320_ch9.png, postid=492362"&gt; &lt;param name="background" value="#00FFFFFF"&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" mce_href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" mce_src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none;"&gt; &lt;/a&gt; &lt;/object&gt;  &lt;p&gt;&lt;a href="http://www.brianpeek.com/" mce_href="http://www.brianpeek.com/"&gt;Brian Peek&lt;/a&gt; interviews &lt;a href="http://www.cynergysystems.com/blogs/page/rickbarraza" mce_href="http://www.cynergysystems.com/blogs/page/rickbarraza" target="_blank"&gt;Rick Barraza&lt;/a&gt; for the Coding4Fun Show on &lt;a href="http://channel9.msdn.com/shows/Coding4FunTV/" mce_href="http://channel9.msdn.com/shows/Coding4FunTV/" target="_blank"&gt;Coding4Fun TV&lt;/a&gt;!&amp;nbsp; In this episode, Brian chats with Rick about advanced rendering techniques in Silverlight 3.&amp;nbsp; Great stuff!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9896902" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Coding4Fun+TV/default.aspx">Coding4Fun TV</category></item><item><title>Silverlight 3 File Transfer Application</title><link>http://blogs.msdn.com/coding4fun/archive/2009/09/03/9876572.aspx</link><pubDate>Fri, 04 Sep 2009 09:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9876572</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9876572.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9876572</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9876572</wfw:comment><description>&lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Author: &lt;/strong&gt;&lt;a href="http://www.gmontrone.com/" target="_blank"&gt;Giovanni Montrone&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Difficulty: &lt;/b&gt;Intermediate &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 5-10 hours &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Cost: &lt;/b&gt;Free &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software: &lt;/b&gt;&lt;a href="http://msdn.com/express/"&gt;Visual Web Developer Express SP1&lt;/a&gt; (or Visual Studio 2008 SP1), &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=2050e580-f1d5-4040-bb09-e6185591b6b5&amp;amp;displaylang=en" target="_blank"&gt;Silverlight 3 SDK&lt;/a&gt;,&amp;#160; &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=9442B0F2-7465-417A-88F3-5E7B5409E9DD&amp;amp;displaylang=en" target="_blank"&gt;Silverlight 3 Tools for VS&lt;/a&gt;, &lt;a href="http://www.codeplex.com/Silverlight" target="_blank"&gt;Silverlight Toolkit&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Hardware: &lt;/b&gt;None &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Use it Now: &lt;a href="http://www.gmontrone.com/SLFileSend/SLFileSenderTestPage.aspx" target="_blank"&gt;Run the Application&lt;/a&gt;&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Source Download: &lt;/strong&gt;&lt;a href="http://slfilesend.codeplex.com/" target="_blank"&gt;&lt;strong&gt;CodePlex&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;Every once in a while I will run into a situation where I need to send a file to someone, but struggle to find an easy way of doing it.&amp;#160; Instant messenger programs usually work until you run into a situation where someone just cannot send or receive a file from chat client whether it’s due to firewalls, differing client versions, or multi-IM incompatibilities.&amp;#160; Other times, I will try to send the file via email just to find out that the person’s email server blocks specific extensions.&amp;#160; This quick application will allow two users to quickly and easily connect to a Silverlight 3 client and send each other files.&amp;#160; &lt;/p&gt;  &lt;h3&gt;Overview&lt;/h3&gt;  &lt;p&gt;In this application, a user will connect to the Silverlight application and choose to either host or join a session.&amp;#160; If a user decides to host a session, he or she will be given a random eight character session key, and he or she will be in a waiting status until another user connects.&amp;#160; When a user wishes to connect to the host, he or she can supply the host’s session key which will establish a connection between the two users.&amp;#160; Once connected, the users will be able to send files to one another, and will also be able to chat with each other via simple text messages.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.coding4fun.net/images/ca982cecb767_2D39/SendFile.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SendFile" border="0" alt="SendFile" src="http://www.coding4fun.net/images/ca982cecb767_2D39/SendFile_thumb.jpg" width="544" height="237" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;Polling Duplex&lt;/h3&gt;  &lt;p&gt;Sending a file from one client application to another requires a common central point in order to properly route the message from one user to another.&amp;#160; Since it is easy to host a Silverlight application in an ASP.NET page, we will use an ASP.NET back end to manage all communications between all connected users.&amp;#160; In order to do this, we must be able to host a service that is able to accept incoming messages from the Silverlight client, and perform a push back out to the intended recipient.&amp;#160; This is accomplished by using the WCF Polling Duplex (&lt;strong&gt;System.ServiceModel.PollingDuplex.dll&lt;/strong&gt;) channel.&amp;#160; Silverlight 3 allows us to directly add a service reference to a service of this type, and handles all of the complexities for us.&amp;#160; I began by using the&lt;strong&gt; DuplexService.cs&lt;/strong&gt; file from a sample application from MIX09 when Silverlight 3 beta was released.&amp;#160; The code starts us out with a couple of base abstract classes, and interfaces that we must inherit from in order to create our own service.&amp;#160; &lt;/p&gt;  &lt;h3&gt;FileSendService&lt;/h3&gt;  &lt;p&gt;The two main things we need to do in order to create our own service is to define custom message types that will be used for communication, and override the base &lt;strong&gt;DuplexService&lt;/strong&gt; class to properly handle these messages.&amp;#160; We will create our custom message types by using &lt;strong&gt;DuplexMessage&lt;/strong&gt; (defined in &lt;strong&gt;DuplexService.cs&lt;/strong&gt;) as our base class.&amp;#160; Our message classes must be defined with the &lt;strong&gt;[DataContract]&lt;/strong&gt; attribute, and the the member variables must be public and contain the &lt;strong&gt;[DataMember]&lt;/strong&gt; attribute.&amp;#160; This will allow our Silverlight client project to share these definitions using a service reference.&amp;#160; Additionally, the Duplex message class must have the &lt;strong&gt;[KnownType]&lt;/strong&gt; attribute for each descendant message created.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: 98.08%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; height: 90px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;p&gt;[KnownType(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(HostSessionMessage))]&lt;br /&gt;[KnownType(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(JoinSessionMessage))]&lt;br /&gt;[KnownType(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(FileBeginUploadMessage))]&lt;br /&gt;[KnownType(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(FileTransferBytesMessage))]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DuplexMessage { }&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[DataContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; HostSessionMessage : DuplexMessage&lt;br /&gt;{&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Username;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[DataContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; JoinSessionMessage : DuplexMessage&lt;br /&gt;{&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Username;&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SessionKey;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[DataContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FileBeginUploadMessage : DuplexMessage&lt;br /&gt;{&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FileName;&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;long&lt;/span&gt; TotalBytes;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[DataContract]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FileTransferBytesMessage : DuplexMessage&lt;br /&gt;{&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;long&lt;/span&gt; StartByte;&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;long&lt;/span&gt; PacketSize;&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] Bytes;&lt;br /&gt;    [DataMember]&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; EndFile;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&amp;lt;DataContract(&lt;span style="color: #0000ff"&gt;Namespace&lt;/span&gt; := &lt;span style="color: #006080"&gt;&amp;quot;http://samples.microsoft.com/silverlight2/duplex&amp;quot;&lt;/span&gt;), &lt;br /&gt;KnownType(&lt;span style="color: #0000ff"&gt;GetType&lt;/span&gt;(HostSessionMessage)), KnownType(&lt;span style="color: #0000ff"&gt;GetType&lt;/span&gt;(JoinSessionMessage)), KnownType(&lt;span style="color: #0000ff"&gt;GetType&lt;/span&gt;(FileBeginUploadMessage)), KnownType(&lt;span style="color: #0000ff"&gt;GetType&lt;/span&gt;(FileTransferBytesMessage))&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; DuplexMessage&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&amp;lt;DataContract()&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; HostSessionMessage&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexMessage&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; Username &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;DataContract()&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; JoinSessionMessage&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexMessage&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; Username &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; SessionKey &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;DataContract()&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; FileTransferBytesMessage&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexMessage&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; StartByte &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Long&lt;/span&gt;&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; PacketSize &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Long&lt;/span&gt;&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; Bytes() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Byte&lt;/span&gt;&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; EndFile &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Boolean&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;DataContract()&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; FileBeginUploadMessage&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexMessage&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; FileName &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &amp;lt;DataMember()&amp;gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; TotalBytes &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Long&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The next step is to create our &lt;strong&gt;FileSendService&lt;/strong&gt; class which descends from the &lt;strong&gt;DuplexService&lt;/strong&gt; class as described above.&amp;#160; We override the &lt;strong&gt;OnMessage&lt;/strong&gt; method so that we can do custom processing based on the type of message sent as shown below.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 99.68%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; height: 210px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: 97.9%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; height: 286px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FileSendService : DuplexService&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; List&amp;lt;SessionConnectionInfo&amp;gt; sessionConnections = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;SessionConnectionInfo&amp;gt;();&lt;br /&gt;&lt;br /&gt;    {...}&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnMessage(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sessionId, DuplexMessage data)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (data &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; HostSessionMessage)&lt;br /&gt;            CreateHostSession(data &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; HostSessionMessage);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (data &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; JoinSessionMessage)&lt;br /&gt;            JoinSession(data &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; JoinSessionMessage);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (data &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; FileBeginUploadMessage)&lt;br /&gt;            StartSendFile(data &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; FileBeginUploadMessage);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;            SendMessage(data);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&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: 99.22%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; height: 159px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (data &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; JoinSessionMessage)&lt;br /&gt;            JoinSession(data &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; JoinSessionMessage);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (data &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; FileBeginUploadMessage)&lt;br /&gt;            StartSendFile(data &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; FileBeginUploadMessage);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;            SendMessage(data);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; FileSenderServiceFactory&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexServiceFactory(Of FileSendService)&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;AspNetCompatibilityRequirements(RequirementsMode := AspNetCompatibilityRequirementsMode.Allowed)&amp;gt; _&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; FileSendService&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; DuplexService&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; sessionConnections &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; List(Of SessionConnectionInfo)()&lt;br /&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Overrides&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; OnMessage(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; sessionId &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; data &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; DuplexMessage)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; data &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; HostSessionMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            CreateHostSession(&lt;span style="color: #0000ff"&gt;TryCast&lt;/span&gt;(data, HostSessionMessage))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; data &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; JoinSessionMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            JoinSession(&lt;span style="color: #0000ff"&gt;TryCast&lt;/span&gt;(data, JoinSessionMessage))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; data &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; FileBeginUploadMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            StartSendFile(&lt;span style="color: #0000ff"&gt;TryCast&lt;/span&gt;(data, FileBeginUploadMessage))&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Else&lt;/span&gt;&lt;br /&gt;            SendMessage(data)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Since we are going to have to keep track of each host and user connected to that host, we will create a class,&amp;#160; &lt;strong&gt;SessionConnectionInfo&lt;/strong&gt;, to manage the relevant data.&amp;#160; When a user decides to host a session, our &lt;strong&gt;OnMessage&lt;/strong&gt; method will receive a &lt;strong&gt;HostSessionMessage&lt;/strong&gt; which will scan our&lt;strong&gt; List&amp;lt;SessionConnectionInfo&amp;gt;&lt;/strong&gt; to see if there is another host with the same username.&amp;#160; If not found, a new &lt;strong&gt;SessionConnectionInfo&lt;/strong&gt; object is created along with a randomly generated session key.&amp;#160; When a user attempts to join a session, the &lt;strong&gt;OnMessage&lt;/strong&gt; method will receive a &lt;strong&gt;JoinSessionMessage&lt;/strong&gt; containing a session key and the name of the user joining the session.&amp;#160; The service will search for a &lt;strong&gt;SessionConnectionInfo&lt;/strong&gt; object containing that session key, and if found, establishes a connection between the two users.&amp;#160; &lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; height: 308px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SessionConnectionInfo&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; HostUserName { get; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ConnectedUsername { get; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SessionKey { get; set; }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ConnectedUserInternalSession { get; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set; }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; HostInternalSession { get; set; }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; UserConnected&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; ConnectedUserInternalSession != &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Empty; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; SessionConnectionInfo()&lt;br /&gt;    {&lt;br /&gt;        ConnectedUserInternalSession = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;        ConnectedUsername = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ....&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&amp;#160;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; SessionConnectionInfo&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; privateHostUserName &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; HostUserName() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; privateHostUserName&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; value &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;)&lt;br /&gt;            privateHostUserName = value&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; privateConnectedUsername &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; ConnectedUsername() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; privateConnectedUsername&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; value &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;)&lt;br /&gt;            privateConnectedUsername = value&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; privateSessionKey &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; SessionKey() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; privateSessionKey&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; value &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;)&lt;br /&gt;            privateSessionKey = value&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; privateConnectedUserInternalSession &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; ConnectedUserInternalSession() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; privateConnectedUserInternalSession&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; value &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;)&lt;br /&gt;            privateConnectedUserInternalSession = value&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; privateHostInternalSession &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; HostInternalSession() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; privateHostInternalSession&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; value &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;)&lt;br /&gt;            privateHostInternalSession = value&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Set&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;ReadOnly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt; UserConnected() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Boolean&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt; ConnectedUserInternalSession &amp;lt;&amp;gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;.Empty&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Get&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Property&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt;()&lt;br /&gt;        ConnectedUserInternalSession = &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;.Empty&lt;br /&gt;        ConnectedUsername = &lt;span style="color: #0000ff"&gt;String&lt;/span&gt;.Empty&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h2&gt;Client Side&lt;/h2&gt;

&lt;p&gt;Now that we have the basic server side complete, we can add a service reference, and create the necessary service reference.&amp;#160; Our service is hosted within ASP.NET via a simple xml file (see &lt;strong&gt;FileSendService.svc&lt;/strong&gt;), which allows our Silverlight project to see it.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/ca982cecb767_2D39/AddServiceRef.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Add Service Ref" border="0" alt="Add Service Ref" src="http://www.coding4fun.net/images/ca982cecb767_2D39/AddServiceRef_thumb.jpg" width="540" height="475" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Be sure to mark the checkbox “Always generate message contracts”.&amp;#160; Note that your web portion of the project must be compiled before you will be able to “Discover” the service.&lt;/p&gt;

&lt;p&gt;We will now have access to the &lt;strong&gt;DuplexServiceClient&lt;/strong&gt; class which will allow us to send and receive messages to the server.&amp;#160; We instantiate the service as shown below.&amp;#160; In order to do this, you must manually add a reference to &lt;strong&gt;System.ServiceModel.PollingDuplex&lt;/strong&gt; assembly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; DuplexServiceClient fileDuplexService;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; CustomBinding binding = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; CustomBinding(&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PollingDuplexBindingElement(),&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BinaryMessageEncodingBindingElement(),&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpTransportBindingElement());&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage()&lt;br /&gt;{&lt;br /&gt;    InitializeComponent();&lt;br /&gt;    fileDuplexService = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DuplexServiceClient(binding, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; EndpointAddress(&lt;span style="color: #006080"&gt;&amp;quot;http://localhost:9797/FileSendService.svc&amp;quot;&lt;/span&gt;));&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; fileDuplexService &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; DuplexServiceClient&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; binding &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; CustomBinding(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; PollingDuplexBindingElement(), &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; BinaryMessageEncodingBindingElement(), &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; HttpTransportBindingElement())&lt;br /&gt;&lt;/p&gt;&lt;p&gt;    &lt;br /&gt;&lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt;()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; InitializeComponent()&lt;br /&gt;    fileDuplexService = &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; DuplexServiceClient(binding, &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; EndpointAddress(&lt;span style="color: #006080"&gt;&amp;quot;http://localhost:9797/FileSendService.svc&amp;quot;&lt;/span&gt;))&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Note that at the time of this writing, adding the service reference does not properly create the &lt;strong&gt;ServiceReferences.ClientConfig&lt;/strong&gt; file.&amp;#160; This is the reason the above is done using code.&lt;/p&gt;

&lt;p&gt;Now that our service is setup, we need to set it up to handle sending and receiving of messages.&amp;#160;&amp;#160; In order to send a message, we must first create a &lt;strong&gt;DupexMessage&lt;/strong&gt; (or a descendant), and use the &lt;strong&gt;SendToServiceAsync&lt;/strong&gt; method.&amp;#160; This requires a &lt;strong&gt;SendToService&lt;/strong&gt; object which will contain the message, and an optional &lt;strong&gt;userState&lt;/strong&gt; object which we can use to tag the request.&amp;#160; In this case, we will pass an enum value which represents the status of our upload.&amp;#160; In the example below, when the user clicks on the send file button, after choosing the file, we open our file, and send a &lt;strong&gt;FileBeginUpload&lt;/strong&gt; message which contains the file name and its size.&amp;#160; Note that the server is set to deny any file that is greater than 20 million bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; btnSendFile_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt; {&lt;br /&gt;     OpenFileDialog openFileDialog = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; OpenFileDialog();&lt;br /&gt;     openFileDialog.Multiselect = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;     openFileDialog.ShowDialog();&lt;br /&gt;     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (openFileDialog.File != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;     {&lt;br /&gt;         fileToSend = openFileDialog.File.OpenRead();&lt;br /&gt;&lt;br /&gt;         FileBeginUploadMessage fsm = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileBeginUploadMessage();&lt;br /&gt;         fsm.FileName = openFileDialog.File.Name;&lt;br /&gt;         fsm.TotalBytes = openFileDialog.File.Length;&lt;br /&gt;         fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SendToService(fsm), FileSendState.FileStart);&lt;br /&gt;         ....&lt;br /&gt;     }&lt;br /&gt; }&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; btnSendFile_Click(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Object&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; RoutedEventArgs)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; openFileDialog &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; OpenFileDialog()&lt;br /&gt;    openFileDialog.Multiselect = &lt;span style="color: #0000ff"&gt;False&lt;/span&gt;&lt;br /&gt;    openFileDialog.ShowDialog()&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; openFileDialog.File IsNot &lt;span style="color: #0000ff"&gt;Nothing&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;        fileToSend = openFileDialog.File.OpenRead()&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; fsm &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; FileBeginUploadMessage()&lt;br /&gt;        fsm.FileName = openFileDialog.File.Name&lt;br /&gt;        fsm.TotalBytes = openFileDialog.File.Length&lt;br /&gt;        totalBytesSent = 0&lt;br /&gt;        fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SendToService(fsm), FileSendState.FileStart)&lt;br /&gt;        ....&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The service object fires two main events that need to be handled: &lt;strong&gt;SendToServiceCompleted&lt;/strong&gt; and &lt;strong&gt;SendToClientReceived&lt;/strong&gt;. &lt;strong&gt;SendToServiceCompleted&lt;/strong&gt; is the event which is fired after the server acknowledges that it has received and processed a message that the client has sent.&amp;#160; Once the server receives and processes the &lt;strong&gt;FileBeginUploadMessage&lt;/strong&gt;, the event handler below will receive the results.&amp;#160; In this case, if there isn’t an error, and the value of &lt;strong&gt;userState&lt;/strong&gt; is &lt;strong&gt;FileSendState.FileStart&lt;/strong&gt;,&amp;#160; the method will send a &lt;strong&gt;FileTransferBytesMessage&lt;/strong&gt; which will send the bytes from the file.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; FileDuplexServiceSendToServiceCompleted(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, System.ComponentModel.AsyncCompletedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.Error == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        {...}&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; ((FileSendState)e.UserState == FileSendState.FileEnd)&lt;br /&gt;        {&lt;br /&gt;            fileToSend.Close();&lt;br /&gt;            fileProgress.Value = 100;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; ((FileSendState)e.UserState == FileSendState.FileStart || (FileSendState)e.UserState == FileSendState.FileContinue)&lt;br /&gt;        {&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ...&lt;br /&gt;            FileTransferBytesMessage fileMessage = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileTransferBytesMessage();&lt;br /&gt;            fileMessage.StartByte = totalBytesSent;&lt;br /&gt;            fileMessage.EndFile = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;            fileMessage.PacketSize = CHUNK;&lt;br /&gt;&lt;br /&gt;            ...&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] bytes = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[numBytesToRead];&lt;br /&gt;            fileToSend.Read(bytes, 0, numBytesToRead);&lt;br /&gt;            totalBytesSent += numBytesToRead;&lt;br /&gt;            fileMessage.Bytes = bytes;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (fileMessage.EndFile)&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SendToService(fileMessage), FileSendState.FileEnd);&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SendToService(fileMessage), FileSendState.FileContinue);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/p&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; FileDuplexServiceSendToServiceCompleted(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Object&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; System.ComponentModel.AsyncCompletedEventArgs)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; e.&lt;span style="color: #0000ff"&gt;Error&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Nothing&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; e.UserState &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Nothing&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.UserState, FileSendState) = FileSendState.FileEnd &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            fileToSend.Close()&lt;br /&gt;            fileProgress.Value = 100&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.UserState, FileSendState) = FileSendState.FileStart &lt;span style="color: #0000ff"&gt;OrElse&lt;/span&gt; &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.UserState, FileSendState) = FileSendState.FileContinue &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            ...&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; fileMessage &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; FileTransferBytesMessage()&lt;br /&gt;            fileMessage.StartByte = totalBytesSent&lt;br /&gt;            fileMessage.EndFile = &lt;span style="color: #0000ff"&gt;False&lt;/span&gt;&lt;br /&gt;            fileMessage.PacketSize = CHUNK&lt;br /&gt;            ...&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; bytes(numBytesToRead - 1) &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Byte&lt;/span&gt;&lt;br /&gt;            fileToSend.Read(bytes, 0, numBytesToRead)&lt;br /&gt;            totalBytesSent += numBytesToRead&lt;br /&gt;            fileMessage.Bytes = bytes&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; fileMessage.EndFile &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SendToService(fileMessage), FileSendState.FileEnd)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Else&lt;/span&gt;&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SendToService(fileMessage), FileSendState.FileContinue)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The &lt;strong&gt;SendToClientReceived&lt;/strong&gt; event is fired when a message has been sent from the service.&amp;#160; In this case, the client will check to see which message was received and process it accordingly.&amp;#160; The method below shows that when a &lt;strong&gt;FileBeginUpload&lt;/strong&gt; message is received, the user is prompted to accept or deny the file.&amp;#160; If the user does not accept the file, a &lt;strong&gt;FileDenyMessage&lt;/strong&gt; will be sent which will notify the sender to stop sending bytes.&amp;#160; If accepted, file bytes are added to a buffer.&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; FileDuplexServiceSendToClientReceived(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, SendToClientReceivedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.Error == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.request.msg &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; ClientConnectedMessage)&lt;br /&gt;        {&lt;br /&gt;            ClientConnectedMessage msg = (ClientConnectedMessage)e.request.msg;&lt;br /&gt;            AddMsgToListbox(msg.Username + &lt;span style="color: #006080"&gt;&amp;quot; has just connected.&amp;quot;&lt;/span&gt;);&lt;br /&gt;            connectedTo = msg.Username;&lt;br /&gt;            UIState = UIState.Chat;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.request.msg &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; HostSessionServerMessage)&lt;br /&gt;        {&lt;br /&gt;            HostSessionServerMessage hssm = e.request.msg &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; HostSessionServerMessage;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (hssm.Failed) {...}&lt;br /&gt;            SessionCreated(hssm);&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.request.msg &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; JoinSessionServerMessage)&lt;br /&gt;        {&lt;br /&gt;            JoinSessionServerMessage jssm = e.request.msg &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; JoinSessionServerMessage;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (jssm.Failed) {....}&lt;br /&gt;            SessionJoined(jssm);&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.request.msg &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; FileBeginUploadMessage )&lt;br /&gt;        {&lt;br /&gt;            FileBeginUploadMessage fsm = (FileBeginUploadMessage)e.request.msg;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; sizeInKB = (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;)fsm.TotalBytes / 1024;&lt;br /&gt;            totalRevd = 0;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (MessageBox.Show(connectedTo + &lt;span style="color: #006080"&gt;&amp;quot; would like to send you the file: &amp;quot;&lt;/span&gt; + fsm.FileName + &lt;span style="color: #006080"&gt;&amp;quot;, Size: &amp;quot;&lt;/span&gt; + sizeInKB + &lt;span style="color: #006080"&gt;&amp;quot;.  Would you like to receive this file?&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;File Upload&amp;quot;&lt;/span&gt;, MessageBoxButton.OKCancel) == MessageBoxResult.OK)&lt;br /&gt;            {&lt;br /&gt;                bytesReceived = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;&amp;gt;((&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;)fsm.TotalBytes);&lt;br /&gt;                fileNameReceiving = fsm.FileName;&lt;br /&gt;                ....&lt;br /&gt;            }&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SendToService(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileDenyMessage()));&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.request.msg &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; FileTransferBytesMessage)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (bytesReceived == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;            FileTransferBytesMessage fm = (FileTransferBytesMessage)e.request.msg;&lt;br /&gt;            bytesReceived.AddRange(fm.Bytes);&lt;br /&gt;            ....&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; {....}&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; FileDuplexServiceSendToClientReceived(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Object&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; SendToClientReceivedEventArgs)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; e.&lt;span style="color: #0000ff"&gt;Error&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Nothing&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; e.request.msg &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; ClientConnectedMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; msg &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; ClientConnectedMessage = &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.request.msg, ClientConnectedMessage)&lt;br /&gt;            AddMsgToListbox(msg.Username &amp;amp; &lt;span style="color: #006080"&gt;&amp;quot; has just connected.&amp;quot;&lt;/span&gt;)&lt;br /&gt;            connectedTo = msg.Username&lt;br /&gt;            UIState = UIState.Chat&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; e.request.msg &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; HostSessionServerMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; hssm &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; HostSessionServerMessage = &lt;span style="color: #0000ff"&gt;TryCast&lt;/span&gt;(e.request.msg, HostSessionServerMessage)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; hssm.Failed &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt; ...&lt;br /&gt;&lt;br /&gt;            SessionCreated(hssm)&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; e.request.msg &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; JoinSessionServerMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; jssm &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; JoinSessionServerMessage = &lt;span style="color: #0000ff"&gt;TryCast&lt;/span&gt;(e.request.msg, JoinSessionServerMessage)&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; jssm.Failed &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt; ...&lt;br /&gt;&lt;br /&gt;            SessionJoined(jssm)&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; e.request.msg &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; FileBeginUploadMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; fsm &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; FileBeginUploadMessage = &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.request.msg, FileBeginUploadMessage)&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; sizeInKB &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Integer&lt;/span&gt; = &lt;span style="color: #0000ff"&gt;CInt&lt;/span&gt;(Fix(fsm.TotalBytes)) / 1024&lt;br /&gt;            totalRevd = 0&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; MessageBox.Show(connectedTo &amp;amp; &lt;span style="color: #006080"&gt;&amp;quot; would like to send you the file: &amp;quot;&lt;/span&gt; &amp;amp; fsm.FileName &amp;amp; &lt;span style="color: #006080"&gt;&amp;quot;, Size: &amp;quot;&lt;/span&gt; &amp;amp; sizeInKB &amp;amp; &lt;span style="color: #006080"&gt;&amp;quot; KB.  Would you like to receive this file?&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;File Upload&amp;quot;&lt;/span&gt;, MessageBoxButton.OKCancel) = MessageBoxResult.OK &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;                bytesReceived = &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; List(Of &lt;span style="color: #0000ff"&gt;Byte&lt;/span&gt;)(&lt;span style="color: #0000ff"&gt;CInt&lt;/span&gt;(Fix(fsm.TotalBytes)))&lt;br /&gt;                fileNameReceiving = fsm.FileName&lt;br /&gt;                ...&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Else&lt;/span&gt;&lt;br /&gt;                fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SendToService(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; FileDenyMessage()))&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;span style="color: #0000ff"&gt;TypeOf&lt;/span&gt; e.request.msg &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; FileTransferBytesMessage &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; bytesReceived &lt;span style="color: #0000ff"&gt;Is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Nothing&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;Return&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; fm &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; FileTransferBytesMessage = &lt;span style="color: #0000ff"&gt;CType&lt;/span&gt;(e.request.msg, FileTransferBytesMessage)&lt;br /&gt;            bytesReceived.AddRange(fm.Bytes)&lt;br /&gt;            ...&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;ElseIf&lt;/span&gt; &lt;br /&gt;          ...&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;When the file is complete, the user will be presented with two buttons.&amp;#160; One button will allow the user to save, and the other will allow the user to discard/ignore the file.&amp;#160; If the user chooses to save, a &lt;strong&gt;SaveFileDialog&lt;/strong&gt; is created, and we use the filename to assign the default extension and filter so that when the user saves the file, no extension needs to be typed in.&amp;#160; Once the dialog appears and the user enters the file name, the bytes will be written to disk.&amp;#160; Finally we send a message back to the server which will notify the sender that user has done something with the file.&amp;#160; During file send operations, the Send File button is disabled, and becomes re-enabled once a file has been received, cancelled, or denied.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; void btnSaveFile_Click(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e)&lt;br /&gt;{&lt;br /&gt;    SaveFileDialog sfd = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SaveFileDialog();&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; extension = GetExtension(fileNameReceiving);&lt;br /&gt;    sfd.DefaultExt = extension;&lt;br /&gt;    sfd.Filter = extension +  &lt;span style="color: #006080"&gt;&amp;quot; Files|&amp;quot;&lt;/span&gt; + extension;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (sfd.ShowDialog() == &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        using (Stream fsx = sfd.OpenFile())&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] fBytes = bytesReceived.ToArray();&lt;br /&gt;            fsx.Write(fBytes, 0, fBytes.Length);&lt;br /&gt;            fsx.Close();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SendToService(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileReceivedMessage()));&lt;br /&gt;        UIState = UIState.Chat;&lt;br /&gt;        btnSendFile.IsEnabled = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&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: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;Private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; btnSaveFile_Click(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Object&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; e &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; RoutedEventArgs)&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; sfd &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SaveFileDialog()&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; extension &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;String&lt;/span&gt; = GetExtension(fileNameReceiving)&lt;br /&gt;    sfd.DefaultExt = extension&lt;br /&gt;    sfd.Filter = extension &amp;amp; &lt;span style="color: #006080"&gt;&amp;quot; Files|&amp;quot;&lt;/span&gt; &amp;amp; extension&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;If&lt;/span&gt; sfd.ShowDialog() = &lt;span style="color: #0000ff"&gt;True&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Then&lt;/span&gt;&lt;br /&gt;        Using fsx &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; Stream = sfd.OpenFile()&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; fBytes() &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Byte&lt;/span&gt; = bytesReceived.ToArray()&lt;br /&gt;            fsx.Write(fBytes, 0, fBytes.Length)&lt;br /&gt;            fsx.Close()&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Using&lt;br /&gt;&lt;br /&gt;        fileDuplexService.SendToServiceAsync(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; SendToService(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; FileReceivedMessage()))&lt;br /&gt;        UIState = UIState.Chat&lt;br /&gt;        btnSendFile.IsEnabled = &lt;span style="color: #0000ff"&gt;True&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;If&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;h3&gt;&amp;#160;&lt;/h3&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Overall, the application does what I set out for it to.&amp;#160; It allows a user to send a file to another user and even does basic chat.&amp;#160; There is definitely a lot of room for improvement, and some general additions that can make this more robust.&amp;#160; Right now we keep a list of connections, but never do basic pinging to see if the clients are still there.&amp;#160; The only way to know if a client has disconnected is when a message fails, or when the user decides to hit the disconnect button. Better maintenance of this list is needed.&amp;#160; The file bytes are basically being stored in memory, which is why i set a file size limit, but I’m sure with things like Silverlight local storage, we can increase the limit.&amp;#160; &lt;/p&gt;

&lt;h3&gt;Thanks&lt;/h3&gt;

&lt;p&gt;I would like to thank &lt;a href="http://www.brianpeek.com/"&gt;Brian Peek&lt;/a&gt; for taking the time to review my article and test the code.&lt;/p&gt;

&lt;h3&gt;Additional Notes&lt;/h3&gt;

&lt;p&gt;The ASP.NET project requires a file called System.ServiceModel.PollingDuplex.dll to be added as a reference.&amp;#160; Somewhere between Silverlight 3 Beta and Silverlight 3 RTW, the file was no longer available via the basic add .NET reference link.&amp;#160; In order to add the file, I had to browse for it in &lt;strong&gt;%Program Files%\Microsoft SDKs\Silverlight\v3.0\Libraries\Server&lt;/strong&gt;.&amp;#160; For 64 bit users, it will be the &lt;strong&gt;Program Files (x86)&lt;/strong&gt; folder.&amp;#160; &lt;/p&gt;

&lt;p&gt;For ease of use, I used a static port (9797) for this project. When deploying to a server, you must rename all references of http://localhost:9797 in the Silverlight project.&amp;#160; This will remain true until the config file is supported. &lt;/p&gt;

&lt;p&gt;The user interface uses the Silverlight Toolkit TwilightBlue theme.&amp;#160; For more information about this,&amp;#160; see &lt;a title="http://www.codeplex.com/Silverlight" href="http://www.codeplex.com/Silverlight"&gt;http://www.codeplex.com/Silverlight&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9876572" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/utility/default.aspx">utility</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web+miscellaneous/default.aspx">web miscellaneous</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Creating Games with Silverlight: A Simple Shooter</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/21/9658490.aspx</link><pubDate>Tue, 21 Jul 2009 20:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9658490</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9658490.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9658490</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9658490</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb.png" width="300" height="240" /&gt;&lt;/a&gt;This article will take you through some of the steps to creating the basics for writing games with Silverlight. This is a simple shooter style game that contains some of the building blocks for games that require abilities such as vectors, collision detection, a game loop, movement, and keyboard input. Please refer to the full source code for this project for any areas of the code that are not covered in detail in this article.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Source Code:&lt;/b&gt; &lt;a href="http://simpleshooter.codeplex.com/"&gt;http://simpleshooter.codeplex.com/&lt;/a&gt;       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Difficulty:&lt;/b&gt; Beginner/Intermediate &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 3 hours &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Cost:&lt;/b&gt; $0 &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Needed:&lt;/b&gt;       &lt;ul&gt;       &lt;li&gt;&lt;a href="http://www.microsoft.com/express/download/"&gt;Visual Basic or Visual C# Express&lt;/a&gt; &lt;/li&gt;        &lt;li&gt;&lt;a href="http://www.microsoft.com/expression/"&gt;Expression Blend&lt;/a&gt; (optional) &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Inspiration&lt;/h3&gt;  &lt;p&gt;There are a number of examples and tutorials out there to help get started creating games. You will find the basics of this game to be very similar to a Visual C# Coding4Fun article &lt;a href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/916430.aspx"&gt;here&lt;/a&gt;. This article will show you how to create a shooter style game in Silverlight. My first Silverlight game experience was playing with a Asteroids style game by Bill Reiss. My usage of vectors and how to do a game loop came from great examples like his and others. There are a number of sites and blogs where you can get good info on game development with Silverlight. Here are just a few.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.bluerosegames.com/brg/"&gt;BlueRoseGames.com&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.farseergames.com/"&gt;Farseer Games&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cameronalbert.com/"&gt;Cameron Albert&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://silverlight.net/blogs/msnow/default.aspx"&gt;Mike Snow&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Basic Layout&lt;/h3&gt;  &lt;p&gt;To begin, Open Visual Studio and create a new Silverlight Application in either C# or VB.net. First, we will start with the layout of the screen. First, I added a canvas to the grid inside the Page.xaml that is created for you. I set it's background to black, and named it 'gameRoot'. &lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SimpleShooter.Page&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;300&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;gameRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Black&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Next we will start adding controls to our project for things like game entities, and information displays. In this example. To complete layout of the game, add user controls to the progect called: Info, LivesRemaining, Score, and WaveInfo. In those controls, To begin I added basic TextBlocks to display information on the state of the game. Please note there are better ways to arrange things in a user control, beyond what I show here. I am keeping to a simple canvas and direct positioning to introduce the concept of Canvas.Top and Canvas.Left. I have now placed these controls on Page.xaml and assigned an x:Name to each. At this point, the controls have only a TextBlock in them.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SimpleShooter.Page&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:SimpleShooter&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;clr-namespace:SimpleShooter&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;gameRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Black&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:RemainingLives&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlLives&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;380&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:Score&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlScore&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:WaveInfo&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlWaveInfo&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;440&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:Info&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlInfo&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;To finish our layout lets add a star field. To do this, we will write function to generate random numbers, and another that will randomly distribute ellipses on our base canvas. We will need to inherit from System.Security.Cryptography for this:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Page : UserControl
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Page()
    {
        InitializeComponent();

        GenerateStarField(350);
    }

    &lt;span class="kwrd"&gt;void&lt;/span&gt; GenerateStarField(&lt;span class="kwrd"&gt;int&lt;/span&gt; numberOfStars)
    {

        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numberOfStars; i++)
        {

            Ellipse star = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ellipse();
            &lt;span class="kwrd"&gt;double&lt;/span&gt; size = GetRandInt(10, 800) * .01;
            star.Width = size;
            star.Height = size;
            star.Opacity = GetRandInt(1, 5) * .1;
            star.Fill = &lt;span class="kwrd"&gt;new&lt;/span&gt; SolidColorBrush(Colors.White);
            &lt;span class="kwrd"&gt;int&lt;/span&gt; x = GetRandInt(0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)Math.Round(gameRoot.Height, 0));
            &lt;span class="kwrd"&gt;int&lt;/span&gt; y = GetRandInt(0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)Math.Round(gameRoot.Width, 0));
            star.SetValue(Canvas.TopProperty, (&lt;span class="kwrd"&gt;double&lt;/span&gt;)x);
            star.SetValue(Canvas.LeftProperty, (&lt;span class="kwrd"&gt;double&lt;/span&gt;)y);
            gameRoot.Children.Add(star);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; GetRandInt(&lt;span class="kwrd"&gt;int&lt;/span&gt; min, &lt;span class="kwrd"&gt;int&lt;/span&gt; max)
    {
        Byte[] rndBytes = &lt;span class="kwrd"&gt;new&lt;/span&gt; Byte[10];
        RNGCryptoServiceProvider rndC = &lt;span class="kwrd"&gt;new&lt;/span&gt; RNGCryptoServiceProvider();
        rndC.GetBytes(rndBytes);
        &lt;span class="kwrd"&gt;int&lt;/span&gt; seed = BitConverter.ToInt32(rndBytes, 0);
        Random rand = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random(seed);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; rand.Next(min, max);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Partial &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Page
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; UserControl

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
        InitializeComponent()
        GenerateStarField(350)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; GenerateStarField(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; numberOfStars &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)

        &lt;span class="kwrd"&gt;For&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = 0 &lt;span class="kwrd"&gt;To&lt;/span&gt; numberOfStars - 1

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; star &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Ellipse()
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; size &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = GetRandInt(10, 800) * 0.01
            star.Width = size
            star.Height = size
            star.Opacity = GetRandInt(1, 5) * 0.1
            star.Fill = &lt;span class="kwrd"&gt;New&lt;/span&gt; SolidColorBrush(Colors.White)
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = GetRandInt(0, &lt;span class="kwrd"&gt;CInt&lt;/span&gt;(Math.Round(gameRoot.Height, 0)))
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = GetRandInt(0, &lt;span class="kwrd"&gt;CInt&lt;/span&gt;(Math.Round(gameRoot.Width, 0)))
            star.SetValue(Canvas.TopProperty, &lt;span class="kwrd"&gt;CDbl&lt;/span&gt;(x))
            star.SetValue(Canvas.LeftProperty, &lt;span class="kwrd"&gt;CDbl&lt;/span&gt;(y))
            gameRoot.Children.Add(star)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; GetRandInt(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; min &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; max &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rndBytes &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Byte&lt;/span&gt;]() = &lt;span class="kwrd"&gt;New&lt;/span&gt; [&lt;span class="kwrd"&gt;Byte&lt;/span&gt;](9) {}
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rndC &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; RNGCryptoServiceProvider()
        rndC.GetBytes(rndBytes)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; seed &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = BitConverter.ToInt32(rndBytes, 0)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rand &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Random(seed)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; rand.[&lt;span class="kwrd"&gt;Next&lt;/span&gt;](min, max)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;We now have a function called GenerateStarField. Note that it is adding each ellipse to the 'Children' of our base gameRoot canvas, and how the Top and Left properties determine the positions of those ellipses. Now we have a basic layout of our game and a background.&lt;/p&gt;

&lt;h4&gt;&lt;/h4&gt;

&lt;h4&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_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="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_1.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/h4&gt;

&lt;h4&gt;Sprites and Vectors&lt;/h4&gt;

&lt;p&gt;I am not going to go in to great detail on &lt;a href="http://en.wikipedia.org/wiki/Sprite_(computer_graphics)"&gt;sprites&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Euclidean_vector"&gt;vectors&lt;/a&gt;. There are a number of great resources (see other articles linked to in this post), as well as &lt;a href="http://blogs.msdn.com/coding4fun/archive/2007/02/20/1727608.aspx"&gt;another good Coding4Fun article&lt;/a&gt; on this topic. We are however, going to add a class to represent a sprite and a vector to our code:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Width { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Height { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector Velocity { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Canvas SpriteCanvas { get; set; }
    &lt;span class="kwrd"&gt;private&lt;/span&gt; Point _position;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Point Position
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _position;
        }
        set
        {
            _position = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            SpriteCanvas.SetValue(Canvas.TopProperty, _position.Y - (Height / 2));
            SpriteCanvas.SetValue(Canvas.LeftProperty, _position.X - (Width / 2));
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Sprite(Double width, Double height, Point position)
    {
        Width = width;
        Height = height;

        SpriteCanvas = RenderSpriteCanvas();

        SpriteCanvas.Width = width;
        SpriteCanvas.Height = height;
        &lt;span class="rem"&gt;//NOTE: because the setter for Position uses both Height and Width, it is important this comes after they are set.&lt;/span&gt;
        Position = position;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Canvas RenderSpriteCanvas();

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Canvas LoadSpriteCanvas(&lt;span class="kwrd"&gt;string&lt;/span&gt; xamlPath)
    {
        System.IO.Stream s = &lt;span class="kwrd"&gt;this&lt;/span&gt;.GetType().Assembly.GetManifestResourceStream(xamlPath);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (Canvas)XamlReader.Load(&lt;span class="kwrd"&gt;new&lt;/span&gt; System.IO.StreamReader(s).ReadToEnd());
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(TimeSpan elapsedTime)
    {
        Position = (Position + Velocity * elapsedTime.TotalSeconds);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;MustInherit&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Width() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Width
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _Width = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Height() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Height
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _Height = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Velocity &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Velocity() &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Velocity
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector)
            _Velocity = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _SpriteCanvas &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; SpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _SpriteCanvas
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas)
            _SpriteCanvas = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _position &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Position() &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _position
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
            _position = value
            SpriteCanvas.SetValue(Canvas.TopProperty, _position.Y - (Height / 2))
            SpriteCanvas.SetValue(Canvas.LeftProperty, _position.X - (Width / 2))
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialWidth &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Double&lt;/span&gt;], &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialHeight &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Double&lt;/span&gt;], &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        Width = initialWidth
        Height = initialHeight

        SpriteCanvas = RenderSpriteCanvas()

        SpriteCanvas.Width = Width
        SpriteCanvas.Height = Height
        &lt;span class="rem"&gt;'NOTE: because the setter for Position uses both Height and Width, it is important this comes after they are set. &lt;/span&gt;
        Position = initialPosition
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;MustOverride&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; LoadSpriteCanvas(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; xamlPath &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; s &lt;span class="kwrd"&gt;As&lt;/span&gt; System.IO.Stream = &lt;span class="kwrd"&gt;Me&lt;/span&gt;.[&lt;span class="kwrd"&gt;GetType&lt;/span&gt;]().&lt;span class="kwrd"&gt;Assembly&lt;/span&gt;.GetManifestResourceStream(xamlPath)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(XamlReader.Load(&lt;span class="kwrd"&gt;New&lt;/span&gt; System.IO.StreamReader(s).ReadToEnd()), Canvas)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overridable&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
        Position = (Position + Velocity * elapsedTime.TotalSeconds)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;This Sprite class will give us the basis for entities in the game such as a ship, aliens, and projectiles. For all these items, we need to know the location of the item, the site, and what it looks like. We will take advantage of Point to track our position, and a property of type Canvas to display the XAML for each of these items. The constructor sets these initial parameters, and calls a RenderSpriteCanvas method to be implemented by each class that inherits from it. This method allows the inheriting class to set the contents of the canvas and therefore as control of the way the sprite looks.&lt;/p&gt;

&lt;p&gt;Next we have our vector class that will help us control movement of our sprite. Again, I will not go into detail on vectors as this code is borrowed and adapted from many other readily available sources:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; Vector
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; X;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Y;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector(&lt;span class="kwrd"&gt;double&lt;/span&gt; x, &lt;span class="kwrd"&gt;double&lt;/span&gt; y)
    {
        X = x;
        Y = y;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Length
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; Math.Sqrt(LengthSquared);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; LengthSquared
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; X * X + Y * Y;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Normalize()
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; length = Length;
        X /= length;
        Y /= length;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Vector &lt;span class="kwrd"&gt;operator&lt;/span&gt; -(Vector vector)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(-vector.X, -vector.Y);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Vector &lt;span class="kwrd"&gt;operator&lt;/span&gt; *(Vector vector, &lt;span class="kwrd"&gt;double&lt;/span&gt; scalar)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(scalar * vector.X, scalar * vector.Y);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Point &lt;span class="kwrd"&gt;operator&lt;/span&gt; +(Point point, Vector vector)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(point.X + vector.X, point.Y + vector.Y);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector CreateVectorFromAngle(&lt;span class="kwrd"&gt;double&lt;/span&gt; angleInDegrees, &lt;span class="kwrd"&gt;double&lt;/span&gt; length)
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; x = Math.Sin(DegreesToRadians(180 - angleInDegrees)) * length;
        &lt;span class="kwrd"&gt;double&lt;/span&gt; y = Math.Cos(DegreesToRadians(180 - angleInDegrees)) * length;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(x, y);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; DegreesToRadians(&lt;span class="kwrd"&gt;double&lt;/span&gt; degrees)
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; radians = ((degrees / 360) * 2 * Math.PI);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; radians;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Structure&lt;/span&gt; Vector
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; X &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; x__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; y__2 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
        X = x__1
        Y = y__2
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Length() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; Math.Sqrt(LengthSquared)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; LengthSquared() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; X * X + Y * Y
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Normalize()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; length__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Length
        X /= length__1
        Y /= length__1
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; -(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(-vector.X, -vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; *(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; scalar &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(scalar * vector.X, scalar * vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; +(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; point &lt;span class="kwrd"&gt;As&lt;/span&gt; Point, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector) &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(point.X + vector.X, point.Y + vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;


    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; CreateVectorFromAngle(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; angleInDegrees &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; length &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Math.Sin(DegreesToRadians(180 - angleInDegrees)) * length
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Math.Cos(DegreesToRadians(180 - angleInDegrees)) * length
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(x, y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; DegreesToRadians(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; degrees &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; radians &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = ((degrees / 360) * 2 * Math.PI)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; radians
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Structure&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;
    &lt;br /&gt;&lt;/b&gt;Now we can implement Sprite with a new Ship class. Add a class called Ship, and a file called Ship.xaml to your project. Be sure to set the properties of Ship.xaml to 'Embedded Resource'. We now need to inherit from our Sprite class:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Ship : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Ship(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Ship.xaml&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Ship
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;When Ship is instantiated, it calls the constructor of it's base, Sprite. It also implements the RenderSpriteCanvas method and specifies the XAML (a simple white square) to load into the sprite's canvas. Now we are ready to add a sprite to our main page. In this simple game, we will have only one ship (the others will be aliens), so lets add a property to our page, and a function that will instantiate our ship:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/client/2007&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Rectangle&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Fill&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;White&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeGame()
{
    PlayerShip = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(100, 300));
    gameRoot.Children.Add(PlayerShip.SpriteCanvas);
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InitializeGame()
    PlayerShip = &lt;span class="kwrd"&gt;New&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(100, 300))
    gameRoot.Children.Add(PlayerShip.SpriteCanvas)
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;We can now call this method from our page constructor and when we run the project, we get a white square (our ship) at the bottom left of our Page. Taking a closer look, we set our page size to 500 x 400, and the InitializeGame method places our Ship at a point 100 pixels from the left of the gameRoot canvas, and 300pixels from the top.&lt;/p&gt;

&lt;h4&gt;Keyboard Input and the Game Loop&lt;/h4&gt;

&lt;p&gt;Now we are ready to make some things move. To start, we need choose keys on the keyboard to allow movement, and act on those events. We then need to capture those key presses and act on them if they are relevant to our game loop. Once again, may examples of key handlers and game loops are readily available, so I won't go into detail. The keyboard handler captures all key up and down events. We can therefore ask our instance of the handler if a key is pressed at any given time. The game loop is just that, a constant loop. It consists of a storyboard that has the start called on it and it immediately ends. The class raises an event, and starts the storyboard again. Subscribers to Update event are provided a value that reports the number of milliseconds since the last update. That value can be applied to vectors to apply smooth movement to sprites. To take advantage of these classes, we need to add an instance of both a KeyHandler and GameLoop to our Page. To do this, we update the InitializeGame and Page constructor, and add a handler for the GameLoop:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Page()
{
    InitializeComponent();
    keyHandler = &lt;span class="kwrd"&gt;new&lt;/span&gt; KeyHandler(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
    GenerateStarField(350);
    InitializeGame();
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeGame()
{
    gameLoop = &lt;span class="kwrd"&gt;new&lt;/span&gt; GameLoop(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
    gameLoop.Update += &lt;span class="kwrd"&gt;new&lt;/span&gt; GameLoop.UpdateHandler(gameLoop_Update);

    PlayerShip = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(100, 360));
    gameRoot.Children.Add(PlayerShip.SpriteCanvas);

    gameLoop.Start();
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; gameLoop_Update(TimeSpan elapsed)
{
    &lt;span class="rem"&gt;//clear the current vector so the sprite is not moving unless a keys is pressed&lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Left))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Right))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125);
    }
    PlayerShip.Update(elapsed);
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
    InitializeComponent()
    keyHandler = &lt;span class="kwrd"&gt;New&lt;/span&gt; KeyHandler(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)
    GenerateStarField(350)
    InitializeGame()
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InitializeGame()
    gameLoop = &lt;span class="kwrd"&gt;New&lt;/span&gt; GameLoop(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)
    &lt;span class="kwrd"&gt;AddHandler&lt;/span&gt; gameLoop.Update, &lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; gameLoop_Update

    PlayerShip = &lt;span class="kwrd"&gt;New&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(100, 360))
    gameRoot.Children.Add(PlayerShip.SpriteCanvas)

    gameLoop.Start()
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; gameLoop_Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsed &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
    &lt;span class="rem"&gt;'clear the current vector so the sprite is not moving unless a keys is pressed &lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Left) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Right) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

    PlayerShip.Update(elapsed)
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_2.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now we have a functioning game loop. Launch the app, click on the Silverlight control to give it focus, and you can now use the arrow keys to move our ship right and left. One problem however is that you are able to take the ship completely off the screen. To prevent this, we can add MinX and MaxX properties to our ship class and override the Update method it inherits from Sprite. Be sure to also add those min and max values after the Ship is instantiated in InitializeGame of our Page: &lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Ship : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Ship(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Ship.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(System.TimeSpan elapsedTime)
    {
        &lt;span class="rem"&gt;//verify that this is a position we can move to&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;gt; MaxX)
        {
            Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(MaxX, Position.Y);
            Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
        }
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;lt; MinX)
        {
            Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(MinX, Position.Y);
            Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
        }
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Ship
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; System.TimeSpan)
        &lt;span class="rem"&gt;'verify that this is a position we can move to &lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;gt; MaxX &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Position = &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(MaxX, Position.Y)
            Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;lt; MinX &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Position = &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(MinX, Position.Y)
            Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;h4&gt;Prepare to Fire!&lt;/h4&gt;

&lt;p&gt;We now have all the basic plumbing to add additional sprites such as aliens or projectiles. To begin, we will add additional classes inheriting from Sprite: Alien, Missle, and Bomb, along with Alien.xaml, Missle.xaml, and Bomb.xaml (these new .xaml files need to be set as Embedded Resources). These xaml files are just like our Ship.xaml with different sizes and colors. Make the Aliens and their bombs red, and shrink the bomb and missile height and width to 5. The xaml is very similar, but the classes themselves are going to have a few differing capabilities. Bomb and Missile are very similar. They only need to load their corresponding xaml. Here is our Bomb class for example:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Bomb : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Bomb(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Bomb.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(System.TimeSpan elapsedTime)
    {
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Bomb
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Bomb.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; System.TimeSpan)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;We now need to add an Alien class similar to our Ship class. These will both need a bit more capability. For one, we are going to have them both fire at each other. In the case of the Alien, it will be firing down, and it's monition of choice will be a bomb:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Alien : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRateMilliseconds = 2000;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireVelocity = 250;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; wayPointMin;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; wayPointMax;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; speed = 100;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; spawnWait;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime spawnComplete;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Alien(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CheckDirection()
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;gt; wayPointMax)
        {
            Velocity = Vector.CreateVectorFromAngle(270, speed);
        }
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;lt; wayPointMin)
        {
            Velocity = Vector.CreateVectorFromAngle(90, speed);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Alien.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(TimeSpan elapsedTime)
    {
        CheckDirection();
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Bomb Fire()
    {
        Bomb bomb = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bomb(5, 5, Position);
        bomb.Velocity = Vector.CreateVectorFromAngle(180, fireVelocity);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; bomb;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Alien
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; fireRateMilliseconds &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 2000
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; fireVelocity &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 250
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; wayPointMin &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; wayPointMax &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; speed &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 100
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; spawnWait &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; spawnComplete &lt;span class="kwrd"&gt;As&lt;/span&gt; DateTime
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; CheckDirection()
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;gt; wayPointMax &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Velocity = Vector.CreateVectorFromAngle(270, speed)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;lt; wayPointMin &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Velocity = Vector.CreateVectorFromAngle(90, speed)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
        CheckDirection()
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Fire() &lt;span class="kwrd"&gt;As&lt;/span&gt; Bomb
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; bomb &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Bomb(5, 5, Position)
        bomb.Velocity = Vector.CreateVectorFromAngle(180, fireVelocity)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; bomb
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;Now that we are going to add munitions to the game, we need to be able to know when these munitions collide with other sprites. To do this, we are going to add a collision detection method to our sprite class. As a form of simple collision detection, we will give each sprite a CollisionRadius from its center point. To keep it simple we will make the radius one half the width of our sprites. A vector created from these two points can then detect if the sum of those two radius is greater than the length of our vector. If it is, they have collided:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Collides(Sprite s1, Sprite s2)
{
    Vector v = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector((s1.Position.X) - (s2.Position.X), (s1.Position.Y) - (s2.Position.Y));
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (s1.CollisionRadius + s2.CollisionRadius &amp;gt; v.Length)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Collides(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; s1 &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; s2 &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; v &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector((s1.Position.X) - (s2.Position.X), (s1.Position.Y) - (s2.Position.Y))
    &lt;span class="kwrd"&gt;If&lt;/span&gt; s1.CollisionRadius + s2.CollisionRadius &amp;gt; v.Length &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;True&lt;/span&gt;
    &lt;span class="kwrd"&gt;Else&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Function&lt;/pre&gt;

&lt;p&gt;We are now ready to extend our game loop of our Page to do more than track the movement of our Ship. We begin by taking action on more than just the arrow keys. We will wire up the space bar for shooting. Lets add an enumeration to track the state of our game. We will also add a supporting class that will handle the firing of munitions and adding those to our game. We will keep this simple for now, but it is an obvious place to being extending this code to take advantage of things like partial classes that could contain functionality like our 'Fire' method:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; GameState
{
    Ready = 0,
    Running = 1,
    Paused = 2,
    BetweenWaves = 3,
    GameOver = 4
}

&lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Space))
{
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (status)
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Ready:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Running:
            EntityFired(PlayerShip);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Paused:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.BetweenWaves:
            status = GameState.Running;
            ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
            StartWave();
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.GameOver:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;default&lt;/span&gt;:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; EntityFired(Sprite shooter)
{
    Debug.WriteLine(shooter);
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (shooter.ToString())
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship&amp;quot;&lt;/span&gt;:
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (missles.Count == 0)
            {
                Missle missle = ((Ship)shooter).Fire();
                missles.Add(missle);
                gameRoot.Children.Add(missle.SpriteCanvas);

            }
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien&amp;quot;&lt;/span&gt;:
            Bomb bomb = ((Alien)shooter).Fire();
            bombs.Add(bomb);
            gameRoot.Children.Add(bomb.SpriteCanvas);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;default&lt;/span&gt;:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Enum&lt;/span&gt; GameState
    Ready = 0
    Running = 1
    Paused = 2
    BetweenWaves = 3
    GameOver = 4
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Enum&lt;/span&gt;

&lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Space) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
    &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; status
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Ready
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Running
            EntityFired(PlayerShip)
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Paused
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.BetweenWaves
            status = GameState.Running
            ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
            StartWave()
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.GameOver
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; EntityFired(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; shooter &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite)
    Debug.WriteLine(shooter)
    &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; shooter.ToString()
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship&amp;quot;&lt;/span&gt;
            &lt;span class="kwrd"&gt;If&lt;/span&gt; missles.Count = 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; missle &lt;span class="kwrd"&gt;As&lt;/span&gt; Missle = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(shooter, Ship).Fire()
                missles.Add(missle)

                gameRoot.Children.Add(missle.SpriteCanvas)
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien&amp;quot;&lt;/span&gt;
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; bomb &lt;span class="kwrd"&gt;As&lt;/span&gt; Bomb = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(shooter, Alien).Fire()
            bombs.Add(bomb)
            gameRoot.Children.Add(bomb.SpriteCanvas)
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;To make it easier to track the state of our game, let's add public properties to our controls and let them be the keepers of those game states. For instance, give the ReamainingLives control a Lives property, and have the setter of that property also update the TextBlock on the control to show the user how many lives are left. Perform a similar task for the other three controls:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RemainingLives : UserControl
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _lives;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Lives
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _lives; }
        set
        {
            _lives = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; livesString = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;
            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; _lives - 1; i++)
            {
                livesString = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0}{1}&amp;quot;&lt;/span&gt;, livesString, &lt;span class="str"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;);
            }
            txtRemainingLives.Text = livesString;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; RemainingLives()
    {
        InitializeComponent();
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Partial &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; RemainingLives
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; UserControl
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _lives &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Lives() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _lives
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _lives = value
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; livesString &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; = &lt;span class="kwrd"&gt;String&lt;/span&gt;.Empty
            &lt;span class="kwrd"&gt;For&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = 0 &lt;span class="kwrd"&gt;To&lt;/span&gt; _lives - 2
                livesString = &lt;span class="kwrd"&gt;String&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0}{1}&amp;quot;&lt;/span&gt;, livesString, &lt;span class="str"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;)
            &lt;span class="kwrd"&gt;Next&lt;/span&gt;
            txtRemainingLives.Text = livesString
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
        InitializeComponent()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;Now we have a number of other things to add to our page class. We are going to have lots of entities to deal with in our game loop. We will have a ship, x number of Aliens, x number of Bombs, and at some points we will have our Missiles, though we restricted that to one at a time in our EntityFired method. We need to, in each game loop check if any missile hits any alien or leaves the game map, if any bomb hits our ship or leaves the game map, and if it is time for a Alien to fire at the ship. Our Page already has a property for our Ship, but the Aliens, Bombs, Missiles need to be collections. When we begin to loop through our Bombs for instance, we will want to remove a bomb if it strikes a Ship or leaves the game map. Since we are going to be iterating these collections, we cannot subtract from those collections as we are looping through them. There are a number of approaches for this, but to keep it simple, we will add a corresponding collection for each of our Bomb, Alien, and Missile collections to track the ones that need to be removed and removed from our game canvas. With this approach, our game loop can add to the &amp;quot;remove these&amp;quot; collection, and then take action on them when we leave the main loop for the parent collection:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;List&amp;lt;Alien&amp;gt; aliens;
List&amp;lt;Alien&amp;gt; aliensRemove;
List&amp;lt;Alien&amp;gt; alienShooters;
List&amp;lt;Bomb&amp;gt; bombs;
List&amp;lt;Bomb&amp;gt; bombsRemove;
List&amp;lt;Missle&amp;gt; missles;
List&amp;lt;Missle&amp;gt; misslesRemove;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; aliens &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; aliensRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; alienShooters &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; bombs &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Bomb)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; bombsRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Bomb)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; missles &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Missle)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; misslesRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Missle)&lt;/pre&gt;

&lt;p&gt;We also want to add a class to our page that can track the waves of aliens we are going to send one at a time. In addition, we can add a collection to hold these waves. Each wave will track the number of aliens that spawn at once, the total number of aliens faced in the wave, how many aliens get to drop bombs, and how frequently they can fire.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WaveData
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; WaveData(&lt;span class="kwrd"&gt;int&lt;/span&gt; count, &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRate, &lt;span class="kwrd"&gt;int&lt;/span&gt; atOnce, &lt;span class="kwrd"&gt;int&lt;/span&gt; fireatonce)
    {
        EnemyCount = count;
        fireRateMilliseconds = fireRate;
        enemiesAtOnce = atOnce;
        fireAtOnce = fireatonce;
        waveEmpty = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; EnemyCount { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRateMilliseconds { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; enemiesAtOnce { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; fireAtOnce { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; waveEmpty { get; set; }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; WaveData
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; count &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; fireRate &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; atOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; fireatonce__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
        EnemyCount = count
        fireRateMilliseconds = fireRate
        enemiesAtOnce = atOnce
        fireAtOnce = fireatonce__1
        waveEmpty = &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _EnemyCount &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; EnemyCount() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _EnemyCount
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _EnemyCount = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _fireRateMilliseconds &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; fireRateMilliseconds() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _fireRateMilliseconds
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _fireRateMilliseconds = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _enemiesAtOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; enemiesAtOnce() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _enemiesAtOnce
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _enemiesAtOnce = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _fireAtOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; fireAtOnce() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _fireAtOnce
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _fireAtOnce = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _waveEmpty &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; waveEmpty() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _waveEmpty
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)
            _waveEmpty = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;We will need to setup an initialization method for our game, to setup all our collections for sprites, and add progressively difficult waves to our game. The meat of it all now lies in our game loop. For each collection of sprites we call methods that will iterate over its contents and make decisions based on collisions, locations of the sprites. After a loop on each main collection, we can use the 'remove us' collection to clear items from the main collection, remove their canvas from our game canvas, and delete them from the main collection. Finally, we check to see if enough time has elapsed to let the aliens drop another bomb:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; gameLoop_Update(TimeSpan elapsed)
{
    &lt;span class="rem"&gt;//clear the current Vector so the sprite is not moving unless a keys is pressed&lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Left))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Right))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Space))
    {
        &lt;span class="kwrd"&gt;switch&lt;/span&gt; (status)
        {
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Ready:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Running:
                EntityFired(PlayerShip);
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Paused:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.BetweenWaves:
                status = GameState.Running;
                ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
                StartWave();
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.GameOver:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;default&lt;/span&gt;:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        }
    }
    PlayerShip.Update(elapsed);

    BombLoop(elapsed);
    MissleLoop(elapsed);
    AlienLoop(elapsed);

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; aliensRemove)
    {
        aliens.Remove(alien);
        gameRoot.Children.Remove(alien.SpriteCanvas);
        AlienShot(alien);
    }
    aliensRemove.Clear();

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Missle missle &lt;span class="kwrd"&gt;in&lt;/span&gt; misslesRemove)
    {
        missles.Remove(missle);
        gameRoot.Children.Remove(missle.SpriteCanvas);
    }
    misslesRemove.Clear();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (nextShot &amp;lt;= DateTime.Now)
    {
        nextShot = DateTime.Now.AddMilliseconds(enemyShootMilliseonds).AddMilliseconds(elapsed.Milliseconds * -1);

        shotsThisPass = shotsAtOnce;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (shotsThisPass &amp;gt; aliens.Count)
        {
            shotsThisPass = aliens.Count;
        }

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (aliens.Count &amp;gt; 0)
        {
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; aliens)
            {
                alienShooters.Add(alien);
            }
        }

        &lt;span class="kwrd"&gt;while&lt;/span&gt; (alienShooters.Count &amp;gt; shotsThisPass)
        {
            alienShooters.RemoveAt(GetRandInt(0, alienShooters.Count - 1));
        }

        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; alienShooters)
        {
            EntityFired(alien);
        }

        alienShooters.Clear();
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; gameLoop_Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsed &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
    &lt;span class="rem"&gt;'clear the current Vector so the sprite is not moving unless a keys is pressed &lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Left) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Right) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Space) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; status
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Ready
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Running
                EntityFired(PlayerShip)
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Paused
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.BetweenWaves
                status = GameState.Running
                ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
                StartWave()
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.GameOver
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    PlayerShip.Update(elapsed)

    BombLoop(elapsed)
    MissleLoop(elapsed)
    AlienLoop(elapsed)

    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; aliensRemove
        aliens.Remove(alien)
        gameRoot.Children.Remove(alien.SpriteCanvas)
        AlienShot(alien)
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    aliensRemove.Clear()

    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; missle &lt;span class="kwrd"&gt;As&lt;/span&gt; Missle &lt;span class="kwrd"&gt;In&lt;/span&gt; misslesRemove
        missles.Remove(missle)
        gameRoot.Children.Remove(missle.SpriteCanvas)
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    misslesRemove.Clear()

    &lt;span class="kwrd"&gt;If&lt;/span&gt; nextShot &amp;lt;= DateTime.Now &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        nextShot = DateTime.Now.AddMilliseconds(enemyShootMilliseonds).AddMilliseconds(elapsed.Milliseconds * -1)

        shotsThisPass = shotsAtOnce
        &lt;span class="kwrd"&gt;If&lt;/span&gt; shotsThisPass &amp;gt; aliens.Count &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            shotsThisPass = aliens.Count
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;If&lt;/span&gt; aliens.Count &amp;gt; 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; aliens
                alienShooters.Add(alien)
            &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;While&lt;/span&gt; alienShooters.Count &amp;gt; shotsThisPass
            alienShooters.RemoveAt(GetRandInt(0, alienShooters.Count - 1))
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;While&lt;/span&gt;

        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; alienShooters
            EntityFired(alien)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;

        alienShooters.Clear()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now we can almost call it a game:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_3.png" width="500" height="402" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Taking it one step further, I jumped into Expression Blend to create a bit more interesting XAML that our sprites are loading into their SpriteCanvas. This XAML can be found in the full download of the source code. The results make our game just a tad bit more interesting:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_4.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Silverlight provides a number of capabilities to enable robust game development. It should be noted that in this game, we are only scratching the surface. The core of this example used a game loop to position sprites on a canvas. Additional capabilities within Silverlight such as &lt;a href="http://blogs.msdn.com/silverlight_sdk/archive/2008/03/21/silverlight-animations-a-walkthrough.aspx"&gt;animations&lt;/a&gt;, &lt;a href="http://silverlight.net/blogs/msnow/archive/2008/10/02/silverlight-tip-of-the-day-55-how-to-apply-styles-in-silverlight-part-i.aspx"&gt;styling&lt;/a&gt;, &lt;a href="http://timheuer.com/blog/archive/2008/10/06/silverlight-control-template-gallery-styles.aspx"&gt;templating&lt;/a&gt;, and &lt;a href="http://timheuer.com/blog/archive/2008/06/04/silverlight-introduces-visual-state-manager-vsm.aspx"&gt;visual states&lt;/a&gt; provide even more ways to push the limits of browser based games. If you are at all like me, dabbling with game development is a great way to get your feet wet with Silverlight and learn some basics. Happy coding!&lt;/p&gt;

&lt;p&gt;If you want to try this out, the download link for the source code is at the top of the article!&lt;/p&gt;

&lt;h3&gt;About The Author&lt;/h3&gt;

&lt;p&gt;Roger Guess is the Director of IT for The Wedge Group where he works with technologies such as Silverlight and WPF. He writes games in his spare time, and blogs at &lt;a href="http://silverlightaddict.com/blogs/"&gt;SilverlightAddict.com&lt;/a&gt;. He can be reached via email at &lt;a href="mailto:email@rogerguess.net"&gt;email@rogerguess.net&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9658490" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Audio/default.aspx">Audio</category></item><item><title>Robozzle</title><link>http://blogs.msdn.com/coding4fun/archive/2009/06/02/9688468.aspx</link><pubDate>Wed, 03 Jun 2009 04:02:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9688468</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9688468.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9688468</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9688468</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Robozzle_119B7/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 5px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Robozzle_119B7/image_thumb.png" width="240" height="172" /&gt;&lt;/a&gt; &lt;a href="http://www.robozzle.com/"&gt;Robozzle&lt;/a&gt; is a Silverlight game that helps teach players the art of programming logic through a series of fun and challenging community-created puzzles.&amp;#160; What's more, the game is &amp;quot;community sourced&amp;quot; meaning not only can you contribute new levels - but actually help expand the game and shape it's future. (&lt;a href="http://robozzle.codeplex.com/"&gt;http://robozzle.codeplex.com/&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;[via &lt;a href="http://www.imagitronics.org/"&gt;Tim Farley&lt;/a&gt;]&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9688468" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Blu-ray/DVD Comparison Utility</title><link>http://blogs.msdn.com/coding4fun/archive/2009/03/28/9515699.aspx</link><pubDate>Sat, 28 Mar 2009 10:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9515699</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9515699.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9515699</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9515699</wfw:comment><description>&lt;table border="0" cellpadding="1" cellspacing="0" width="100%"&gt;&lt;tbody&gt;     &lt;tr class="entry_overview"&gt;       &lt;td&gt;&lt;span class="entry_description"&gt;In this article, Giovanni Montrone will provide an overview on how to create an image comparison application for DVD and Blu-ray screen captures.&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td colspan="2"&gt;         &lt;div class="entry_author"&gt;&lt;a href="http://www.gmontrone.com/" target="_blank"&gt;Giovanni Montrone&lt;/a&gt;&lt;/div&gt;          &lt;div class="entry_company"&gt;&lt;a href="http://www.aspsoft.com/" mce_href="http://www.aspsoft.com/"&gt;ASPSOFT, Inc.&lt;/a&gt;&lt;/div&gt;          &lt;br&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Difficulty: &lt;/b&gt;&lt;span class="entry_details_input"&gt;Beginner&lt;/span&gt;&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Time Required:&lt;/b&gt; 2&lt;span class="entry_details_input"&gt;-3 hours&lt;/span&gt;&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Cost: &lt;/b&gt;&lt;span class="entry_details_input"&gt;Free&lt;/span&gt;&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Software: &lt;/b&gt;&lt;span class="entry_details_input"&gt;&lt;a href="http://msdn.com/express/" mce_href="http://msdn.com/express/"&gt;Visual Web Developer Express SP1&lt;/a&gt; (or Visual Studio 2008 SP1), &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=c22d6a7b-546f-4407-8ef6-d60c8ee221ed&amp;amp;displaylang=en"&gt;Silverlight 2 Tools&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Hardware: &lt;/b&gt;None&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Application: &lt;/b&gt;&lt;a href="http://gmontrone.com/bdcompare/BlurayDVDCompare.html" target="_blank"&gt;Run the application&lt;/a&gt;&lt;/div&gt;          &lt;div class="entry_details"&gt;&lt;b&gt;Source Download: &lt;/b&gt;&lt;a href="http://channel9.msdn.com/playground/Sandbox/462814-Blu-rayDVD-Comparison-Utility/" mce_href="http://channel9.msdn.com/playground/Sandbox/462814-Blu-rayDVD-Comparison-Utility/" target="_blank"&gt;Download the source&lt;/a&gt;&lt;/div&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;As a fan of High Definition movies and television, I often find it difficult to explain the benefits of upgrading from DVD to blu-ray to family and friends.&amp;nbsp; Many people believe that DVDs look good enough and/or that you need a really big television to notice the difference, and, even then, feel the improvement is not good enough to justify the upgrade.&amp;nbsp; The best way to convince someone is to show him/her the same movie playing side by side on blu-ray and dvd using the same sized screens.&amp;nbsp; Since it's difficult to do, the next best thing is to look at still images from both the DVD and the blu-ray and compare them.&amp;nbsp; The images should be from the exact same scene.&amp;nbsp; Fortunately there are places on the web such as &lt;a href="http://www.avsforum.com/"&gt;AVSForum&lt;/a&gt; where members take the time to do this.&amp;nbsp; They usually do this to help people determine what kind of an upgrade they can expect when viewing the blu-ray compared to the DVD.&amp;nbsp; Sometimes the differences are easy to see, and other times they are not as clear.&amp;nbsp; I built this comparison tool as a way to see what differences exist, and provided a few ways to make those differences easier to see.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;This utility will allow the user to compare two different images using 3 different modes: dual view, swap view, and Rectangle View. Dual view will allow a side by side comparison where you see part of the first image, and the remaining part of the second image. Moving the mouse left or right will show more of one image, and less of the other. The swap mode will allow the user to see one image in its entirety, and then, when placing the mouse over the image, the application will swap the first image with the second one. The last mode will allow the user to specify a region that he or she wishes to compare. They can draw a rectangle using the mouse, and the application will show the first image with a chunk cut out, replacing the rectangle area with the equivalent area on the second image. The image below shows an example of the rectangle view. &lt;/p&gt;  &lt;p&gt;&amp;nbsp; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BLURAYDVDCOMPARISONUTILITY_2CE4/bddvdcompare1_2.jpg"&gt;&lt;img src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BLURAYDVDCOMPARISONUTILITY_2CE4/bddvdcompare1_thumb.jpg" style="border-width: 0px;" alt="bddvdcompare1" border="0" height="484" width="543"&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;Notes&lt;/h3&gt;  &lt;p&gt;The application is set to handle images that are 1920x1080 in resolution (the standard resolution of blu-ray movies).&amp;nbsp; DVDs have a resolution of 720x480, but are typically up-converted to 1920x1080 in order to do a direct comparison.&amp;nbsp; This upconversion is similar to what happens when you play a DVD on a 1080p high definition television.&amp;nbsp; While it wouldn't be too difficult to handle other image sizes, to keep things simple, the application assumes the user will be providing images at the proper resolution.&amp;nbsp; Using different images with different resolutions will result in incorrect behavior. &lt;/p&gt;  &lt;p&gt;At the time of this writing, Silverlight 3 beta has just recently been released, but has not been tested with this application. &lt;/p&gt;  &lt;h3&gt;Setup&lt;/h3&gt;  &lt;p&gt;Before beginning, you must have Visual Studio 2008 SP1 or Visual Web Developer Express SP1.&amp;nbsp; Be sure to install &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=c22d6a7b-546f-4407-8ef6-d60c8ee221ed&amp;amp;displaylang=en"&gt;Silverlight 2 Tools&lt;/a&gt; in order to create and run the project.&amp;nbsp; Once you have that, start a new Silverlight Application and call it BlurayDVDCompare, or whatever you desire.&amp;nbsp; As shown in figure 1, choose the option to host Silverlight with an ASP.NET web application. This will create a web project and a separate Silverlight project.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;  &lt;p&gt;&amp;nbsp; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BLURAYDVDCOMPARISONUTILITY_2CE4/figure1_2.png"&gt;&lt;img src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BLURAYDVDCOMPARISONUTILITY_2CE4/figure1_thumb.png" style="border-width: 0px;" alt="figure1" border="0" height="311" width="349"&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;nbsp; &lt;/p&gt;  &lt;h3&gt;Creating the UI &lt;/h3&gt;  &lt;p&gt;Since we will be displaying HD images (1920x1080), our next step is to ensure that the browser adds scrollbars so that the entire Silverlight control can be seen.&amp;nbsp; To do this, we need to open and edit the hosting web page (BlurayDVDCompareTestPage.aspx) and set the Width and Height properties of the Silverlight control to 1920 x 1200 as shown below.&amp;nbsp; The 1200 is extra room for other controls that we will add. &lt;/p&gt;  &lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;   &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;asp:Silverlight&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;ID&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="Xaml1"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;runat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="server"&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Source&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="~/ClientBin/BlurayDVDCompare.xap"&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;MinimumVersion&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="2.0.31005.0"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Width&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="1920"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Height&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="1200"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;Our UI will be pretty simple.&amp;nbsp; We will have a couple of textboxes which will be used so that a user can specify a URLs to images they wish to compare.&amp;nbsp; We will also add a couple of progress indicators so that we can show progress when the images are being loaded.&amp;nbsp;&amp;nbsp; We will also have three radio buttons to allow the user to select which comparison mode they wish to use.&amp;nbsp; &lt;/div&gt;

&lt;p&gt;The main part of our UI, will be the canvas that holds the two images that we are comparing.&amp;nbsp; Each image has a clip region which will be used to display parts of each image or to hide/show an image.&amp;nbsp; For example, in DualView mode, we want to display part of one image, and the remaining part of the second image.&amp;nbsp; If no clip regions were set, the default behavior would force the second image to cover the first.&amp;nbsp; The final thing we add is a border which we will be used to draw a line as a separator between the two images in DualView.&amp;nbsp; In Rectangle view, the border is used to draw the rectangle.&lt;/p&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;StackPanel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Canvas&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="canvas"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Background&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="White"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Width&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="1920"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Height&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="1080"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Margin&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="0,5,0,0"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;    &lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="img1"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Stretch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="None"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;        &lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image.Clip&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;RectangleGeometry&lt;/span&gt;  &lt;span style="color: rgb(255, 0, 0);"&gt;Rect&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="0,0,960,1080"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="rcImg1"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image.Clip&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="img2"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Stretch&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="None"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image.Clip&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;                &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;RectangleGeometry&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Rect&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="960,0,1920,1080"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="rcImg2"&lt;/span&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image.Clip&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Image&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;    &lt;br&gt;            &lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Border&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;x:Name&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="border"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Width&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="2"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Height&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="1920"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;Margin&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="0,0,0,0"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;BorderBrush&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="Bisque"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;BorderThickness&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="0"&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;Canvas&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;StackPanel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;h3&gt;Implementation&lt;/h3&gt;

&lt;div&gt;Since we want to allow the application to load images from the web, we define the LoadImages() method which accepts two URLs.&amp;nbsp; In order to do this, we create new BitmapImage objects with the given URLs.&amp;nbsp; This will load the images asynchronously and we will assign them as the image Source property for the two UIElements.&amp;nbsp; Once loading has finished, they will automatically appear in the canvas.&amp;nbsp; &lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; LoadImages(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; s1, &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt; s2)&lt;br&gt;{&lt;br&gt;    brLoading.Visibility = Visibility.Visible;&lt;br&gt;    spLoadingError.Visibility = Visibility.Collapsed;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.IsNullOrEmpty(s1) || &lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.IsNullOrEmpty(s2))&lt;br&gt;    {&lt;br&gt;        txtLoadingError.Text = &lt;span style="color: rgb(0, 96, 128);"&gt;"Invalid image location given"&lt;/span&gt;;&lt;br&gt;        brLoading.Visibility = Visibility.Collapsed;&lt;br&gt;        spLoadingError.Visibility = Visibility.Visible;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt;;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    prgLoading1.Value = 0;&lt;br&gt;    prgLoading2.Value = 0;&lt;br&gt;    &lt;br&gt;    _bmi1 = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BitmapImage();&lt;br&gt;    _bmi2 = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BitmapImage();&lt;br&gt;    _bmi1.DownloadProgress += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; EventHandler&amp;lt;DownloadProgressEventArgs&amp;gt;(bmi1_DownloadProgress);&lt;br&gt;    _bmi2.DownloadProgress += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; EventHandler&amp;lt;DownloadProgressEventArgs&amp;gt;(bmi2_DownloadProgress);&lt;br&gt;&lt;br&gt;    _bmi1.UriSource = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Uri(s1, UriKind.Absolute);&lt;br&gt;    _bmi2.UriSource = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Uri(s2, UriKind.Absolute);&lt;br&gt;&lt;br&gt;    img1.Source = _bmi1;&lt;br&gt;    img2.Source = _bmi2;           &lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; LoadImages(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; s1 &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;String&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; s2 &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;String&lt;/span&gt;)&lt;br&gt;    brLoading.Visibility = Visibility.Visible&lt;br&gt;    spLoadingError.Visibility = Visibility.Collapsed&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;String&lt;/span&gt;.IsNullOrEmpty(s1) &lt;span style="color: rgb(0, 0, 255);"&gt;OrElse&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;String&lt;/span&gt;.IsNullOrEmpty(s2) &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        txtLoadingError.Text = &lt;span style="color: rgb(0, 96, 128);"&gt;"Invalid image location given"&lt;/span&gt;&lt;br&gt;        brLoading.Visibility = Visibility.Collapsed&lt;br&gt;        spLoadingError.Visibility = Visibility.Visible&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;br&gt;    prgLoading1.Value = 0&lt;br&gt;    prgLoading2.Value = 0&lt;br&gt;&lt;br&gt;    _bmi1 = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; BitmapImage()&lt;br&gt;    _bmi2 = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; BitmapImage()&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;AddHandler&lt;/span&gt; _bmi1.DownloadProgress, &lt;span style="color: rgb(0, 0, 255);"&gt;AddressOf&lt;/span&gt; bmi1_DownloadProgress&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;AddHandler&lt;/span&gt; _bmi2.DownloadProgress, &lt;span style="color: rgb(0, 0, 255);"&gt;AddressOf&lt;/span&gt; bmi2_DownloadProgress&lt;br&gt;&lt;br&gt;    _bmi1.UriSource = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Uri(s1, UriKind.Absolute)&lt;br&gt;    _bmi2.UriSource = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Uri(s2, UriKind.Absolute)&lt;br&gt;&lt;br&gt;    img1.Source = _bmi1&lt;br&gt;    img2.Source = _bmi2&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;Now that we have a way to load images, the next step is to handle the different type of comparison modes.&amp;nbsp;&amp;nbsp; We will start with the dual view since that will be our default mode.&amp;nbsp; As mentioned earlier, the dual view basically shows both images side by side in such a way where both images are presented as one. The first image is partially shown, and the second is shown as a continuation of the first.&amp;nbsp; In order to do this, we track the mouse movement when it is inside of the canvas by setting up a MouseMove event handler.&amp;nbsp; Every time the mouse moves, we grab the new x,y coordinates.&amp;nbsp; Since dual view is only concerned with left/right movement, we only pass in the x coordinate.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; canvas_MouseMove(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, MouseEventArgs e)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x = e.GetPosition(canvas).X;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; y = e.GetPosition(canvas).Y;&lt;br&gt;    &lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_comparisonMode == ComparisonMode.DualView)&lt;br&gt;        DisplayDualView(x);&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_comparisonMode == ComparisonMode.Rectangle)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// ...&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; canvas_MouseMove(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Object&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; e &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; MouseEventArgs)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Dim&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt; = e.GetPosition(canvas).X&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Dim&lt;/span&gt; y &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt; = e.GetPosition(canvas).Y&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; _comparisonMode = ComparisonMode.DualView &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        DisplayDualView(x)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;ElseIf&lt;/span&gt; _comparisonMode = ComparisonMode.Rectangle &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;            &lt;span style="color: rgb(0, 128, 0);"&gt;' .....&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; DisplayDualView(&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x)&lt;br&gt;{&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, x, IMGHEIGHT);         &lt;span style="color: rgb(0, 128, 0);"&gt;// Show the first half (left side), up to xCoord&lt;/span&gt;&lt;br&gt;    rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(x, 0, IMGWIDTH, IMGHEIGHT);  &lt;span style="color: rgb(0, 128, 0);"&gt;// Show the second-half (right side), starting from xCoord&lt;/span&gt;&lt;br&gt;&lt;br&gt;    Canvas.SetLeft(border, x);                          &lt;span style="color: rgb(0, 128, 0);"&gt;// make the separator start at x&lt;/span&gt;&lt;br&gt;    Canvas.SetTop(border, 0);                           &lt;span style="color: rgb(0, 128, 0);"&gt;// set the separator to the top&lt;/span&gt;&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; DisplayDualView(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;)&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, x, IMGHEIGHT)         &lt;span style="color: rgb(0, 128, 0);"&gt;' Show the first half (left side), up to xCoord&lt;/span&gt;&lt;br&gt;    rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(x, 0, IMGWIDTH, IMGHEIGHT)  &lt;span style="color: rgb(0, 128, 0);"&gt;' Show the second-half (right side), starting from xCoord&lt;/span&gt;&lt;br&gt;&lt;br&gt;    Canvas.SetLeft(border, x) &lt;span style="color: rgb(0, 128, 0);"&gt;' make the separator start at x&lt;/span&gt;&lt;br&gt;    Canvas.SetTop(border, 0)  &lt;span style="color: rgb(0, 128, 0);"&gt;' set the separator to the top&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;The DisplayDualView() method sets the image clip region of the first image to be a rectangle starting from pixel 0 to pixel x, then sets the clip region of the second image to be the remaining pixels.&amp;nbsp; It uses our border to represent a break in the two images so that it is clear where our mouse is. Note that the border is defined as having a width of 2 and a height of 1080 in order to display as a single line separating the two images.&amp;nbsp; The border size is defined when the comparison mode is selected.&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;For the next comparison, we will focus on is the swap view mode.&amp;nbsp; This is the easiest one to implement.&amp;nbsp; In this, we show the first image in its entirety until the user places the mouse inside the canvas.&amp;nbsp; When that happens, the second image is displayed until the user moves the mouse outside of the canvas. &lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; DisplaySwapView(&lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; mouseOver)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (!mouseOver)&lt;br&gt;    {&lt;br&gt;        rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT);&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, 0, 0);&lt;br&gt;    }&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;br&gt;    {&lt;br&gt;        rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, 0, 0);&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT);&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; DisplaySwapView(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; mouseOver &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;Not&lt;/span&gt; mouseOver) &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT)&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, 0, 0)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Else&lt;/span&gt;&lt;br&gt;        rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, 0, 0)&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;&lt;/b&gt;&lt;/div&gt;

&lt;div&gt;Our DisplaySwapView() method is very basic in that it basically uses the clip region to show an entire image, or to show none of the image depending on the value of the passed in bool, mouseOver.&amp;nbsp; When mouseOver is false, we show the first image, when true we show the second image.&amp;nbsp; While we could have used the Visibility property to show/hide the images, we use clipping in order to be consistent with our other modes.&lt;/div&gt;

&lt;p&gt;The rectangle view is the last and most involved.&amp;nbsp; Since the user will be drawing a rectangle with the mouse, we must keep track of mouse movement, the state of the mouse button, and the coordinates for the rectangle.&amp;nbsp; We also need to display the rectangle as the user is drawing it and update clipped area of the image in real time in.&amp;nbsp; In order to make this easier, we create a class called MouseRectangle will contain the important information about the mouse state and rectangle size and location.&amp;nbsp; &lt;/p&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; MouseRectangle&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; MouseLeftButtonDown { get; set;}&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; Rect _rtg;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt;? _startX;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt;? _startY;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; RectangleIsDrawn { get; set; }&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; Rect Rectangle { get { &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _rtg;} }&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; MouseRectangle()&lt;br&gt;    {&lt;br&gt;        RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Reset()&lt;br&gt;    {&lt;br&gt;        _startX = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br&gt;        _startY = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br&gt;        RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; CalculateRectangle(&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x, &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; y)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_startX == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;            _startX = x;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_startY == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;            _startY = y;&lt;br&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;// pick the smaller of the two&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; left = Math.Min(_startX.Value, x);&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; top = Math.Min(_startY.Value, y);&lt;br&gt;&lt;br&gt;        _rtg = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(left, top, Math.Abs(_startX.Value - x), Math.Abs(_startY.Value - y));             &lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;bool&lt;/span&gt; InBoundsOfRect(&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x, &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; y)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (x &amp;gt; _rtg.Left &amp;amp;&amp;amp; x &amp;lt; _rtg.Right &amp;amp;&amp;amp;&lt;br&gt;            y &amp;gt; _rtg.Top &amp;amp;&amp;amp; y &amp;lt; _rtg.Bottom)&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Class&lt;/span&gt; MouseRectangle&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; privateMouseLeftButtonDown &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt; MouseLeftButtonDown() &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt; privateMouseLeftButtonDown&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Set&lt;/span&gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; value &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;)&lt;br&gt;            privateMouseLeftButtonDown = value&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Set&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; _rtg &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; Rect&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; _startX? &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; _startY? &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; privateRectangleIsDrawn &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt; RectangleIsDrawn() &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt; privateRectangleIsDrawn&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Set&lt;/span&gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; value &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;)&lt;br&gt;            privateRectangleIsDrawn = value&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Set&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;ReadOnly&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt; Rectangle() &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; Rect&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt; _rtg&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Get&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Property&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt;()&lt;br&gt;        RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; Reset()&lt;br&gt;        _startX = &lt;span style="color: rgb(0, 0, 255);"&gt;Nothing&lt;/span&gt;&lt;br&gt;        _startY = &lt;span style="color: rgb(0, 0, 255);"&gt;Nothing&lt;/span&gt;&lt;br&gt;        RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; CalculateRectangle(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; y &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Not&lt;/span&gt; _startX.HasValue &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;            _startX = x&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Not&lt;/span&gt; _startY.HasValue &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;            _startY = y&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;' pick the smaller of the two&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Dim&lt;/span&gt; left &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt; = Math.Min(_startX.Value, x)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Dim&lt;/span&gt; top &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt; = Math.Min(_startY.Value, y)&lt;br&gt;&lt;br&gt;        _rtg = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(left, top, Math.Abs(_startX.Value - x), Math.Abs(_startY.Value - y))&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Function&lt;/span&gt; InBoundsOfRect(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; y &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;) &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Boolean&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; x &amp;gt; _rtg.Left &lt;span style="color: rgb(0, 0, 255);"&gt;AndAlso&lt;/span&gt; x &amp;lt; _rtg.Right &lt;span style="color: rgb(0, 0, 255);"&gt;AndAlso&lt;/span&gt; y &amp;gt; _rtg.Top &lt;span style="color: rgb(0, 0, 255);"&gt;AndAlso&lt;/span&gt; y &amp;lt; _rtg.Bottom &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;Return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Function&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;The MouseLeftButtonDown property keeps track of whether the left button is currently being held down.&amp;nbsp; There is also a Rect (_rtg) object which contains the location and dimension of the rectangle that has been drawn.&amp;nbsp; The _startX and _startY member variables are nullable doubles used to keep track of the initial location where the mouse button was pressed.&amp;nbsp; &lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;Our CalculateRectangle() method is what we use to create the dimensions and location of the rectangle.&amp;nbsp; We start by looking at the inital x,y coordinates, and if they are not set, it means the rectangle hasn't been drawn.&amp;nbsp; In that case, a 0 pixel rectangle will be created.&amp;nbsp; If those values are already set, we create the rectangle based on the x and y values passed in.&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;The InBoundsOfRect() method is a simple helper function that will tell us whether or not an x,y coordinate is within the bounds of the rectangle. &lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;When in rectangle mode, the rectangle drawing begins when the mouse's left button is pressed down, and when the mouse is de-pressed, the drawing ends, and we consider the rectangle complete.&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; canvas_MouseLeftButtonDown(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, MouseButtonEventArgs e)&lt;br&gt;{&lt;br&gt;    _mouseRectangle.Reset();&lt;br&gt;    _mouseRectangle.MouseLeftButtonDown = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;;&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; canvas_MouseLeftButtonUp(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, MouseButtonEventArgs e)&lt;br&gt;{&lt;br&gt;    _mouseRectangle.MouseLeftButtonDown = &lt;span style="color: rgb(0, 0, 255);"&gt;false&lt;/span&gt;;&lt;br&gt;    _mouseRectangle.RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;true&lt;/span&gt;;&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; canvas_MouseLeftButtonDown(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Object&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; e &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; MouseButtonEventArgs)&lt;br&gt;    _mouseRectangle.Reset()&lt;br&gt;    _mouseRectangle.MouseLeftButtonDown = &lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; canvas_MouseLeftButtonUp(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Object&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; e &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; MouseButtonEventArgs)&lt;br&gt;    _mouseRectangle.MouseLeftButtonDown = &lt;span style="color: rgb(0, 0, 255);"&gt;False&lt;/span&gt;&lt;br&gt;    _mouseRectangle.RectangleIsDrawn = &lt;span style="color: rgb(0, 0, 255);"&gt;True&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;While in rectangle mode, when the mouse moves over the canvas, there are two different states.&amp;nbsp; If the mouse button is pressed, it means the user is drawing, and our goal is to update the rectangle to match the location where the user's mouse is in relation to the canvas.&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;...&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_comparisonMode == ComparisonMode.Rectangle)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_mouseRectangle.MouseLeftButtonDown)&lt;br&gt;        DrawRectangle(x,y);&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_mouseRectangle.RectangleIsDrawn)&lt;br&gt;        DisplayRectangleView(x,y);&lt;br&gt;}&lt;br&gt;...&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;...&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;ElseIf&lt;/span&gt; _comparisonMode = ComparisonMode.Rectangle &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; _mouseRectangle.MouseLeftButtonDown &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        DrawRectangle(x,y)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;ElseIf&lt;/span&gt; _mouseRectangle.RectangleIsDrawn &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        DisplayRectangleView(x,y)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;...&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;The DrawRectangle() method starts out by calculating the rectangle based on the current x,y coordinates.&amp;nbsp; We then set the border to represent our rectangle outline by sitting its location based on the _mouseRectangle's Rectangle property.&amp;nbsp; Finally we draw the first image in its entirety, and the second image will only display the portion of the image based on the rectangle's location and width/height information.&amp;nbsp; This will result in seeing the the first image with the rectangular portion showing the corresponding 2nd image.&lt;/div&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; DrawRectangle(&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x, &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; y)&lt;br&gt;{&lt;br&gt;    _mouseRectangle.CalculateRectangle(x,y);&lt;br&gt;&lt;br&gt;    Canvas.SetLeft(border, _mouseRectangle.Rectangle.Left);&lt;br&gt;    Canvas.SetTop(border, _mouseRectangle.Rectangle.Top);&lt;br&gt;    border.Width = _mouseRectangle.Rectangle.Width;&lt;br&gt;    border.Height = _mouseRectangle.Rectangle.Height;&lt;br&gt;&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT);&lt;br&gt;    rcImg2.Rect = _mouseRectangle.Rectangle;&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; DrawRectangle(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; y &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;)&lt;br&gt;    _mouseRectangle.CalculateRectangle(x, y)&lt;br&gt;&lt;br&gt;    Canvas.SetLeft(border, _mouseRectangle.Rectangle.Left)&lt;br&gt;    Canvas.SetTop(border, _mouseRectangle.Rectangle.Top)&lt;br&gt;    border.Width = _mouseRectangle.Rectangle.Width&lt;br&gt;    border.Height = _mouseRectangle.Rectangle.Height&lt;br&gt;&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT)&lt;br&gt;    rcImg2.Rect = _mouseRectangle.Rectangle&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;When the mouse button has been de-pressed, and the rectangle has been completed, we add the ability to do a mouse over effect for the rectangular region.&amp;nbsp; While the mouse is located outside of the drawn rectangle, we leave it as is.&amp;nbsp; If the user moves the mouse within the rectangle, we want them to see what the original looked like, so we display only the first image.&amp;nbsp; &lt;/div&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; DisplayRectangleView(&lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; x, &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; y)&lt;br&gt;{&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT);&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_mouseRectangle.InBoundsOfRect(x,y))&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; Rect(0, 0, 0, 0);&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;else&lt;/span&gt;&lt;br&gt;        rcImg2.Rect = _mouseRectangle.Rectangle;&lt;br&gt;&lt;br&gt;}&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&lt;b&gt;VB&lt;/b&gt;&lt;/div&gt;

&lt;div style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; line-height: 12pt; background-color: rgb(244, 244, 244); width: 97.5%; font-family: 'Courier New',courier,monospace; max-height: 200px; font-size: 8pt; cursor: text;" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; color: black; font-size: 8pt;" id="codeSnippet"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;Private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Sub&lt;/span&gt; DisplayRectangleView(&lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; x &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;ByVal&lt;/span&gt; y &lt;span style="color: rgb(0, 0, 255);"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;Double&lt;/span&gt;)&lt;br&gt;    rcImg1.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, IMGWIDTH, IMGHEIGHT)&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt; _mouseRectangle.InBoundsOfRect(x, y) &lt;span style="color: rgb(0, 0, 255);"&gt;Then&lt;/span&gt;&lt;br&gt;        rcImg2.Rect = &lt;span style="color: rgb(0, 0, 255);"&gt;New&lt;/span&gt; Rect(0, 0, 0, 0)&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;Else&lt;/span&gt;&lt;br&gt;        rcImg2.Rect = _mouseRectangle.Rectangle&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;If&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

  &lt;br&gt;&lt;/div&gt;

&lt;div&gt;&amp;nbsp;&lt;/div&gt;

&lt;h3&gt;Conclusion &amp;amp; Future Work&lt;/h3&gt;

&lt;p&gt;Overall, the blu-ray DVD comparison utility is a pretty straight-forward Silverlight application that you can use to see the differences between two images.&amp;nbsp; These images don't even need to be from blu-ray or DVD sources, but as long as they are the right dimension, a user can use it to see subtle differences between the two by allowing three different comparison modes.&amp;nbsp; For blu-ray purchases when you already own the DVD, I think it helps to see what kind of benefit you will get by moving to blu-ray.&amp;nbsp; It has been my experience that almost every blu-ray offers some type of picture upgrade over its DVD counterpart (and sound upgrade as well).&amp;nbsp; Sometimes the differences are subtle, sometimes they are incredibly better.&amp;nbsp; There are a lot of factors involved, but&amp;nbsp; &lt;/p&gt;

&lt;p&gt;Some future work that can be done to this is adding the ability to zoom in and out on the images in order to be able to see differences in a closer view.&amp;nbsp;&amp;nbsp; Soon there will be more movies which will be re-released on blu-ray and you can use the tool to compare the old release to the new one.&amp;nbsp; This will require zooming to see some of the more finer differences.&amp;nbsp; Other work can be done by creating a database to hold links, and have a control that allows a user to navigate to different movies and view different comparisons.&amp;nbsp; I have started doing both of these, but they were not ready for this article. &lt;/p&gt;

&lt;p&gt;One definite area that needs work is the UI.&amp;nbsp; I am not good at laying out user interfaces or creating new xaml styles, so I left them default.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;What would be really useful is a full motion comparison where you can use similar modes to see how two movies compare while in motion, but the biggest problem would be finding or creating the comparison material.&amp;nbsp; Playback would be limiting as well since it requires higher end CPUs/video cards.&amp;nbsp;&amp;nbsp; &lt;/p&gt;

&lt;h3&gt;Comparisons&lt;/h3&gt;

&lt;p&gt;Here are a few URLs you can use to play with the comparison utility:&lt;/p&gt;

&lt;table border="0" cellpadding="2" cellspacing="0" width="459"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;&amp;nbsp;&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;URL of Standard Def image (DVD) / HD (Blu-ray)&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;Prince Caspian&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/pc_1_sd.png" title="http://gmontrone.com/BDCompare/imgs/pc_1_sd.png"&gt;http://gmontrone.com/BDCompare/imgs/pc_1_sd.png&lt;/a&gt; 

        &lt;br&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/pc_1_hd.png" title="http://gmontrone.com/BDCompare/imgs/pc_1_hd.png"&gt;http://gmontrone.com/BDCompare/imgs/pc_1_hd.png&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;Prince Caspian&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/pc_2_sd.png" title="http://gmontrone.com/BDCompare/imgs/pc_1_sd.png"&gt;http://gmontrone.com/BDCompare/imgs/pc_2_sd.png&lt;/a&gt; 

        &lt;br&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/pc_2_hd.png" title="http://gmontrone.com/BDCompare/imgs/pc_1_hd.png"&gt;http://gmontrone.com/BDCompare/imgs/pc_2_hd.png&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;Iron Man&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/im_1_sd.png" title="http://gmontrone.com/BDCompare/imgs/im_1_sd.png"&gt;http://gmontrone.com/BDCompare/imgs/im_1_sd.png&lt;/a&gt; 

        &lt;br&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/im_1_hd.png" title="http://gmontrone.com/BDCompare/imgs/im_1_hd.png"&gt;http://gmontrone.com/BDCompare/imgs/im_1_hd.png&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;Iron Man&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/im_2_sd.png" title="http://gmontrone.com/BDCompare/imgs/im_2_sd.png"&gt;http://gmontrone.com/BDCompare/imgs/im_2_sd.png&lt;/a&gt; 

        &lt;br&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/im_2_hd.png" title="http://gmontrone.com/BDCompare/imgs/im_2_hd.png"&gt;http://gmontrone.com/BDCompare/imgs/im_2_hd.png&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="109"&gt;Entrapment&lt;/td&gt;

      &lt;td valign="top" width="348"&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/Ent_4_SD.png" title="http://gmontrone.com/BDCompare/imgs/Ent_4_SD.png"&gt;http://gmontrone.com/BDCompare/imgs/Ent_4_SD.png&lt;/a&gt; 

        &lt;br&gt;&lt;a href="http://gmontrone.com/BDCompare/imgs/Ent_4_HD.png" title="http://gmontrone.com/BDCompare/imgs/Ent_4_HD.png"&gt;http://gmontrone.com/BDCompare/imgs/Ent_4_HD.png&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Note: Images were copied from the blu-ray threads of A/V Science Forum: &lt;a href="http://www.avsforum.com/"&gt;www.avsforum.com&lt;/a&gt; .&lt;/p&gt;

&lt;h3&gt;Thanks&lt;/h3&gt;

&lt;p&gt;I would like to thank &lt;a href="http://www.brianpeek.com/" target="_blank"&gt;Brian Peek&lt;/a&gt; for introducing me to Coding4Fun, and taking the time to review my article and test the code.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9515699" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/media/default.aspx">media</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Silverlight Dynamic Video Puzzle</title><link>http://blogs.msdn.com/coding4fun/archive/2008/01/22/7163484.aspx</link><pubDate>Tue, 22 Jan 2008 22:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7163484</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/7163484.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=7163484</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=7163484</wfw:comment><description>&lt;P&gt;My blog title says it all, &lt;A href="http://www.betterthaneveryone.com/" mce_href="http://www.betterthaneveryone.com/"&gt;Monkey see, Monkey Build&lt;/A&gt;.&amp;nbsp; I saw Microsoft Surface's video puzzle and I needed to build it.&amp;nbsp; I took this opportunity to play with Microsoft's new technology, &lt;A href="http://silverlight.net/" mce_href="http://silverlight.net"&gt;Silverlight&lt;/A&gt; and build my own puzzle game.&amp;nbsp; I couldn't find anything out on the Internet regarding dynamic video creation with Silverlight so I took it upon myself to do this.&amp;nbsp; The screen shots in this demo will be in c#, however, everything should hold true for VB.&lt;/P&gt;
&lt;P&gt;Clint Rutkas - Academic Developer Evangelist - Microsoft &lt;BR&gt;&lt;A href="http://www.betterthaneveryone.com/" mce_href="http://www.betterthaneveryone.com/"&gt;Monkey see, Monkey Build&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Difficulty: &lt;/B&gt;Intermediate &lt;BR&gt;&lt;B&gt;Time Required:&lt;/B&gt; 6-10 hours &lt;BR&gt;&lt;B&gt;Cost: &lt;/B&gt;Free &lt;BR&gt;&lt;B&gt;Software: &lt;/B&gt;&lt;B&gt;&lt;A href="http://www.microsoft.com/express/" mce_href="http://www.microsoft.com/express/"&gt;Visual Studio Express&lt;/A&gt;, &lt;A href="http://silverlight.net/GetStarted/" mce_href="http://silverlight.net/GetStarted/"&gt;Silverlight SDK and Runtime for &lt;STRIKE&gt;1.1&lt;/STRIKE&gt; 2.0 beta2&lt;/A&gt;&lt;/B&gt; &lt;BR&gt;&lt;B&gt;Download: &lt;/B&gt;&lt;A href="http://www.peacelovecode.com/code/silverlight/puzzle/1.0.0.0/surfacePuzzleDemo.zip" mce_href="http://www.peacelovecode.com/code/silverlight/puzzle/1.0.0.0/surfacePuzzleDemo.zip"&gt;&lt;STRIKE&gt;Download c#&lt;/STRIKE&gt;&lt;/A&gt;&lt;STRIKE&gt; - &lt;/STRIKE&gt;&lt;A href="http://www.peacelovecode.com/code/silverlight/puzzle/1.0.0.0/surfacePuzzleDemoVB.zip" mce_href="http://www.peacelovecode.com/code/silverlight/puzzle/1.0.0.0/surfacePuzzleDemoVB.zip"&gt;&lt;STRIKE&gt;Download VB&lt;/STRIKE&gt;&lt;/A&gt; Updated to Silverlight 2.0 Beta - &lt;A href="http://www.peacelovecode.com/code/silverlight/puzzle/2.0.0.0/VideoRealTimeBreakUp.zip"&gt;Download c#&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Updates: &lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Fixed the Silverlight.Js, used the new version that is included with the 1.1 Refresh SDK so the proper installer will prompt now.&amp;nbsp; Downloads are corrected also too. &lt;/LI&gt;
&lt;LI&gt;6/20/2008&amp;nbsp;- Verified the solution works with Silverlight 2.0beta2.&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Spec's, Requirements and headaches&lt;/H2&gt;
&lt;P&gt;So I had a few mental requirements for the application to "entertain" myself.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Be able to rotate the video blocks &lt;/LI&gt;
&lt;LI&gt;Be able to translate &lt;/LI&gt;
&lt;LI&gt;Be able to increase the difficulty of the puzzle on the fly. &lt;/LI&gt;
&lt;LI&gt;Zero interaction with the keyboard&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Simple, right?&amp;nbsp; You want to see a demo too see some awesomeness?&amp;nbsp; No problem, &lt;A href="http://www.betterthaneveryone.com/archive/2008/01/06/silverlight-ms-surface-puzzle-like-demo.aspx" mce_href="http://www.betterthaneveryone.com/archive/2008/01/06/silverlight-ms-surface-puzzle-like-demo.aspx"&gt;head over to my site&lt;/A&gt; and see it in person.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Microsoft Surface Video Puzzle&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=180 alt=sl_9[1] src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/sl_91.jpg" width=240 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/sl_91.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Clint's Silverlight Video Puzzle&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image1.png" mce_href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image1.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=219 alt=image[1] src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image1_thumb.png" width=240 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image1_thumb.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;H2&gt;To the Internet Batman!&lt;/H2&gt;
&lt;P&gt;To help save time building this, I first when out and attempting to see if anything has been done anything close to this before.&amp;nbsp; I found&lt;/P&gt;
&lt;P&gt;Also I need 2 small controls which I found out are in the Silverlight SDK.&amp;nbsp; I needed horizontal slider controls for this app.&amp;nbsp; While I know a text box control would have worked just as well, see requirement 4.&lt;/P&gt;
&lt;P&gt;On the Silverlight community site, there is also a very nice demo showing the &lt;A href="http://silverlight.net/samples/1.1/SilverlightSurface/Run/default.html" mce_href="http://silverlight.net/samples/1.1/SilverlightSurface/Run/default.html"&gt;photo application in Surface with source code&lt;/A&gt;.&amp;nbsp; This is where I learned the majority of what I needed.&amp;nbsp; This application was very close to where I needed to go with mine.&amp;nbsp; However, while it was close, it wasn't perfect.&amp;nbsp; Parts of the photo application code are pretty much copied.&amp;nbsp; This isn't the best way of doing this, but it is one way of doing it.&lt;/P&gt;
&lt;H2&gt;Toolbelt: check, Keyboard: check, Band-aids: check&lt;/H2&gt;
&lt;P&gt;So on to building it.&amp;nbsp; I first started off with a new project and some XAML.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_6.png" mce_href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_6.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=269 alt=image src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image.png" width=400 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image.png"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;After clicking "OK", I get a very nice blank project.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_7.png" mce_href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_7.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=367 alt=image src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_3.png" width=400 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We'll want to add an additional XAML object for the video blocks.&amp;nbsp; Go to "&lt;STRONG&gt;Project -&amp;gt; Add New Item&lt;/STRONG&gt;", select Silverlight Page, name it "VideoBlock.xaml" and click "OK".&lt;/P&gt;
&lt;P&gt;So now we have everything we need for the most part, grab any video you have, if you don't have one, go to a site like &lt;A href="http://teamxbox.com/" mce_href="http://teamxbox.com"&gt;TeamXbox&lt;/A&gt; and grab one from there.&amp;nbsp; I used Windows Movie Maker and grabbed a snippet from The Office (best show ever).&lt;/P&gt;
&lt;P&gt;In addition to all these files, we need to add in a reference to the Silverlight SDK to get a hold of the Slider controls. Go to "&lt;STRONG&gt;Project -&amp;gt; Add Reference&lt;/STRONG&gt;", and add in the &lt;STRONG&gt;Silverlight.Samples.Controls.dll&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;While we have a reference in the application, we still need the XAML to know about this also.&amp;nbsp; So we'll add a line to link this in.&amp;nbsp; It is the xmlns line that I bolded, XML name space, real clever, eh?&amp;nbsp; We'll also&amp;nbsp; change the background color to a gray so we can see it more easy.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Canvas&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="parentCanvas"&lt;/SPAN&gt;
        &lt;SPAN class=attr&gt;xmlns&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/client/2007"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;xmlns:x&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;Loaded&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Page_Loaded"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;x:Class&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="c4f_SilverlightVideoDemo.VideoBlock;assembly=ClientBin/c4f_SilverlightVideoDemo.dll"&lt;/SPAN&gt;
        &lt;STRONG&gt;&lt;SPAN class=attr&gt;xmlns:uicontrol&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll"&lt;/SPAN&gt;&lt;/STRONG&gt;
        &lt;SPAN class=attr&gt;Background&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="#CCCCCC" &lt;SPAN class=attr&gt;Width&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="640"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Height&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="480"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;Canvas&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
	overflow: auto;
}
.csharpcode pre { margin: 0em;  }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;H2&gt;Silverlight 101&lt;/H2&gt;
&lt;P&gt;So in Silverlight, you have brushes which can "painted" onto objects.&amp;nbsp; One of these brushes is called the VideoBrush which I'll talk about later on.&amp;nbsp; For playing videos, one would use the &lt;STRONG&gt;MediaElement&lt;/STRONG&gt;.&amp;nbsp; Lets add this to our canvas.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;MediaElement&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="media"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Source&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="theOffice.wmv"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Now if we run the application at this point, we'll get the video playing.&amp;nbsp; Lets sit back with some popcorn and enjoy this victory for a second or two.&amp;nbsp; Now that we have this, lets hide it since this isn't what we want.&amp;nbsp; We do that by adding in an XML attribute &lt;STRONG&gt;Opacity&lt;/STRONG&gt; and setting it to 0.&lt;/P&gt;
&lt;P&gt;On top of that element, lets add on some TextBlock elements and the sliders.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Canvas&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="parentCanvas"&lt;/SPAN&gt;
        &lt;SPAN class=attr&gt;xmlns&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/client/2007"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;xmlns:x&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;Loaded&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Page_Loaded"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;x:Class&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="VideoCreatedObjectTest.Page;assembly=ClientBin/VideoCreatedObjectTest.dll"&lt;/SPAN&gt;
        &lt;SPAN class=attr&gt;xmlns:uicontrol&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="clr-namespace:Silverlight.Samples.Controls;assembly=ClientBin/Silverlight.Samples.Controls.dll"&lt;/SPAN&gt; 
        &lt;SPAN class=attr&gt;Background&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="#CCCCCC"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;MediaElement&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="media"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Source&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="theOffice.wmv"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Opacity&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="0"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;TextBlock&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="sliderX"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Top&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="5"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Left&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="5"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Text&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="X (01):"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;TextBlock&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="sliderY"&lt;/SPAN&gt;  &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Top&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="21"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Left&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="5"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Text&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Y (01):"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;uicontrol:Slider&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="hSliderX"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Top&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="8"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Left&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="55"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;uicontrol:Slider&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="hSliderY"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Top&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="25"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Canvas&lt;/SPAN&gt;.&lt;SPAN class=attr&gt;Left&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="55"&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;Canvas&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;So now when we run the application, we get a blank screen but we can hear the audio from the video.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_8.png" mce_href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_8.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=345 alt=image src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_4.png" width=400 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Super exciting I know.&amp;nbsp; Now if you resize the browser, you'll notice also the canvas isn't resizing automatically.&amp;nbsp; Lets fix that.&lt;/P&gt;
&lt;P&gt;So in the code behind lets add in a new event on the BrowserHost object with the Resize event.&amp;nbsp; We'll attach the Browser_Resize event to it.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; BrowserHost_Resize(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; sender, EventArgs e)
{
    &lt;SPAN class=rem&gt;// Set size to host size&lt;/SPAN&gt;
    Width = BrowserHost.ActualWidth;
    Height = BrowserHost.ActualHeight;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB.Net&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; BrowserHost_Resize(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; EventArgs)
    &lt;SPAN class=rem&gt;' Set size to host size&lt;/SPAN&gt;
    Width = BrowserHost.ActualWidth
    Height = BrowserHost.ActualHeight
&lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; Sub&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;H2&gt;Video as Paint?&lt;/H2&gt;
&lt;P&gt;So now that we have that small part done, lets get into more of the nitty gritty with video.&amp;nbsp; As I said earlier, you'll use a &lt;STRONG&gt;VideoBrush&lt;/STRONG&gt; to paint on the video.&lt;/P&gt;
&lt;P&gt;We'll use the brush as the Fill for the rectangle we'll be using to show off the clip of the video we want.&lt;/P&gt;
&lt;H3&gt;Still not moving?&amp;nbsp; Try explosives?&lt;/H3&gt;
&lt;P&gt;While doing this project, I discovered an interesting thing when using the VideoBrush and dynamically appending items on.&amp;nbsp; Having items in the XAML rather than programmatically creating everything caused it not to work properly.&amp;nbsp; Nothing would appear.&lt;/P&gt;
&lt;H3&gt;Throwback to how UI's use to be done&lt;/H3&gt;
&lt;P&gt;The XAML for this is a simplified version of the Surface demo Photo.XAML.&amp;nbsp; You have a 3 Rectangles, 1 for the video, 1 for translation and 1 for rotation.&amp;nbsp; Simple, no?&amp;nbsp; Doing all this by hand gets a bit annoying since you need to nest everything properly.&lt;/P&gt;
&lt;P&gt;Here is the more interesting &lt;STRONG&gt;VideoBrush&lt;/STRONG&gt; code.&amp;nbsp; You need the name from the &lt;STRONG&gt;MediaElement &lt;/STRONG&gt;along with the how much of an offset you want the video.&amp;nbsp; Since your moving the "camera" in, this value needs to be negative.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_10.png" mce_href="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_10.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=395 alt=image src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_5.png" width=654 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/image_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;delegate&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; SetActiveVideo(VideoBlock videoBlock, ActionType actionType, Point photoCenter, Point lastPosition);
&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; VideoBlock(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; mediaName, &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; offsetX, &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; offsetY, &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; rectWidth, &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; rectHeight, SetActiveVideo functionPointer)
{
    &lt;SPAN class=rem&gt;// additional code here&lt;/SPAN&gt;
    Rectangle video = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Rectangle();
    VideoBrush vb = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; VideoBrush();
    TranslateTransform videoTransform = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; TranslateTransform();
    videoTransform.X = -1 * offsetX;
    videoTransform.Y = -1 * offsetY;

    vb.SourceName = mediaName;
    vb.Transform = videoTransform;
    vb.Stretch = Stretch.None;
    vb.AlignmentX = AlignmentX.Left;
    vb.AlignmentY = AlignmentY.Top;

    video.Width = Width;
    video.Height = Height;
    video.Fill = vb;
    &lt;SPAN class=rem&gt;// additional code here&lt;/SPAN&gt;
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB.Net&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Delegate&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; SetActiveVideo(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; videoBlock &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; VideoBlock, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; actionType &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; ActionType, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; photoCenter &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Point, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; lastPosition &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Point)
&lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; VideoBlock(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; mediaName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; offsetX &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; offsetY &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; rectWidth &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; rectHeight &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; functionPointer &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; SetActiveVideo)
      &lt;SPAN class=rem&gt;' additional code here&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; video &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Rectangle = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; Rectangle
      &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; vb &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; VideoBrush = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; VideoBrush
      &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; videoTransform &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; TranslateTransform = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; TranslateTransform
      videoTransform.X = ((1 * offsetX)  * -1)
      videoTransform.Y = ((1 * offsetY)  * -1)
      vb.SourceName = mediaName
      vb.Transform = videoTransform
      vb.Stretch = Stretch.None
      vb.AlignmentX = AlignmentX.Left
      vb.AlignmentY = AlignmentY.Top
      video.Width = Width
      video.Height = Height
      video.Fill = vb
      &lt;SPAN class=rem&gt;' additional code here&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; Sub&lt;/PRE&gt;
&lt;H3&gt;There is a mouse on my object!&lt;/H3&gt;
&lt;P&gt;So now we have our video block element prepped and ready.&amp;nbsp; There are additional events added in, but they are fairly boiler plate.&amp;nbsp; When looking at this code, you see I pass in a delegate, a function pointer if you will.&amp;nbsp; Why do I do this?&amp;nbsp; This is how the mouse position is calculated.&amp;nbsp; I bet this could be redone better, but this is how I did it.&amp;nbsp; The parent element, the canvas, has the mouse position I need to calculate proper translation and rotation, however, mouse events on the VideoBlock object will give me the mouse position only on that element.&amp;nbsp; So I may be at point 150, 150 on the screen, on the VideoBlock object, I'm actually at 10,15.&lt;/P&gt;
&lt;P&gt;Additionally, this approach allows me to move the VideoBlock to the top of the objects.&amp;nbsp; Once again, this could be done other ways, this is how I chose to do it.&lt;/P&gt;
&lt;P&gt;So on all my mouse related all boils down to doing the exact same function.&amp;nbsp; Since events are bubbled, a mouse movement on a VideoBlock is also a mouse movement on the root canvas.&lt;/P&gt;
&lt;P&gt;C#&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; root_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; sender, MouseEventArgs e)
{
    HandleMouseLeftButtonDown(ActionType.Selecting, e);
}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; translateControls_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; sender, MouseEventArgs e)
{
    HandleMouseLeftButtonDown(ActionType.Moving, e);
}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; rotateScaleControls_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; sender, MouseEventArgs e)
{
    HandleMouseLeftButtonDown(ActionType.RotatingScaling, e);
}

&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; HandleMouseLeftButtonDown(ActionType actionType, MouseEventArgs e)
{
    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (functionPointer != &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;)
        functionPointer(&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;, actionType,
            &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Point(translateTransform.X + rotateTransform.CenterX,
                  translateTransform.Y + rotateTransform.CenterY), e.GetPosition(&lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;));
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;VB.Net&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; root_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; MouseEventArgs)
        HandleMouseLeftButtonDown(ActionType.Selecting, e)
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; translateControls_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; MouseEventArgs)
        HandleMouseLeftButtonDown(ActionType.Moving, e)
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; rotateScaleControls_MouseLeftButtonDown(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; MouseEventArgs)
        HandleMouseLeftButtonDown(ActionType.RotatingScaling, e)
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt;
    
    &lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; HandleMouseLeftButtonDown(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; actionType &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; ActionType, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; MouseEventArgs)
        &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;Not&lt;/SPAN&gt; (functionPointer) &lt;SPAN class=kwrd&gt;Is&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Nothing&lt;/SPAN&gt;) &lt;SPAN class=kwrd&gt;Then&lt;/SPAN&gt;
            functionPointer(&lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;, actionType, &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; Point((translateTransform.X + rotateTransform.CenterX), &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (translateTransform.Y + rotateTransform.CenterY)), e.GetPosition(&lt;SPAN class=kwrd&gt;Nothing&lt;/SPAN&gt;))
        &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; Sub&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;H2&gt;You built a car out of all these parts?&lt;/H2&gt;
&lt;P&gt;So we move back from VideoBlock.xaml.cs to Page.xaml.cs&lt;/P&gt;
&lt;P&gt;We've created our primary object for painting video, lets populate it.&amp;nbsp; First, how do we know how big the media file is?&amp;nbsp; With the &lt;STRONG&gt;MediaOpened&lt;/STRONG&gt; event!&amp;nbsp; And we'll restart the video with the &lt;STRONG&gt;MediaEnded&lt;/STRONG&gt; event.&lt;/P&gt;
&lt;P&gt;So here is the full Page_Load event from Page.xaml.cs&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Page_Loaded(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; o, EventArgs e)
{
    &lt;SPAN class=rem&gt;// Required to initialize variables&lt;/SPAN&gt;
    InitializeComponent();

    BrowserHost.Resize +=&lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; EventHandler(BrowserHost_Resize);
    media.MediaOpened += &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; EventHandler(media_MediaOpened);
    media.MediaEnded += &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; EventHandler(media_MediaEnded);
    MouseLeftButtonUp += Page_MouseLeftButtonUpOrLeave;
    MouseLeave += Page_MouseLeftButtonUpOrLeave;
    MouseMove += Page_MouseMove;

    hSliderX.Range = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; ValueRange(1, 20);
    hSliderY.Range = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; ValueRange(1, 20);
    hSliderX.ValueChanged += &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; EventHandler(slider_ValueChanged);
    hSliderY.ValueChanged += &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; EventHandler(slider_ValueChanged);

    hSliderX.SetValue(ZIndexProperty, 500);
    hSliderY.SetValue(ZIndexProperty, 500);
    sliderX.SetValue(ZIndexProperty, 500);
    sliderY.SetValue(ZIndexProperty, 500);
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB.Net&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; Page_Loaded(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; o &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; EventArgs)
        &lt;SPAN class=rem&gt;' Required to initialize variables&lt;/SPAN&gt;
        InitializeComponent
        &lt;SPAN class=kwrd&gt;AddHandler&lt;/SPAN&gt; BrowserHost.Resize, &lt;SPAN class=kwrd&gt;AddressOf&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;.BrowserHost_Resize
        &lt;SPAN class=kwrd&gt;AddHandler&lt;/SPAN&gt; media.MediaOpened, &lt;SPAN class=kwrd&gt;AddressOf&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;.media_MediaOpened
        &lt;SPAN class=kwrd&gt;AddHandler&lt;/SPAN&gt; media.MediaEnded, &lt;SPAN class=kwrd&gt;AddressOf&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;.media_MediaEnded
        MouseLeftButtonUp = (MouseLeftButtonUp + Page_MouseLeftButtonUpOrLeave)
        MouseLeave = (MouseLeave + Page_MouseLeftButtonUpOrLeave)
        MouseMove = (MouseMove + Page_MouseMove)
        hSliderX.Range = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; ValueRange(1, 20)
        hSliderY.Range = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; ValueRange(1, 20)
        &lt;SPAN class=kwrd&gt;AddHandler&lt;/SPAN&gt; hSliderX.ValueChanged, &lt;SPAN class=kwrd&gt;AddressOf&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;.slider_ValueChanged
        &lt;SPAN class=kwrd&gt;AddHandler&lt;/SPAN&gt; hSliderY.ValueChanged, &lt;SPAN class=kwrd&gt;AddressOf&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Me&lt;/SPAN&gt;.slider_ValueChanged
        hSliderX.SetValue(ZIndexProperty, 500)
        hSliderY.SetValue(ZIndexProperty, 500)
        sliderX.SetValue(ZIndexProperty, 500)
        sliderY.SetValue(ZIndexProperty, 500)
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; Sub&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;So now that we have the media loaded, we can use the media.NaturalVideoWidth and media.NaturalVideoHeight properties to find out exactly how big the video is!&amp;nbsp; I have a function that is fired off when the page is first loaded or a slider value has changed.&amp;nbsp; This is how we chop it up.&lt;/P&gt;
&lt;P&gt;&lt;IMG style="MARGIN: 0px 0px 0px 5px" height=180 alt=mince_garlic[1] src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/mince_garlic1.jpg" width=240 border=0 mce_src="http://www.coding4fun.net/images/SilverlightsVideoPuzzle_E3FF/mince_garlic1.jpg"&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; prepVideoObjects()
{
    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPhotosX = (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt;)hSliderX.Value;
    &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; totalPhotosY = (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt;)hSliderY.Value;

    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (lastX != totalPhotosX || lastY != totalPhotosY)
    {
        lastX = totalPhotosX;
        lastY = totalPhotosY;

        sliderX.Text = &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN class=str&gt;"X ({0}):"&lt;/SPAN&gt;, totalPhotosX.ToString(&lt;SPAN class=str&gt;"D2"&lt;/SPAN&gt;));
        sliderY.Text = &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN class=str&gt;"Y ({0}):"&lt;/SPAN&gt;, totalPhotosY.ToString(&lt;SPAN class=str&gt;"D2"&lt;/SPAN&gt;));

        &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; rectWidth = (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt;)Math.Floor(media.NaturalVideoWidth / totalPhotosX);
        &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; rectHeight = (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt;)Math.Floor(media.NaturalVideoHeight / totalPhotosY);
        Random random = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Random();

        &lt;SPAN class=rem&gt;// remove all old videoblocks&lt;/SPAN&gt;
        &lt;SPAN class=kwrd&gt;for&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; i = Children.Count - 1; i &amp;gt; 0; i--)
        {
            &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (Children[i].GetType() == &lt;SPAN class=kwrd&gt;typeof&lt;/SPAN&gt;(VideoBlock))
                Children.RemoveAt(i);
        }

        &lt;SPAN class=kwrd&gt;for&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; x = 0; x &amp;lt; totalPhotosX; x++)
        {
            &lt;SPAN class=kwrd&gt;for&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; y = 0; y &amp;lt; totalPhotosY; y++)
            {
                VideoBlock vb = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; VideoBlock(media.Name, x * rectWidth, y * rectHeight, rectWidth, rectHeight, SetActivePhoto);
                shuffle(vb, random);
                Children.Add(vb);
            }
        }
    }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB.Net&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; prepVideoObjects()
        &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; totalPhotosX &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = &lt;SPAN class=kwrd&gt;CType&lt;/SPAN&gt;(hSliderX.Value,&lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;)
        &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; totalPhotosY &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = &lt;SPAN class=kwrd&gt;CType&lt;/SPAN&gt;(hSliderY.Value,&lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;)
        &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt; ((lastX &amp;lt;&amp;gt; totalPhotosX)  _
                    &lt;SPAN class=kwrd&gt;OrElse&lt;/SPAN&gt; (lastY &amp;lt;&amp;gt; totalPhotosY)) &lt;SPAN class=kwrd&gt;Then&lt;/SPAN&gt;
            lastX = totalPhotosX
            lastY = totalPhotosY
            sliderX.Text = &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN class=str&gt;"X ({0}):"&lt;/SPAN&gt;, totalPhotosX.ToString(&lt;SPAN class=str&gt;"D2"&lt;/SPAN&gt;))
            sliderY.Text = &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN class=str&gt;"Y ({0}):"&lt;/SPAN&gt;, totalPhotosY.ToString(&lt;SPAN class=str&gt;"D2"&lt;/SPAN&gt;))
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; rectWidth &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = &lt;SPAN class=kwrd&gt;CType&lt;/SPAN&gt;(Math.Floor((media.NaturalVideoWidth / totalPhotosX)),&lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;)
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; rectHeight &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = &lt;SPAN class=kwrd&gt;CType&lt;/SPAN&gt;(Math.Floor((media.NaturalVideoHeight / totalPhotosY)),&lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;)
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; random &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Random = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; Random
            &lt;SPAN class=rem&gt;' remove all old videoblocks&lt;/SPAN&gt;
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; i &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = (Children.Count - 1)
            &lt;SPAN class=kwrd&gt;Do&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;While&lt;/SPAN&gt; (i &amp;gt; 0)
                &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt; (Children(i).&lt;SPAN class=kwrd&gt;GetType&lt;/SPAN&gt; = &lt;SPAN class=kwrd&gt;GetType&lt;/SPAN&gt;(VideoBlock)) &lt;SPAN class=kwrd&gt;Then&lt;/SPAN&gt;
                    Children.RemoveAt(i)
                &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt;
                i = (i - 1)
            &lt;SPAN class=kwrd&gt;Loop&lt;/SPAN&gt;
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; x &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = 0
            &lt;SPAN class=kwrd&gt;Do&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;While&lt;/SPAN&gt; (x &amp;lt; totalPhotosX)
                &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; y &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt; = 0
                &lt;SPAN class=kwrd&gt;Do&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;While&lt;/SPAN&gt; (y &amp;lt; totalPhotosY)
                    &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; vb &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; VideoBlock = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; VideoBlock(media.Name, (x * rectWidth), (y * rectHeight), rectWidth, rectHeight, SetActivePhoto)
                    shuffle(vb, random)
                    Children.Add(vb)
                    y = (y + 1)
                &lt;SPAN class=kwrd&gt;Loop&lt;/SPAN&gt;
                x = (x + 1)
            &lt;SPAN class=kwrd&gt;Loop&lt;/SPAN&gt;
        &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Last but not least, lets see how the mouse movements are handled on the page.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Page_MouseMove(&lt;SPAN class=kwrd&gt;object&lt;/SPAN&gt; sender, MouseEventArgs e)
{
    &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt; != activeVideoBlock)
    {
        &lt;SPAN class=rem&gt;// Perform the appropriate transform on the active photo&lt;/SPAN&gt;
        Point position = e.GetPosition(&lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;);
        &lt;SPAN class=kwrd&gt;switch&lt;/SPAN&gt; (currentActionType)
        {
            &lt;SPAN class=kwrd&gt;case&lt;/SPAN&gt; VideoBlock.ActionType.Moving:
                &lt;SPAN class=rem&gt;// Move it by the amount of the mouse move&lt;/SPAN&gt;
                activeVideoBlock.Translate(position.X - currentLastPosition.X, position.Y - currentLastPosition.Y);
                &lt;SPAN class=kwrd&gt;break&lt;/SPAN&gt;;
            &lt;SPAN class=kwrd&gt;case&lt;/SPAN&gt; VideoBlock.ActionType.RotatingScaling:
                &lt;SPAN class=rem&gt;// Rotate it according to the angle the mouse moved around the photo's center&lt;/SPAN&gt;
                &lt;SPAN class=kwrd&gt;double&lt;/SPAN&gt; radiansToDegrees = 360 / (2 * Math.PI);
                &lt;SPAN class=kwrd&gt;double&lt;/SPAN&gt; lastAngle = Math.Atan2(currentLastPosition.Y - currentVideoCenter.Y, currentLastPosition.X - currentVideoCenter.X) * radiansToDegrees;
                &lt;SPAN class=kwrd&gt;double&lt;/SPAN&gt; currentAngle = Math.Atan2(position.Y - currentVideoCenter.Y, position.X - currentVideoCenter.X) * radiansToDegrees;
                activeVideoBlock.Rotate(currentAngle - lastAngle);

                &lt;SPAN class=kwrd&gt;break&lt;/SPAN&gt;;
        }
        currentLastPosition = position;
    }
}&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB.Net&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;Private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; Page_MouseMove(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; sender &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Object&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; e &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; MouseEventArgs)
        &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;Not&lt;/SPAN&gt; (activeVideoBlock) &lt;SPAN class=kwrd&gt;Is&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Nothing&lt;/SPAN&gt;) &lt;SPAN class=kwrd&gt;Then&lt;/SPAN&gt;
            &lt;SPAN class=rem&gt;' Perform the appropriate transform on the active photo&lt;/SPAN&gt;
            &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; position &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Point = e.GetPosition(&lt;SPAN class=kwrd&gt;Nothing&lt;/SPAN&gt;)
            &lt;SPAN class=kwrd&gt;Select&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Case&lt;/SPAN&gt; (currentActionType)
                &lt;SPAN class=kwrd&gt;Case&lt;/SPAN&gt; VideoBlock.ActionType.Moving
                    &lt;SPAN class=rem&gt;' Move it by the amount of the mouse move&lt;/SPAN&gt;
                    activeVideoBlock.Translate((position.X - currentLastPosition.X), (position.Y - currentLastPosition.Y))
                &lt;SPAN class=kwrd&gt;Case&lt;/SPAN&gt; VideoBlock.ActionType.RotatingScaling
                    &lt;SPAN class=rem&gt;' Rotate it according to the angle the mouse moved around the photo's center&lt;/SPAN&gt;
                    &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; radiansToDegrees &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Double&lt;/SPAN&gt; = (360 / (2 * Math.PI))
                    &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; lastAngle &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Double&lt;/SPAN&gt; = (Math.Atan2((currentLastPosition.Y - currentVideoCenter.Y), (currentLastPosition.X - currentVideoCenter.X)) * radiansToDegrees)
                    &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; currentAngle &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Double&lt;/SPAN&gt; = (Math.Atan2((position.Y - currentVideoCenter.Y), (position.X - currentVideoCenter.X)) * radiansToDegrees)
                    activeVideoBlock.Rotate((currentAngle - lastAngle))
            &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Select&lt;/SPAN&gt;
            currentLastPosition = position
        &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; Sub&lt;/PRE&gt;
&lt;H2&gt;Wrapping it up&lt;/H2&gt;
&lt;P&gt;So as you can see, Silverlight is pretty powerful once you figure out how to tame it.&amp;nbsp; I used example base code and modified it to suit my own needs to dynamically alter a video in real time.&amp;nbsp; There are a few code tweaks that could happen to improve this code and I'm betting with some additional effort, one could remove the delegate.&amp;nbsp; In addition, you could add in edge detection code and a timer to make this into a true video puzzle game.&amp;nbsp; This was a pet project I worked on while I was at the airport.&amp;nbsp; This demo can be downgraded to Silverlight 1.0 also.&amp;nbsp; I don't believe anything I did was really Silverlight 1.1 (now 2.0) only.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Clint's Bio:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Clint is an academic developer evangelist for Microsoft.&amp;nbsp; His two primary development languages are C# and JavaScript. He has built a &lt;A href="http://msdn.microsoft.com/coding4fun/coolapplications/disco/default.aspx" mce_href="http://msdn.microsoft.com/coding4fun/coolapplications/disco/default.aspx"&gt;Disco Dance Floor&lt;/A&gt; too! In his off time, he whips up other random weird projects and does twenty something activities with his friends.&amp;nbsp; His next two big projects are an automated bartender and a skateboard segway.&amp;nbsp; Clint’s blog is &lt;A href="http://betterthaneveryone.com/" mce_href="http://betterthaneveryone.com/"&gt;betterthaneveryone.com&lt;/A&gt; and can be emailed at &lt;A href="mailto:crutkas@microsoft.com" mce_href="mailto:crutkas@microsoft.com"&gt;crutkas@microsoft.com&lt;/A&gt; if you have any question.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7163484" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/media/default.aspx">media</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/mash+up/default.aspx">mash up</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web+miscellaneous/default.aspx">web miscellaneous</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/puzzle/default.aspx">puzzle</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item></channel></rss>