<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Applications</title>
        <link>http://geekswithblogs.net/TechTwaddle/category/10733.aspx</link>
        <description>Applications</description>
        <language>en-US</language>
        <copyright>TechTwaddle</copyright>
        <managingEditor>p.kumar999@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Windows Phone 7: Translation, rotation, scaling and the effect of &amp;lsquo;BitmapCache&amp;rsquo; on performance</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of.aspx</link>
            <description>&lt;p&gt;I just published a post on my blog at &lt;a href="http://www.techtwaddle.net"&gt;http://www.techtwaddle.net&lt;/a&gt;, here is the link:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://techtwaddle.net/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of-bitmapcache-on-performance/" href="http://techtwaddle.net/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of-bitmapcache-on-performance/"&gt;http://techtwaddle.net/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of-bitmapcache-on-performance/&lt;/a&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/144277.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of.aspx</guid>
            <pubDate>Wed, 09 Mar 2011 21:08:11 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/144277.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2011/03/10/windows-phone-7-translation-rotation-scaling-and-the-effect-of.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/144277.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/144277.aspx</trackback:ping>
        </item>
        <item>
            <title>Windows Phone 7: Building a simple dictionary web client</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/12/30/windows-phone-7-building-a-simple-dictionary-web-client.aspx</link>
            <description>&lt;p align="justify"&gt;Like I mentioned in &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/11/29/dictionary-web-service.aspx"&gt;this post&lt;/a&gt; a while back, I came across a dictionary web service called &lt;a href="http://www.aonaware.com/index.htm"&gt;Aonaware&lt;/a&gt; that serves up word definitions from various dictionaries and is really easy to use. The services page on their website, &lt;a title="http://services.aonaware.com/DictService/DictService.asmx" href="http://services.aonaware.com/DictService/DictService.asmx"&gt;http://services.aonaware.com/DictService/DictService.asmx&lt;/a&gt;, lists all the operations that are supported by the dictionary service. Here they are,&lt;/p&gt;
&lt;p&gt;&lt;font color="#400000"&gt;Word Dictionary Web Service&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#400000"&gt;The following operations are supported. For a formal definition, please review the &lt;/font&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?WSDL"&gt;&lt;font color="#400000"&gt;Service Description&lt;/font&gt;&lt;/a&gt;&lt;font color="#400000"&gt;. &lt;/font&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=Define"&gt;Define&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Define given word, returning definitions from all dictionaries &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=DefineInDict"&gt;DefineInDict&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Define given word, returning definitions from specified dictionary &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=DictionaryInfo"&gt;DictionaryInfo&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Show information about the specified dictionary &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=DictionaryList"&gt;DictionaryList&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Returns a list of available dictionaries &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=DictionaryListExtended"&gt;DictionaryListExtended&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Returns a list of advanced dictionaries (e.g. translating dictionaries) &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=Match"&gt;Match&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Look for matching words in all dictionaries using the given strategy &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=MatchInDict"&gt;MatchInDict&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Look for matching words in the specified dictionary using the given strategy &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=ServerInfo"&gt;ServerInfo&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Show remote server information &lt;/font&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://services.aonaware.com/DictService/DictService.asmx?op=StrategyList"&gt;StrategyList&lt;/a&gt;      &lt;br /&gt;
    &lt;font color="#400000"&gt;Return list of all available strategies on the server &lt;/font&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;Follow the links above to get more information on each API.&lt;/p&gt;
&lt;p align="justify"&gt;In this post we will be building a simple windows phone 7 client which uses this service to get word definitions for words entered by the user. The application will also allow the user to select a dictionary from all the available ones and look up the word definition in that dictionary. So of all the apis above we will be using only two, &lt;em&gt;DictionaryList()&lt;/em&gt; to get a list of all supported dictionaries and &lt;em&gt;DefineInDict()&lt;/em&gt; to get the word definition from a particular dictionary.&lt;/p&gt;
&lt;p align="justify"&gt;Before we get started, a note to you all; I would have liked to implement this application using concepts from data binding, item templates, data templates etc. I have a basic understanding of what they are but, being a beginner, I am not very comfortable with those topics yet so I didn’t use them. I thought I’ll get this version out of the way and maybe in the next version I could give those a try.&lt;/p&gt;
&lt;p align="justify"&gt;A somewhat scary mock-up of the what the final application will look like,&lt;/p&gt;
&lt;p&gt;&lt;img height="409" border="0" width="240" style="border: 0px none ; display: inline;" title="AppMockup" alt="AppMockup" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7Buildingasimpledictionarywe_13E86/AppMockup_f158c577-3ef8-47bb-8582-0ca83463d2b5.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;em&gt;Select Dictionary &lt;/em&gt;is a list picker control from the &lt;a href="http://silverlight.codeplex.com/"&gt;silverlight toolkit&lt;/a&gt; (you need to &lt;a href="http://silverlight.codeplex.com/releases/view/55034"&gt;download and install&lt;/a&gt; the toolkit if you haven’t already). Below it is a textbox where the user can enter words to look up and a button beside it to fetch the word definition when clicked. Finally we have a textblock which occupies the remaining area and displays the word definition from the selected dictionary.&lt;/p&gt;
&lt;p align="justify"&gt;Create a silverlight application for windows phone 7, AonawareDictionaryClient, and add references to the silverlight toolkit and the web service. From the solution explorer right on &lt;em&gt;References&lt;/em&gt; and select &lt;em&gt;Microsoft.Phone.Controls.Toolkit&lt;/em&gt; from under the .NET tab,&lt;/p&gt;
&lt;p&gt;&lt;img height="410" border="0" width="486" style="border: 0px none ; display: inline;" title="adding-reference-sl-toolkit" alt="adding-reference-sl-toolkit" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7Buildingasimpledictionarywe_13E86/adding-reference-sl-toolkit.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Next, add a reference to the web service. Again right click on &lt;em&gt;References&lt;/em&gt; and this time select &lt;em&gt;Add Service Reference&lt;/em&gt; In the resulting dialog paste the service url in the Address field and press go, (url –&amp;gt; &lt;a title="http://services.aonaware.com/DictService/DictService.asmx" href="http://services.aonaware.com/DictService/DictService.asmx"&gt;http://services.aonaware.com/DictService/DictService.asmx&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;&lt;img height="512" border="0" width="635" style="border: 0px none ; display: inline;" title="AddingReference" alt="AddingReference" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7Buildingasimpledictionarywe_13E86/AddingReference.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;once the service is discovered, provide a name for the NameSpace, in this case I’ve called it &lt;em&gt;AonawareDictionaryService&lt;/em&gt;. Press OK. You can now use the classes and functions that are generated in the &lt;em&gt;AonawareDictionaryClient.AonawareDictionaryService&lt;/em&gt; namespace.&lt;/p&gt;
&lt;p align="justify"&gt;Let’s get the UI done now. In &lt;em&gt;MainPage.xaml&lt;/em&gt; add a namespace declaration to use the toolkit controls,&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;the content of &lt;em&gt;LayoutRoot&lt;/em&gt; is changed as follows, (sorry, no syntax highlighting in this post)&lt;/p&gt;
&lt;p&gt;&lt;font color="#400000"&gt;&lt;font face="Arial" size="2"&gt;&amp;lt;StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,5,0,5"&amp;gt;       &lt;br /&gt;
    &amp;lt;TextBlock x:Name="ApplicationTitle" Text="AONAWARE DICTIONARY CLIENT" Style="{StaticResource PhoneTextNormalStyle}"/&amp;gt;        &lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Arial"&gt;&lt;font color="#008000"&gt;    &amp;lt;!--&amp;lt;TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/&amp;gt;--&amp;gt;           &lt;br /&gt;
&lt;/font&gt;&amp;lt;/StackPanel&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#400000"&gt;&lt;font size="2"&gt;&lt;font face="Arial"&gt;&lt;font color="#008000"&gt;&amp;lt;!--ContentPanel - place additional content here--&amp;gt;           &lt;br /&gt;
&lt;/font&gt;&amp;lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&amp;gt;          &lt;br /&gt;
    &amp;lt;Grid.RowDefinitions&amp;gt;          &lt;br /&gt;
        &amp;lt;RowDefinition Height="Auto"/&amp;gt;          &lt;br /&gt;
        &amp;lt;RowDefinition Height="Auto"/&amp;gt;          &lt;br /&gt;
        &amp;lt;RowDefinition Height="*"/&amp;gt;          &lt;br /&gt;
    &amp;lt;/Grid.RowDefinitions&amp;gt;          &lt;br /&gt;
    &amp;lt;toolkit:ListPicker Grid.Row="0" x:Name="listPickerDictionaryList"          &lt;br /&gt;
                        Header="Select Dictionary :"&amp;gt;          &lt;br /&gt;
    &amp;lt;/toolkit:ListPicker&amp;gt; &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;    &amp;lt;Grid Grid.Row="1" Margin="0,5,0,0"&amp;gt;     &lt;br /&gt;
        &amp;lt;Grid.ColumnDefinitions&amp;gt;      &lt;br /&gt;
            &amp;lt;ColumnDefinition Width="*"/&amp;gt;      &lt;br /&gt;
            &amp;lt;ColumnDefinition Width="Auto" /&amp;gt;      &lt;br /&gt;
        &amp;lt;/Grid.ColumnDefinitions&amp;gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;        &amp;lt;TextBox x:Name="txtboxInputWord" Grid.Column="0" GotFocus="OnTextboxInputWordGotFocus" /&amp;gt;     &lt;br /&gt;
        &amp;lt;Button x:Name="btnGo" Grid.Column="1" Click="OnButtonGoClick" &amp;gt;      &lt;br /&gt;
            &amp;lt;Button.Content&amp;gt;      &lt;br /&gt;
                &amp;lt;Image Source="/images/button-go.png"/&amp;gt;      &lt;br /&gt;
            &amp;lt;/Button.Content&amp;gt;      &lt;br /&gt;
        &amp;lt;/Button&amp;gt;      &lt;br /&gt;
    &amp;lt;/Grid&amp;gt; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;    &amp;lt;ScrollViewer Grid.Row="2" x:Name="scrollViewer"&amp;gt;     &lt;br /&gt;
        &amp;lt;TextBlock  Margin="12,5,12,5"  x:Name="txtBlockWordMeaning" HorizontalAlignment="Stretch"      &lt;br /&gt;
                   VerticalAlignment="Stretch" TextWrapping="Wrap"       &lt;br /&gt;
                   FontSize="26" /&amp;gt;      &lt;br /&gt;
    &amp;lt;/ScrollViewer&amp;gt;      &lt;br /&gt;
&amp;lt;/Grid&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;I have commented out the &lt;em&gt;PageTitle &lt;/em&gt;as it occupies too much valuable space, and the &lt;em&gt;ContentPanel&lt;/em&gt; is changed to contain three rows. First row contains the list picker control, second row contains the textbox and the button, and the third row contains a textblock within a scroll viewer.&lt;/p&gt;
&lt;p align="justify"&gt;The designer will now be showing the final ui,&lt;/p&gt;
&lt;p&gt;&lt;img height="484" border="0" width="273" style="border: 0px none ; display: inline;" title="designerViewOfUI" alt="designerViewOfUI" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7Buildingasimpledictionarywe_13E86/designerViewOfUI.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Now go to &lt;em&gt;MainPage.xaml.cs&lt;/em&gt;, and add the following namespace declarations,&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;using Microsoft.Phone.Controls;     &lt;br /&gt;
using AonawareDictionaryClient.AonawareDictionaryService;      &lt;br /&gt;
using System.IO.IsolatedStorage;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;A class called &lt;em&gt;DictServiceSoapClient&lt;/em&gt; would have been created for you in the background when you added a reference to the web service. This class functions as a wrapper to the services exported by the web service. All the web service functions that we saw at the start can be access through this class, or more precisely through an object of this class.&lt;/p&gt;
&lt;p align="justify"&gt;Create a data member of type &lt;em&gt;DictServiceSoapClient&lt;/em&gt; in the &lt;em&gt;Mainpage&lt;/em&gt; class, and a function which initializes it,&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;DictServiceSoapClient DictSvcClient = null; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;private DictServiceSoapClient GetDictServiceSoapClient()     &lt;br /&gt;
{      &lt;br /&gt;
    if (null == DictSvcClient)      &lt;br /&gt;
    {      &lt;br /&gt;
        DictSvcClient = new DictServiceSoapClient();      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;    return DictSvcClient;     &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;We have two major tasks remaining. First, when the application loads we need to populate the list picker with all the supported dictionaries and second, when the user enters a word and clicks on the arrow button we need to fetch the word’s meaning.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;br /&gt;
Populating the List Picker&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;In the &lt;em&gt;OnNavigatingTo&lt;/em&gt; event of the &lt;em&gt;MainPage&lt;/em&gt;, we call the &lt;em&gt;DictionaryList()&lt;/em&gt; api. This can also be done in the &lt;em&gt;OnLoading&lt;/em&gt; event handler of the &lt;em&gt;MainPage&lt;/em&gt;; not sure if one has an advantage over the other. Here’s the code for &lt;em&gt;OnNavigatedTo&lt;/em&gt;,&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    DictServiceSoapClient client = GetDictServiceSoapClient(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    client.DictionaryListCompleted += new EventHandler&amp;lt;DictionaryListCompletedEventArgs&amp;gt;(OnGetDictionaryListCompleted); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    client.DictionaryListAsync(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    base.OnNavigatedTo(e);     &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Windows Phone 7 supports only async calls to web services. When we added a reference to the dictionary service, asynchronous versions of all the functions were generated automatically. So in the above function we register a handler to the &lt;em&gt;DictionaryListCompleted&lt;/em&gt; event which will occur when the call to &lt;em&gt;DictionaryList()&lt;/em&gt; gets a response from the server. Then we call the &lt;em&gt;DictionaryListAsync()&lt;/em&gt; function which is the async version of the &lt;em&gt;DictionaryList()&lt;/em&gt; api. The result of this api will be sent to the handler &lt;em&gt;OnGetDictionaryListCompleted()&lt;/em&gt;,&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;void OnGetDictionaryListCompleted(object sender, DictionaryListCompletedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;      &lt;br /&gt;
    Dictionary[] listOfDictionaries; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    if (e.Error == null)     &lt;br /&gt;
    {      &lt;br /&gt;
        listOfDictionaries = e.Result;      &lt;br /&gt;
        PopulateListPicker(listOfDictionaries, settings);      &lt;br /&gt;
    }      &lt;br /&gt;
    else if (settings.Contains("SavedDictionaryList"))      &lt;br /&gt;
    {      &lt;br /&gt;
        listOfDictionaries = settings["SavedDictionaryList"] as Dictionary[];      &lt;br /&gt;
        PopulateListPicker(listOfDictionaries, settings);      &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        MessageBoxResult res = MessageBox.Show("An error occured while retrieving dictionary list, do you want to try again?", "Error", MessageBoxButton.OKCancel); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;        if (MessageBoxResult.OK == res)     &lt;br /&gt;
        {      &lt;br /&gt;
            GetDictServiceSoapClient().DictionaryListAsync();      &lt;br /&gt;
        }      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    settings.Save();     &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;I have used &lt;em&gt;IsolatedStorageSettings&lt;/em&gt; to store a few things; the entire dictionary list and the dictionary that is selected when the user exits the application, so that the next time when the user starts the application the current dictionary is set to the last selected value. First we check if the api returned any error, if the error object is null &lt;em&gt;e.Result&lt;/em&gt; will contain the list (actually array) of &lt;em&gt;Dictionary&lt;/em&gt; type objects. If there was an error, we check the isolated storage settings to see if there is a dictionary list stored from a previous instance of the application and if so, we populate the list picker based on this saved list. Note that in this case there are chances that the dictionary list might be out of date if there have been changes on the server. Finally, if none of these cases are true, we display an error message to the user and try to fetch the list again.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;em&gt;PopulateListPicker()&lt;/em&gt; is passed the array of &lt;em&gt;Dictionary&lt;/em&gt; objects and the settings object as well,&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;void PopulateListPicker(Dictionary[] listOfDictionaries, IsolatedStorageSettings settings)     &lt;br /&gt;
{      &lt;br /&gt;
    listPickerDictionaryList.Items.Clear(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    foreach (Dictionary dictionary in listOfDictionaries)     &lt;br /&gt;
    {      &lt;br /&gt;
        listPickerDictionaryList.Items.Add(dictionary.Name);      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    settings["SavedDictionaryList"] = listOfDictionaries; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    string savedDictionaryName;     &lt;br /&gt;
    if (settings.Contains("SavedDictionary"))      &lt;br /&gt;
    {      &lt;br /&gt;
        savedDictionaryName = settings["SavedDictionary"] as string;      &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        savedDictionaryName = "WordNet (r) 2.0"; //default dictionary, wordnet      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    foreach (string dictName in listPickerDictionaryList.Items)     &lt;br /&gt;
    {      &lt;br /&gt;
        if (dictName == savedDictionaryName)      &lt;br /&gt;
        {      &lt;br /&gt;
            listPickerDictionaryList.SelectedItem = dictName;      &lt;br /&gt;
            break;      &lt;br /&gt;
        }      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;&lt;font face="arial" color="#400000"&gt;    settings["SavedDictionary"] = listPickerDictionaryList.SelectedItem as string;       &lt;br /&gt;
} &lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;We first clear all the items from the list picker, add the dictionary names from the array and then create a key in the settings called &lt;em&gt;SavedDictionaryList&lt;/em&gt; and store the dictionary list in it. We then check if there is saved dictionary available from a previous instance, if there is, we set it as the selected item in the list picker. And if not, we set “WordNet ® 2.0” as the default dictionary. Before returning, we save the selected dictionary in the “&lt;em&gt;SavedDictionary&lt;/em&gt;” key of the isolated storage settings.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Fetching word definitions&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Getting this part done is very similar to the above code. We get the input word from the textbox, call into &lt;em&gt;DefineInDictAsync()&lt;/em&gt; to fetch the definition and when &lt;em&gt;DefineInDictAsync&lt;/em&gt; completes, we get the result and display it in the textblock. Here is the handler for the button click,&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;private void OnButtonGoClick(object sender, RoutedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    txtBlockWordMeaning.Text = "Please wait.."; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    if (txtboxInputWord.Text.Trim().Length &amp;lt;= 0)     &lt;br /&gt;
    {      &lt;br /&gt;
        MessageBox.Show("Please enter a word in the textbox and press 'Go'");      &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        Dictionary[] listOfDictionaries = settings["SavedDictionaryList"] as Dictionary[];      &lt;br /&gt;
        string selectedDictionary = listPickerDictionaryList.SelectedItem.ToString(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;        string dictId = "wn"; //default dictionary is wordnet (wn is the dict id) &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;        foreach (Dictionary dict in listOfDictionaries)     &lt;br /&gt;
        {      &lt;br /&gt;
            if (dict.Name == selectedDictionary)      &lt;br /&gt;
            {      &lt;br /&gt;
                dictId = dict.Id;      &lt;br /&gt;
                break;      &lt;br /&gt;
            }      &lt;br /&gt;
        } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;        DictServiceSoapClient client = GetDictServiceSoapClient(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;        client.DefineInDictCompleted += new EventHandler&amp;lt;DefineInDictCompletedEventArgs&amp;gt;(OnDefineInDictCompleted);     &lt;br /&gt;
        client.DefineInDictAsync(dictId, txtboxInputWord.Text.Trim());      &lt;br /&gt;
    }      &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;We validate the input and then select the dictionary id based on the currently selected dictionary. We need the dictionary id because the api &lt;em&gt;DefineInDict()&lt;/em&gt; expects the dictionary identifier and not the dictionary name. We could very well have stored the dictionary id in isolated storage settings too. Again, same as before, we register a event handler for the &lt;em&gt;DefineInDictCompleted&lt;/em&gt; event and call the &lt;em&gt;DefineInDictAsync()&lt;/em&gt; method passing in the dictionary id and the input word.&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;void OnDefineInDictCompleted(object sender, DefineInDictCompletedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    WordDefinition wd = e.Result; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;    scrollViewer.ScrollToVerticalOffset(0.0f); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Arial" color="#400000" size="2"&gt;    if (wd == null || e.Error != null || wd.Definitions.Length == 0)     &lt;br /&gt;
    {      &lt;br /&gt;
        txtBlockWordMeaning.Text = String.Format("No definitions were found for '{0}' in '{1}'", txtboxInputWord.Text.Trim(), listPickerDictionaryList.SelectedItem.ToString().Trim());      &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        foreach (Definition def in wd.Definitions)      &lt;br /&gt;
        {      &lt;br /&gt;
            string str = def.WordDefinition;      &lt;br /&gt;
            str = str.Replace("  ", " "); //some formatting      &lt;br /&gt;
            txtBlockWordMeaning.Text = str;      &lt;br /&gt;
        }      &lt;br /&gt;
    }      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;When the api completes, &lt;em&gt;e.Result&lt;/em&gt; will contain a &lt;em&gt;WordDefnition&lt;/em&gt; object. This class is also generated in the background while adding the service reference. We check the word definitions within this class to see if any results were returned, if not, we display a message to the user in the textblock. If a definition was found the text on the textblock is set to display the definition of the word.&lt;/p&gt;
&lt;p align="justify"&gt;Adding final touches, we now need to save the current dictionary when the application exits. A small but useful thing is selecting the entire word in the input textbox when the user selects it. This makes sure that if the user has looked up a definition for a really long word, he doesn’t have to press ‘&lt;em&gt;clear&lt;/em&gt;’ too many times to enter the next word,&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    settings["SavedDictionary"] = listPickerDictionaryList.SelectedItem as string; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    settings.Save(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    base.OnNavigatingFrom(e);     &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;private void OnTextboxInputWordGotFocus(object sender, RoutedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    TextBox txtbox = sender as TextBox; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="arial" color="#400000" size="2"&gt;    if (txtbox.Text.Trim().Length &amp;gt; 0)     &lt;br /&gt;
    {      &lt;br /&gt;
        txtbox.SelectionStart = 0;      &lt;br /&gt;
        txtbox.SelectionLength = txtbox.Text.Length;      &lt;br /&gt;
    }      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;em&gt;OnNavigatingFrom()&lt;/em&gt; is called whenever you navigate away from the &lt;em&gt;MainPage&lt;/em&gt;, since our application contains only one page that would mean that it is exiting (or getting tombstoned).&lt;/p&gt;
&lt;p align="justify"&gt;I leave you with a short video of the application in action, but before that if you have any suggestions on how to make the code better and improve it please do leave a comment.&lt;/p&gt;
&lt;p&gt;&lt;object height="385" width="480"&gt;
&lt;param value="http://www.youtube.com/v/cqruDwzO_90?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" name="movie" /&gt;
&lt;param value="true" name="allowFullScreen" /&gt;
&lt;param value="always" name="allowscriptaccess" /&gt;&lt;embed height="385" width="480" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/cqruDwzO_90?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Until next time…&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/143273.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/12/30/windows-phone-7-building-a-simple-dictionary-web-client.aspx</guid>
            <pubDate>Wed, 29 Dec 2010 19:36:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/143273.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/12/30/windows-phone-7-building-a-simple-dictionary-web-client.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/143273.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/143273.aspx</trackback:ping>
        </item>
        <item>
            <title>Windows Phone 7 : Dragging and flicking UI controls</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/12/19/windows-phone-7--dragging-and-flicking-ui-controls.aspx</link>
            <description>&lt;p align="justify"&gt;Who would want to flick and drag UI controls!? There might not be many use cases but I think some concepts here are worthy of a post.&lt;/p&gt;
&lt;p align="justify"&gt;So we will create a simple silverlight application for windows phone 7, containing a canvas element on which we’ll place a button control and an image and then, as the title says, drag and flick the controls. Here’s Mainpage.xaml,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;LayoutRoot&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;Transparent&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;Auto&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;*&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid.RowDefinitions&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;TitlePanel contains the name of the application and page title&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;TitlePanel&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&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; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;12,17,0,28&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;TextBlock&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;ApplicationTitle&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Text&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;KINETICS&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Style&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"{&lt;span style="color: rgb(0, 0, 255);"&gt;StaticResource PhoneTextNormalStyle}&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;TextBlock&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;PageTitle&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Text&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;drag and flick&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;9,-7,0,0&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Style&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"{&lt;span style="color: rgb(0, 0, 255);"&gt;StaticResource PhoneTextTitle1Style}&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;StackPanel&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;ContentPanel - place additional content here&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;ContentPanel&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;1&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Canvas&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &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;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;MainCanvas&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;HorizontalAlignment&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;Stretch&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;VerticalAlignment&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;Stretch&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Canvas.Background&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;LinearGradientBrush&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;StartPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;0 0&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;EndPoint&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;0 1&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;GradientStop&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Offset&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&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; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Color&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;Black&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;GradientStop&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Offset&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;1.5&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt; &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;Color&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;BlanchedAlmond&lt;/span&gt;"&lt;span style="color: rgb(0, 0, 255);"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;LinearGradientBrush&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Canvas.Background&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Canvas&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;  &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Grid&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p align="justify"&gt;the second row in the main grid contains a canvas element, &lt;font color="#000080"&gt;MainCanvas&lt;/font&gt;, with its horizontal and vertical alignment set to stretch so that it occupies the entire grid. The canvas background is a linear gradient brush starting with Black and ending with BlanchedAlmond. We’ll add the button and image control to this canvas at run time.&lt;/p&gt;
&lt;p align="justify"&gt;Moving to Mainpage.xaml.cs the &lt;font color="#000080"&gt;Mainpage&lt;/font&gt; class contains the following members,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;partial&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MainPage&lt;/span&gt; : PhoneApplicationPage&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Button&lt;/span&gt; FlickButton;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Image&lt;/span&gt; FlickImage;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;FrameworkElement&lt;/span&gt; ElemToMove = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; ElemVelX, ElemVelY;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;const&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; SPEED_FACTOR = 60;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;DispatcherTimer&lt;/span&gt; timer;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;FlickButton&lt;/font&gt; and &lt;font color="#000080"&gt;FlickImage&lt;/font&gt; are the controls that we’ll add to the canvas. &lt;font color="#000080"&gt;ElemToMove&lt;/font&gt;, &lt;font color="#000080"&gt;ElemVelX&lt;/font&gt; and &lt;font color="#000080"&gt;ElemVelY&lt;/font&gt; will be used by the timer callback to move the ui control. &lt;font color="#000080"&gt;SPEED_FACTOR&lt;/font&gt; is used to scale the velocities of ui controls.&lt;/p&gt;
&lt;p&gt;Here’s the &lt;font color="#000080"&gt;Mainpage&lt;/font&gt; constructor,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// Constructor&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; MainPage()&lt;/li&gt;
    &lt;li&gt;{&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    InitializeComponent();&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    AddButtonToCanvas();&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    AddImageToCanvas();&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    timer = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;DispatcherTimer&lt;/span&gt;();&lt;/li&gt;
    &lt;li&gt;    timer.Interval = &lt;span style="color: rgb(43, 145, 175);"&gt;TimeSpan&lt;/span&gt;.FromMilliseconds(35);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    timer.Tick += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;(OnTimerTick);&lt;/li&gt;
    &lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We’ll look at those &lt;font color="#000080"&gt;AddButton&lt;/font&gt; and &lt;font color="#000080"&gt;AddImage&lt;/font&gt; functions in a moment. The constructor initializes a timer which fires every 35 milliseconds, this timer will be started after the flick gesture completes with some inertia.&lt;/p&gt;
&lt;p align="justify"&gt;Back to &lt;font color="#000080"&gt;AddButton&lt;/font&gt; and &lt;font color="#000080"&gt;AddImage&lt;/font&gt; functions,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; AddButtonToCanvas()&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;LinearGradientBrush&lt;/span&gt; brush;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;GradientStop&lt;/span&gt; stop1, stop2;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Random&lt;/span&gt; rand = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Random&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.Millisecond);&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickButton = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Button&lt;/span&gt;();&lt;/li&gt;
    &lt;li&gt;    FlickButton.Content = &lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickButton.Width = 100;&lt;/li&gt;
    &lt;li&gt;    FlickButton.Height = 100;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    brush = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;LinearGradientBrush&lt;/span&gt;();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    brush.StartPoint = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Point&lt;/span&gt;(0, 0);&lt;/li&gt;
    &lt;li&gt;    brush.EndPoint = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Point&lt;/span&gt;(0, 1);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    stop1 = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;GradientStop&lt;/span&gt;();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    stop1.Offset = 0;&lt;/li&gt;
    &lt;li&gt;    stop1.Color = Colors.White;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    stop2 = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;GradientStop&lt;/span&gt;();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    stop2.Offset = 1;&lt;/li&gt;
    &lt;li&gt;    stop2.Color = (&lt;span style="color: rgb(43, 145, 175);"&gt;Application&lt;/span&gt;.Current.Resources[&lt;span style="color: rgb(163, 21, 21);"&gt;"PhoneAccentBrush"&lt;/span&gt;] &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; SolidColorBrush).Color;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    brush.GradientStops.Add(stop1);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    brush.GradientStops.Add(stop2);&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickButton.Background = brush;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetTop(FlickButton, rand.Next(0, 400));&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetLeft(FlickButton, rand.Next(0, 200));&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    MainCanvas.Children.Add(FlickButton);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;//subscribe to events&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickButton.ManipulationDelta += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ManipulationDeltaEventArgs&lt;/span&gt;&amp;gt;(OnManipulationDelta);&lt;/li&gt;
    &lt;li&gt;    FlickButton.ManipulationCompleted += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;&amp;lt;ManipulationCompletedEventArgs&amp;gt;(OnManipulationCompleted);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p align="justify"&gt;this function is basically glorifying a simple task. After creating the button and setting its height and width, its background is set to a linear gradient brush. The direction of the gradient is from top towards bottom and notice that the second stop color is the &lt;font color="#000080"&gt;&lt;em&gt;PhoneAccentColor&lt;/em&gt;&lt;/font&gt;, which changes along with the theme of the device. The line,&lt;/p&gt;
&lt;p align="justify"&gt;    stop2.Color = (&lt;span style="color: rgb(43, 145, 175);"&gt;Application&lt;/span&gt;.Current.Resources[&lt;span style="color: rgb(163, 21, 21);"&gt;"PhoneAccentBrush"&lt;/span&gt;] &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; SolidColorBrush).Color;&lt;/p&gt;
&lt;p align="justify"&gt;does the magic of extracting the &lt;font color="#000080"&gt;PhoneAccentBrush&lt;/font&gt; from application’s resources, getting its color and assigning it to the gradient stop.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;AddImage&lt;/font&gt; function is straight forward in comparison,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; AddImageToCanvas()&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Random&lt;/span&gt; rand = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Random&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.Millisecond);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    FlickImage = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Image&lt;/span&gt;();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickImage.Source = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; BitmapImage(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Uri&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"/images/Marble.png"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;UriKind&lt;/span&gt;.Relative));&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetTop(FlickImage, rand.Next(0, 400));&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetLeft(FlickImage, rand.Next(0, 200));&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    MainCanvas.Children.Add(FlickImage);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;//subscribe to events&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    FlickImage.ManipulationDelta += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ManipulationDeltaEventArgs&lt;/span&gt;&amp;gt;(OnManipulationDelta);&lt;/li&gt;
    &lt;li&gt;    FlickImage.ManipulationCompleted += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;&amp;lt;ManipulationCompletedEventArgs&amp;gt;(OnManipulationCompleted);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p align="justify"&gt;The &lt;font color="#000080"&gt;ManipulationDelta&lt;/font&gt; and &lt;font color="#000080"&gt;ManipulationCompleted&lt;/font&gt; handlers are same for both the button and the image.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;OnManipulationDelta()&lt;/font&gt; should look familiar, a similar implementation was used in the &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/11/22/windows-phone-7--ui-control-boundaries.aspx"&gt;previous post&lt;/a&gt;,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnManipulationDelta(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;ManipulationDeltaEventArgs&lt;/span&gt; args)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;FrameworkElement&lt;/span&gt; Elem = sender &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;FrameworkElement&lt;/span&gt;;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; Left = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetLeft(Elem);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; Top = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(Elem);&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    Left += args.DeltaManipulation.Translation.X;&lt;/li&gt;
    &lt;li&gt;    Top += args.DeltaManipulation.Translation.Y;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;//check for bounds&lt;/span&gt;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Left &amp;lt; 0)&lt;/li&gt;
    &lt;li&gt;    {&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        Left = 0;&lt;/li&gt;
    &lt;li&gt;    }&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&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; (Left &amp;gt; (MainCanvas.ActualWidth - Elem.ActualWidth))&lt;/li&gt;
    &lt;li&gt;    {&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        Left = MainCanvas.ActualWidth - Elem.ActualWidth;&lt;/li&gt;
    &lt;li&gt;    }&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Top &amp;lt; 0)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    {&lt;/li&gt;
    &lt;li&gt;        Top = 0;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    }&lt;/li&gt;
    &lt;li&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; (Top &amp;gt; (MainCanvas.ActualHeight - Elem.ActualHeight))&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    {&lt;/li&gt;
    &lt;li&gt;        Top = MainCanvas.ActualHeight - Elem.ActualHeight;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    }&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetLeft(Elem, Left);&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetTop(Elem, Top);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;all it does is calculate the control’s position, check for bounds and then set the top and left of the control.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;OnManipulationCompleted()&lt;/font&gt; is more interesting because here we need to check if the gesture completed with any inertia and if it did, start the timer and continue to move the ui control until it comes to a halt slowly,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnManipulationCompleted(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, ManipulationCompletedEventArgs args)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;FrameworkElement&lt;/span&gt; Elem = sender &lt;span style="color: rgb(0, 0, 255);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;FrameworkElement&lt;/span&gt;;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (args.IsInertial)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    {&lt;/li&gt;
    &lt;li&gt;        ElemToMove = Elem;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;        Debug.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"Linear VelX:{0:0.00}  VelY:{1:0.00}"&lt;/span&gt;, args.FinalVelocities.LinearVelocity.X,&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            args.FinalVelocities.LinearVelocity.Y);&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        ElemVelX = args.FinalVelocities.LinearVelocity.X / SPEED_FACTOR;&lt;/li&gt;
    &lt;li&gt;        ElemVelY = args.FinalVelocities.LinearVelocity.Y / SPEED_FACTOR;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;        timer.Start();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    }&lt;/li&gt;
    &lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;font color="#000080"&gt;ManipulationCompletedEventArgs&lt;/font&gt; contains a member, &lt;font color="#000080"&gt;IsInertial&lt;/font&gt;, which is set to true if the manipulation was completed with some inertia. &lt;font color="#000080"&gt;args.FinalVelocities.LinearVelocity.X&lt;/font&gt; and &lt;font color="#000080"&gt;.Y&lt;/font&gt; will contain the velocities along the X and Y axis. We need to scale down these values so they can be used to increment the ui control’s position sensibly. A reference to the ui control is stored in &lt;font color="#000080"&gt;ElemToMove&lt;/font&gt; and the velocities are stored as well, these will be used in the timer callback to access the ui control. And finally, we start the timer.&lt;/p&gt;
&lt;p align="justify"&gt;The timer callback function is as follows,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background: none repeat scroll 0% 0% rgb(221, 221, 221); overflow: auto;"&gt;
&lt;ol style="padding: 0px 0px 0px 5px; margin: 0px 0px 0px 2.5em; background: none repeat scroll 0% 0% rgb(255, 255, 255);"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnTimerTick(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt; != ElemToMove)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    {&lt;/li&gt;
    &lt;li&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; Left, Top;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        Left = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetLeft(ElemToMove);&lt;/li&gt;
    &lt;li&gt;        Top = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(ElemToMove);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;        Left += ElemVelX;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        Top += ElemVelY;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//check for bounds&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Left &amp;lt; 0)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        {&lt;/li&gt;
    &lt;li&gt;            Left = 0;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            ElemVelX *= -1;&lt;/li&gt;
    &lt;li&gt;        }&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&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; (Left &amp;gt; (MainCanvas.ActualWidth - ElemToMove.ActualWidth))&lt;/li&gt;
    &lt;li&gt;        {&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            Left = MainCanvas.ActualWidth - ElemToMove.ActualWidth;&lt;/li&gt;
    &lt;li&gt;            ElemVelX *= -1;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        }&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (Top &amp;lt; 0)&lt;/li&gt;
    &lt;li&gt;        {&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            Top = 0;&lt;/li&gt;
    &lt;li&gt;            ElemVelY *= -1;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        }&lt;/li&gt;
    &lt;li&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; (Top &amp;gt; (MainCanvas.ActualHeight - ElemToMove.ActualHeight))&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        {&lt;/li&gt;
    &lt;li&gt;            Top = MainCanvas.ActualHeight - ElemToMove.ActualHeight;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            ElemVelY *= -1;&lt;/li&gt;
    &lt;li&gt;        }&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt; &lt;/li&gt;
    &lt;li&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetLeft(ElemToMove, Left);&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetTop(ElemToMove, Top);&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//reduce x,y velocities gradually&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;        ElemVelX *= 0.9;&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        ElemVelY *= 0.9;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        &lt;span style="color: rgb(0, 128, 0);"&gt;//when velocities become too low, break&lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Math&lt;/span&gt;.Abs(ElemVelX) &amp;lt; 1.0 &amp;amp;&amp;amp; &lt;span style="color: rgb(43, 145, 175);"&gt;Math&lt;/span&gt;.Abs(ElemVelY) &amp;lt; 1.0)&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;        {&lt;/li&gt;
    &lt;li&gt;            timer.Stop();&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;            ElemToMove = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;/li&gt;
    &lt;li&gt;        }&lt;/li&gt;
    &lt;li style="background: none repeat scroll 0% 0% rgb(243, 243, 243);"&gt;    }&lt;/li&gt;
    &lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;if &lt;font color="#000080"&gt;ElemToMove&lt;/font&gt; is not null, we get the top and left values of the control and increment the values with their X and Y velocities. Check for bounds, and if the control goes out of bounds we reverse its velocity. Towards the end, the velocities are reduced by 10% every time the timer callback is called, and if the velocities reach too low values the timer is stopped and &lt;font color="#000080"&gt;ElemToMove&lt;/font&gt; is made null.&lt;/p&gt;
&lt;p align="justify"&gt;Here’s a short video of the program, the video is a little dodgy because my display driver &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/fa447c95-496e-431d-88a7-b0a76d177c92"&gt;refuses to run the animations smoothly&lt;/a&gt;. The flicks aren’t always recognised but the program should run well on an actual device (or a pc with better configuration),&lt;/p&gt;
&lt;object width="480" height="385"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/nlqULtosHlw?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowscriptaccess" value="always" /&gt;&lt;embed width="480" height="385" src="http://www.youtube.com/v/nlqULtosHlw?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt;You can download the source code from here: &lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/ButtonDragAndFlick.zip"&gt;ButtonDragAndFlick.zip&lt;/a&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/143174.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/12/19/windows-phone-7--dragging-and-flicking-ui-controls.aspx</guid>
            <pubDate>Sun, 19 Dec 2010 16:48:09 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/143174.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/12/19/windows-phone-7--dragging-and-flicking-ui-controls.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/143174.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/143174.aspx</trackback:ping>
        </item>
        <item>
            <title>Windows Phone 7 : UI Control Boundaries</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/11/22/windows-phone-7--ui-control-boundaries.aspx</link>
            <description>&lt;p align="justify"&gt;I was working on an application at work when I needed to figure out a really trivial case, well, trivial in theory at least. All I wanted to know was if a button was completely inside a rectangle. Could anything else be simpler you think. Just check the button bounds against the rectangle bounds,&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;pseudocode&lt;/strong&gt;    &lt;br /&gt;
&lt;font color="#800000"&gt;if (Button.Top &amp;gt; Rectangle.Top &amp;amp;&amp;amp;     &lt;br /&gt;
      Button.Left &amp;gt; Rectangle.Left &amp;amp;&amp;amp;      &lt;br /&gt;
      Button.Right &amp;lt; Rectangle.Right &amp;amp;&amp;amp;      &lt;br /&gt;
      Button.Bottom &amp;lt; Rectangle.Bottom)      &lt;br /&gt;
{      &lt;br /&gt;
    //Button is inside rectangle      &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;the Right and Bottom of the Button and Rectangle can be calculated using the Width and the Height properties of each. You’d expect this to work correctly, but as far as Windows Phone 7 is concerned there is only one caveat.&lt;/p&gt;
&lt;p align="justify"&gt;I wrote up a simple application to illustrate this. Create a Windows Phone 7 Silverlight application and add the following to the &lt;font color="#004080"&gt;ContentPanel&lt;/font&gt; grid in Mainpage.xaml,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background-color: rgb(221, 221, 221); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; overflow: auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;ol style="margin: 0px 0px 0px 2em; padding: 0px 0px 0px 5px; background-color: rgb(255, 255, 255); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
    &lt;li&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Grid&lt;/span&gt; x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"ContentPanel"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Grid&lt;/span&gt;.Row=&lt;span style="color: rgb(163, 21, 21);"&gt;"1"&lt;/span&gt;&amp;gt;&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    &amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt; x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"MainCanvas"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;HorizontalAlignment&lt;/span&gt;=&lt;span style="color: rgb(163, 21, 21);"&gt;"Stretch"&lt;/span&gt; VerticalAlignment=&lt;span style="color: rgb(163, 21, 21);"&gt;"Stretch"&lt;/span&gt;&amp;gt;&lt;/li&gt;
    &lt;li&gt;        &amp;lt;TextBlock x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"RectangleTop"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Top=&lt;span style="color: rgb(163, 21, 21);"&gt;"0"&lt;/span&gt; /&amp;gt;&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;        &amp;lt;TextBlock x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"ButtonTop"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Top=&lt;span style="color: rgb(163, 21, 21);"&gt;"30"&lt;/span&gt; /&amp;gt;&lt;/li&gt;
    &lt;li&gt;        &amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Rectangle&lt;/span&gt; x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"ColoredRectangle"&lt;/span&gt; Width=&lt;span style="color: rgb(163, 21, 21);"&gt;"480"&lt;/span&gt; Height=&lt;span style="color: rgb(163, 21, 21);"&gt;"150"&lt;/span&gt; Fill=&lt;span style="color: rgb(163, 21, 21);"&gt;"Coral"&lt;/span&gt; Opacity=&lt;span style="color: rgb(163, 21, 21);"&gt;"0.4"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Top=&lt;span style="color: rgb(163, 21, 21);"&gt;"150"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Left=&lt;span style="color: rgb(163, 21, 21);"&gt;"0"&lt;/span&gt;/&amp;gt;&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;        &amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Button&lt;/span&gt; x:Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"MyButton"&lt;/span&gt; Content=&lt;span style="color: rgb(163, 21, 21);"&gt;"Move Me"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Top=&lt;span style="color: rgb(163, 21, 21);"&gt;"400"&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.Left=&lt;span style="color: rgb(163, 21, 21);"&gt;"160"&lt;/span&gt; Background=&lt;span style="color: rgb(163, 21, 21);"&gt;"Purple"&lt;/span&gt;/&amp;gt;&lt;/li&gt;
    &lt;li&gt;    &amp;lt;/&lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;&amp;gt;           &lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&amp;lt;/&lt;span style="color: rgb(43, 145, 175);"&gt;Grid&lt;/span&gt;&amp;gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
the constructor of &lt;font color="#004080"&gt;Mainpage&lt;/font&gt; looks like this,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background-color: rgb(221, 221, 221); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; overflow: auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;ol style="margin: 0px 0px 0px 2em; padding: 0px 0px 0px 5px; background-color: rgb(255, 255, 255); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; MainPage()&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;{&lt;/li&gt;
    &lt;li&gt;    InitializeComponent();&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;/li&gt;
    &lt;li&gt;    MyButton.ManipulationDelta += &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ManipulationDeltaEventArgs&lt;/span&gt;&amp;gt;(OnMyButtonManipulationDelta);&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;/li&gt;
    &lt;li&gt;    RectangleTop.Text = &lt;span style="color: rgb(43, 145, 175);"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"Rectangle Top: {0}"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(ColoredRectangle));&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    ButtonTop.Text = &lt;span style="color: rgb(43, 145, 175);"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"Button Top: {0}"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(MyButton));&lt;/li&gt;
    &lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
We register for the &lt;font color="#004080"&gt;ManipulationDelta&lt;/font&gt; event on the button and update the Text on the two Textblocks to show the Top properties of the rectangle and the button, if you run the program this is how the application will look,&lt;/p&gt;
&lt;p&gt;&lt;img width="272" height="484" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7UIControlBoundaries_136CF/FirstScreen.jpg" alt="FirstScreen" title="FirstScreen" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;The &lt;font color="#004080"&gt;OnMyButtonManipulationDelta()&lt;/font&gt; function will be called whenever a &lt;font color="#004080"&gt;ManipulationDelta&lt;/font&gt; event occurs on the &lt;font color="#004080"&gt;MyButton &lt;/font&gt;control, which happens when the user clicks and drags the button around. Here we want to move the button along with the users drag,&lt;/p&gt;
&lt;div class="le-cah-container"&gt;
&lt;div style="background-color: rgb(221, 221, 221); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; overflow: auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;ol style="margin: 0px 0px 0px 2.5em; padding: 0px 0px 0px 5px; background-color: rgb(255, 255, 255); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
    &lt;li&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; OnMyButtonManipulationDelta(&lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;ManipulationDeltaEventArgs&lt;/span&gt; e)&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;{&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; deltaX = e.DeltaManipulation.Translation.X;&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; deltaY = e.DeltaManipulation.Translation.Y;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; btnX = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetLeft(MyButton);&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;double&lt;/span&gt; btnY = &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(MyButton);&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;/li&gt;
    &lt;li&gt;    btnX += deltaX;&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    btnY += deltaY;&lt;/li&gt;
    &lt;li&gt; &lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetLeft(MyButton, btnX);&lt;/li&gt;
    &lt;li&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.SetTop(MyButton, btnY);&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt; &lt;/li&gt;
    &lt;li&gt;    ButtonTop.Text = &lt;span style="color: rgb(43, 145, 175);"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"Button Top: {0}"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;Canvas&lt;/span&gt;.GetTop(MyButton));&lt;/li&gt;
    &lt;li style="background-color: rgb(243, 243, 243); background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 0%; -moz-background-size: auto auto; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;
In this function we get the delta manipulation values along the X and Y axis and add it to the Buttons Left and Top property, this makes sure that the button control moves along with the users finger when he/she clicks and drags the button.&lt;/p&gt;
&lt;p align="justify"&gt;Now when we move the button and place it inside the rectangle the values on the textblocks update,&lt;/p&gt;
&lt;p&gt;&lt;img width="272" height="484" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7UIControlBoundaries_136CF/ButtonInsideRect.jpg" alt="ButtonInsideRect" title="ButtonInsideRect" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;br /&gt;
Things are fine till here, but they start getting interesting when you move the button to the top of the rectangle,&lt;/p&gt;
&lt;p&gt;&lt;img width="272" height="484" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7UIControlBoundaries_136CF/ButtonTopLessThanRectTop.jpg" alt="ButtonTopLessThanRectTop" title="ButtonTopLessThanRectTop" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;br /&gt;
the Button control appears to be well inside the Rectangle but the Top property of the button is &lt;strong&gt;144&lt;/strong&gt; where as that of the Rectangle is &lt;strong&gt;150&lt;/strong&gt;. The values suggest that the button is outside the rectangle!&lt;/p&gt;
&lt;p align="justify"&gt;This happens because there is more to a button control than what meets the eyes. The button is not confined within the white rectangular boundary but is much bigger than that. When you drag and drop a button control using the visual editor in Visual Studio, the area of the button that is selected is bigger than the button itself, (see image below)&lt;/p&gt;
&lt;p&gt;&lt;img width="365" height="256" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7UIControlBoundaries_136CF/ButtonControlSelected.jpg" alt="ButtonControlSelected" title="ButtonControlSelected" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;br /&gt;
This is intentional and done to make sure that the target area for the user is a bit larger than the control itself so that the control can be selected easily without requiring too much precision. This, of course, is a nice feature (ask anyone who has tried using a Windows Mobile touch screen device with a finger ;)&lt;/p&gt;
&lt;p align="justify"&gt;Placing the Button control precisely on top of the rectangle like below,&lt;/p&gt;
&lt;p&gt;&lt;img width="272" height="484" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/WindowsPhone7UIControlBoundaries_136CF/ButtonOnTopOfRect_1.jpg" alt="ButtonOnTopOfRect" title="ButtonOnTopOfRect" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;reveals that there are &lt;strong&gt;12 extra pixels&lt;/strong&gt; on all sides of the button control. (&lt;strong&gt;rectangle top = 150,  button top = 138&lt;/strong&gt;)&lt;/p&gt;
&lt;p align="justify"&gt;So that is all in this post. In the next post we’ll see how easy it is to pick a button control and flick it, the button will bounce around the screen and come to a halt gradually. Refreshing old memories of marbles ;)&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Post attachments   &lt;br /&gt;
Source Code: &lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/ButtonBoundaries.zip"&gt;ButtonBoundaries.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;&lt;br /&gt;
I forgot to add that the same case is also true for textbox and password box control&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2&lt;/strong&gt;&lt;br /&gt;
I just recorded a short video of the application in action,&lt;/p&gt;
&lt;object width="480" height="385"&gt;
&lt;param value="http://www.youtube.com/v/11oCwcRBAoQ?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" name="movie" /&gt;
&lt;param value="true" name="allowFullScreen" /&gt;
&lt;param value="always" name="allowscriptaccess" /&gt;&lt;embed width="480" height="385" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/11oCwcRBAoQ?fs=1&amp;amp;hl=en_US&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/142831.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/11/22/windows-phone-7--ui-control-boundaries.aspx</guid>
            <pubDate>Mon, 22 Nov 2010 18:07:10 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/142831.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/11/22/windows-phone-7--ui-control-boundaries.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/142831.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/142831.aspx</trackback:ping>
        </item>
        <item>
            <title>Edit boxes and default text selection</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/08/20/edit-boxes-and-the-default-text-selection.aspx</link>
            <description>&lt;p align="justify"&gt;While working on an application of mine, I had to display ‘Help’ information. I chose to use a read-only multiline edit box along with &lt;a href="http://msdn.microsoft.com/en-us/library/aa922540.aspx"&gt;SetWindowText()&lt;/a&gt; to display the text, sounds simple enough. But I ran into a small but annoying problem. The text displayed was all selected, and I thought why would the text be selected by default!? &lt;/p&gt;  &lt;p align="justify"&gt;Here is a sample program to demonstrate this,&lt;/p&gt;  &lt;div class="le-cah-container"&gt;   &lt;div style="background: #ddd; overflow: auto"&gt;     &lt;ol style="padding-bottom: 0px; margin: 0px 0px 0px 3em; padding-left: 5px; padding-right: 0px; background: #ffffff; padding-top: 0px"&gt;       &lt;li&gt;&lt;span style="color: #0000ff"&gt;#include&lt;/span&gt; &lt;span style="color: #a31515"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #0000ff"&gt;#include&lt;/span&gt; &lt;span style="color: #a31515"&gt;&amp;lt;aygshell.h&amp;gt;&lt;/span&gt;&lt;/li&gt;        &lt;li&gt;&lt;span style="color: #0000ff"&gt;#include&lt;/span&gt; &lt;span style="color: #a31515"&gt;"resource.h"&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;HINSTANCE g_hInst;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;BOOL CALLBACK EditBoxSelTextDlgProc(HWND, UINT, WPARAM, LPARAM);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AlignComponents(HWND hDlg);&lt;/li&gt;        &lt;li&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; InitializeComponents(HWND hDlg);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            LPTSTR lpCmdLine, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; nCmdShow)&lt;/li&gt;        &lt;li&gt;{&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    g_hInst = hInst;&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    DialogBox(hInst, MAKEINTRESOURCE(IDD_PPC_EDITBOXTEXTSEL), NULL, EditBoxSelTextDlgProc);&lt;/li&gt;        &lt;li&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; 0;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;BOOL CALLBACK EditBoxSelTextDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam, LPARAM lParam)&lt;/li&gt;        &lt;li&gt;{&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; wmID, wmEvent;&lt;/li&gt;        &lt;li&gt;    PAINTSTRUCT ps;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    HDC hdc;&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;switch&lt;/span&gt;(uMessage)&lt;/li&gt;        &lt;li&gt;    {&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; WM_INITDIALOG:&lt;/li&gt;        &lt;li&gt;            {&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                SHINITDLGINFO shidi;&lt;/li&gt;        &lt;li&gt;                SHMENUBARINFO mbi;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;                memset(&amp;amp;shidi, 0, &lt;span style="color: #0000ff"&gt;sizeof&lt;/span&gt;(shidi));&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                memset(&amp;amp;mbi, 0, &lt;span style="color: #0000ff"&gt;sizeof&lt;/span&gt;(mbi));&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                shidi.dwMask = SHIDIM_FLAGS;&lt;/li&gt;        &lt;li&gt;                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                shidi.hDlg = hDlg;&lt;/li&gt;        &lt;li&gt;                SHInitDialog(&amp;amp;shidi);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;                mbi.cbSize = &lt;span style="color: #0000ff"&gt;sizeof&lt;/span&gt;(mbi);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                mbi.hwndParent = hDlg;&lt;/li&gt;        &lt;li&gt;                mbi.nToolBarId = IDR_MENU_EDITBOXTEXTSEL;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                mbi.hInstRes = g_hInst;&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(!SHCreateMenuBar(&amp;amp;mbi))&lt;/li&gt;        &lt;li&gt;                {&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                    printf(&lt;span style="color: #a31515"&gt;"Error creating menu bar, errcode:0x%x\n"&lt;/span&gt;, GetLastError());&lt;/li&gt;        &lt;li&gt;                }&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;                AlignComponents(hDlg);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;                InitializeComponents(hDlg);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt;        &lt;li&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; TRUE;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;        &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; WM_COMMAND:&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt;        &lt;li&gt;                wmID = LOWORD(wParam);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                wmEvent = HIWORD(wParam);&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color: #0000ff"&gt;switch&lt;/span&gt;(wmID)&lt;/li&gt;        &lt;li&gt;                {&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; IDM_EXIT:&lt;/li&gt;        &lt;li&gt;                        EndDialog(hDlg, uMessage);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;                        &lt;span style="color: #0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt;        &lt;li&gt;                }&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt;        &lt;li&gt;            &lt;span style="color: #0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;        &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; WM_PAINT:&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt;        &lt;li&gt;                hdc = BeginPaint(hDlg, &amp;amp;ps);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt; &lt;/li&gt;        &lt;li&gt;                EndPaint(hDlg, &amp;amp;ps);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt;        &lt;li&gt;            &lt;span style="color: #0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt;        &lt;li&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; FALSE;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AlignComponents(HWND hDlg)&lt;/li&gt;        &lt;li&gt;{&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    HWND hTemp = NULL;&lt;/li&gt;        &lt;li&gt;    RECT rect = {0, 0, 0, 0};&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; x=0, y=0, width=0, height=0;&lt;/li&gt;        &lt;li&gt;    &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; insetX = 3;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; insetY = 3;&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    GetClientRect(hDlg, &amp;amp;rect);&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    hTemp = GetDlgItem(hDlg, IDC_EDITBOX);&lt;/li&gt;        &lt;li&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (hTemp)&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt;        &lt;li&gt;        x = rect.left + insetX;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;        y = rect.top + insetY;&lt;/li&gt;        &lt;li&gt;        width = (rect.right - rect.left - 2*insetX);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;        height = (rect.bottom - rect.top - 2*insetY);&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;        MoveWindow(hTemp, x, y, width, height, FALSE);&lt;/li&gt;        &lt;li&gt;    }&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #0000ff"&gt;const&lt;/span&gt; TCHAR message[] = TEXT(&lt;span style="color: #a31515"&gt;"Lorem ipsum dolor sit amet, consectetur adipisicing elit, \&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris \&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;nisi ut aliquip ex ea commodo consequat.\&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;\r\n\r\n\&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum \&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non \&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\&lt;/span&gt;&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #a31515"&gt;"&lt;/span&gt;);&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; InitializeComponents(HWND hDlg)&lt;/li&gt;        &lt;li&gt;{&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    HWND hTemp = GetDlgItem(hDlg, IDC_EDITBOX);&lt;/li&gt;        &lt;li&gt; &lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (hTemp)&lt;/li&gt;        &lt;li&gt;    {&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;        SetWindowText(hTemp, message);&lt;/li&gt;        &lt;li&gt;    }&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt;     &lt;/ol&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p align="justify"&gt;This is usual stuff. Display a dialog box from &lt;font color="#000080"&gt;WinMain()&lt;/font&gt;, initialize the dialog and the menubar in dialog proc under the &lt;a href="http://msdn.microsoft.com/en-us/library/aa931243.aspx"&gt;WM_INITDIALOG&lt;/a&gt; message. The functions of interest would be &lt;font color="#000080"&gt;AlignComponents()&lt;/font&gt; and &lt;font color="#000080"&gt;InitializeComponents()&lt;/font&gt;. The dialog contains a single multiline edit box, &lt;font color="#000080"&gt;AlignComponents()&lt;/font&gt; resizes the edit box so it occupies the entire screen, and &lt;font color="#000080"&gt;InitializeComponents()&lt;/font&gt; calls &lt;a href="http://msdn.microsoft.com/en-us/library/aa922540.aspx"&gt;SetWindowText()&lt;/a&gt; to set the text on the edit box control.&lt;/p&gt;  &lt;p align="justify"&gt;If you run this program, here is how it’ll look,&lt;/p&gt;  &lt;p&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://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/Editboxesandthedefaulttextselection_10F44/image_8c9cbb0e-a7eb-4762-af87-5fc13a1b4875.png" width="307" height="470" /&gt; &lt;/p&gt;  &lt;p align="justify"&gt;So you see the problem there. Skimming through the edit box messages, I came across &lt;a href="http://msdn.microsoft.com/en-us/library/aa932540.aspx"&gt;EM_SETSEL&lt;/a&gt; message. This message selects a range of characters in an edit control, and if you pass –1 to it, any current selection is removed. So after the &lt;a href="http://msdn.microsoft.com/en-us/library/aa922540.aspx"&gt;SetWindowText()&lt;/a&gt; call in &lt;font color="#000080"&gt;InitializeComponents()&lt;/font&gt;, I put this line,&lt;/p&gt;  &lt;p&gt;SendMessage(hTemp, EM_SETSEL, (WPARAM)-1, (LPARAM)0);&lt;/p&gt;  &lt;p align="justify"&gt;but if it was this simple I wouldn’t be writing this post, would I? In short, it didn’t work.&lt;/p&gt;  &lt;p align="justify"&gt;I thought maybe I am sending the &lt;a href="http://msdn.microsoft.com/en-us/library/aa932540.aspx"&gt;EM_SETSEL&lt;/a&gt; message too early, even before the edit control is ready to process it perhaps? So the next step was to find the right place from where the message could be sent. So, one by one, I went through all the messages sent to dialog proc until the dialog is shown and found &lt;a href="http://msdn.microsoft.com/en-us/library/aa922525.aspx"&gt;WM_ACTIVATE&lt;/a&gt; could be the right place. And it indeed was.&lt;/p&gt;  &lt;p align="justify"&gt;Adding the following switch case to dialog proc did the trick,&lt;/p&gt;  &lt;div class="le-cah-container"&gt;   &lt;div style="background: #ddd; overflow: auto"&gt;     &lt;ol style="padding-bottom: 0px; margin: 0px 0px 0px 2em; padding-left: 5px; padding-right: 0px; background: #ffffff; padding-top: 0px"&gt;       &lt;li&gt;&lt;span style="color: #0000ff"&gt;case&lt;/span&gt; WM_ACTIVATE:&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    SendMessage(GetDlgItem(hDlg, IDC_EDITBOX), EM_SETSEL,&lt;/li&gt;        &lt;li&gt;        (WPARAM)-1, (LPARAM)0);&lt;/li&gt;        &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color: #0000ff"&gt;break&lt;/span&gt;;&lt;/li&gt;     &lt;/ol&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p align="justify"&gt;The edit control now shows up without any selected text.&lt;/p&gt;  &lt;p&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://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/Editboxesandthedefaulttextselection_10F44/image_5b6ac149-6ef8-429c-8947-99063f68f704.png" width="304" height="475" /&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/141414.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/08/20/edit-boxes-and-the-default-text-selection.aspx</guid>
            <pubDate>Fri, 20 Aug 2010 14:09:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/141414.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/08/20/edit-boxes-and-the-default-text-selection.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/141414.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/141414.aspx</trackback:ping>
        </item>
        <item>
            <title>HTC HD2, Accelerometer and Windows Phone 7 &amp;ndash; Source Code</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/08/01/htc-hd2-accelerometer-and-windows-phone-7-ndash-source-code.aspx</link>
            <description>&lt;p align="justify"&gt;If you haven’t seen the &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/07/31/htc-hd2-accelerometer-and-windows-phone-7.aspx"&gt;video demo in my last post&lt;/a&gt;, I suggest you watch it first to get a better understanding. So in this post we are going to see how the WCF service, the .NET Compact Framework application running on HD2 and the Silverlight application running on the Windows Phone 7 emulator work together.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;strong&gt;     &lt;br /&gt;
The Objective&lt;/strong&gt;    &lt;br /&gt;
We are going to write three applications, a simple WCF service hosted in a windows console application which implements two functions, one called UploadAccelData() and the other DownloadAccelData(). A .NET Compact Framework application for HD2 which calls the WCF services’ UploadAccelData() function periodically and passes it the x, y and z components of the acceleration vector. A silverlight application for Windows Phone 7 which calls into DownloadAccelData() periodically and gets the accelerometer data off it.&lt;/p&gt;
&lt;p align="justify"&gt;For those of you who want to jump straight into the code, you can download it from the following links,&lt;/p&gt;
&lt;p align="justify"&gt;&lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/ConsoleApp.zip"&gt;Download ConsoleApp.zip&lt;/a&gt; – VS 2008 solution which contains two projects. One, the console application which hosts the WCF service and two the Compact Framework application which runs on HD2&lt;/p&gt;
&lt;p align="justify"&gt;&lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/WCFClient%5E_WP7Emu.zip"&gt;Download WCFClient_WP7Emu.zip&lt;/a&gt; – Windows Phone 7 application which runs on the emulator&lt;/p&gt;
&lt;p align="justify"&gt;&lt;br /&gt;
&lt;strong&gt;Creating the WCF service&lt;/strong&gt;    &lt;br /&gt;
I will be using Visual Studio 2008 for creating the service and the .net cf application. Open VS 2008 in administrator mode and create a windows console application in Visual C#. It is important that you start visual studio in administrator mode otherwise you might get an error while deploying the service depending on your access rights on the machine. Now lets add a WCF service which will be hosted by this console application. Right click on the project and select “Add –&amp;gt; New Item..”. In the next dialog select “WCF Service” and give a name to it. I call it “AccelerometerService”.&lt;/p&gt;
&lt;p&gt;&lt;img height="387" border="0" width="644" style="border: 0px none ; display: inline;" title="Creating accelerometer service" alt="Creating accelerometer service" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/Creating%20accelerometer%20service_71d03876-031e-4fcd-bcf1-8348e3c7ad1c.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;With this three files will be added to your project AccelerometerService.cs, IAccelerometerService.cs and App.Config. Open App.Config and change a few things in it, first change &lt;font color="#000080"&gt;binding="wsHttpBinding"&lt;/font&gt; to &lt;font color="#000080"&gt;binding="basicHttpBinding"&lt;/font&gt; since .net compact framework only supports basicHttpBinding. Next change the BaseAddress from,&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;&amp;lt;add baseAddress="&lt;/font&gt;&lt;a href="http://localhost:8731/Design_Time_Addresses/ConsoleApp/AccelerometerService/&amp;quot;"&gt;&lt;font color="#000080"&gt;http://localhost:8731/Design_Time_Addresses/ConsoleApp/AccelerometerService/"&lt;/font&gt;&lt;/a&gt;&lt;font color="#000080"&gt; /&amp;gt; to &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;&amp;lt;add baseAddress="&lt;/font&gt;&lt;a title="http://192.168.2.2:8731/ConsoleApp/AccelerometerService/" href="http://192.168.2.2:8731/ConsoleApp/AccelerometerService/"&gt;&lt;font color="#000080"&gt;http://192.168.2.2:8731/ConsoleApp/AccelerometerService/&lt;/font&gt;&lt;/a&gt;&lt;a href="http://169.254.2.2:8731/ConsoleApp/AccelerometerService/&amp;quot;"&gt;&lt;font color="#000080"&gt;"&lt;/font&gt;&lt;/a&gt;&lt;font color="#000080"&gt; /&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Again, it is important that you change “localhost” to the ip address of your machine. This is because we will be using this address from the compact framework application to refer to the service, so we need the address of the machine where the service is running. Using “localhost” from the compact framework application would mean a totally different thing. Here I use 192.168.2.2 which is the IP address assigned to my machine by the wireless router that I am using. &lt;strong&gt;Be sure to change this IP address depending on your machine setup.&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Open Program.cs and add the following code in the Main() function,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;static void Main(string[] args)     &lt;br /&gt;
{      &lt;br /&gt;
    ServiceHost accelService = new ServiceHost(typeof(AccelerometerService)); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    try     &lt;br /&gt;
    {      &lt;br /&gt;
        accelService.Open();      &lt;br /&gt;
        Console.WriteLine("Accel service is running at:{0}", accelService.BaseAddresses[0].AbsoluteUri);      &lt;br /&gt;
        Console.WriteLine("Press &amp;lt;ENTER&amp;gt; to terminate the service.."); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;        Console.ReadLine();     &lt;br /&gt;
    }      &lt;br /&gt;
    catch (Exception ex)      &lt;br /&gt;
    {      &lt;br /&gt;
        Console.WriteLine("An exception occured while creating the serivce. {0}", ex.Message);      &lt;br /&gt;
        accelService.Abort();      &lt;br /&gt;
    }      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Here we are creating an instance of our AccelerometerService and calling Open() on it to start the service. The address where the service is running is output to the console and the service waits for any key to be pressed after which it will be terminated. Now build and run your project, you should see this console window,&lt;/p&gt;
&lt;p&gt;&lt;img height="325" border="0" width="644" style="border: 0px none ; display: inline;" title="Console service running" alt="Console service running" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/Console%20service%20running_4a39e1a3-a627-46af-a57c-8e694b3ab209.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Now to check our service, open internet explorer and browse to the base address that we entered above,&lt;/p&gt;
&lt;p&gt;&lt;a title="http://192.168.2.2:8731/ConsoleApp/AccelerometerService/" href="http://192.168.2.2:8731/ConsoleApp/AccelerometerService/"&gt;&lt;font color="#000080"&gt;http://192.168.2.2:8731/ConsoleApp/AccelerometerService/&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;and you should see this,&lt;/p&gt;
&lt;p&gt;&lt;img height="336" border="0" width="644" style="border: 0px none ; display: inline;" title="sanity check for service IE" alt="sanity check for service IE" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/sanity%20check%20for%20service%20IE_8506ea17-52e5-488c-b972-a3cb6a4caf62.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;If your browser is showing the same thing then great! We are progressing well.&lt;/p&gt;
&lt;p align="justify"&gt;Now lets implement our upload and download functions. Open IAccelerometerService.cs and add the following declarations to the interface,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;[OperationContract]     &lt;br /&gt;
void UploadAccelData(int x, int y, int z); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;[OperationContract]     &lt;br /&gt;
void DownloadAccelData(out int x, out int y, out int z); &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;To keep things simple I am passing only the x, y and z components of the vector, other information from the accelerometer like Roll and Pitch are ignored for simplicity, though adding those shouldn’t be rocket science. We will be implementing these functions in our AccelerometerService class. Open AccelerometerService.cs and add the following member variables to the AccelerometerService class,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;static int _accelX;     &lt;br /&gt;
static int _accelY;      &lt;br /&gt;
static int _accelZ; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;and add the following function definitions to the same class,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;public void UploadAccelData(int x, int y, int z)     &lt;br /&gt;
{      &lt;br /&gt;
        _accelX = x;      &lt;br /&gt;
        _accelY = y;      &lt;br /&gt;
        _accelZ = z;      &lt;br /&gt;
&lt;br /&gt;
        Console.WriteLine("UploadAccelData: X:{0}, Y:{1}, Z:{2}", x, y, z); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;public void DownloadAccelData(out int x, out int y, out int z)     &lt;br /&gt;
{      &lt;br /&gt;
        x = _accelX;      &lt;br /&gt;
        y = _accelY;      &lt;br /&gt;
        z = _accelZ;      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This completes our console app and the WCF service that it will be hosting.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;strong&gt;Creating the Compact Framework Application for HD2&lt;/strong&gt;    &lt;br /&gt;
Now lets go ahead and add a smart device compact framework C# application to the same solution in VS 2008. Right click on the solution and select “Add –&amp;gt; New Project..”. In the Add New Project dialog select Smart Device under Visual C# and add a new project, WCFClient_HD2.&lt;/p&gt;
&lt;p&gt;&lt;img height="412" border="0" width="644" style="border: 0px none ; display: inline;" title="HD2 CF add new project" alt="HD2 CF add new project" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/HD2%20CF%20add%20new%20project_09710d65-be9a-4ebb-8395-12ab368a06da.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;in the next screen select “Windows Mobile 6 Professional SDK” as the target platform and “.NET CF Version 3.5”, choose Device Application and press OK.&lt;/p&gt;
&lt;p&gt;&lt;img height="484" border="0" width="643" style="border: 0px none ; display: inline;" title="HD2 cf project type" alt="HD2 cf project type" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/HD2%20cf%20project%20type_8aa3ffd1-a411-4e3e-a18f-9c8b422e4f15.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;I have designed the form to show the x, y and z values from the accelerometer and a button to start and stop the sensor. Also, add a timer which fires every 100ms and is initially disabled.&lt;/p&gt;
&lt;p&gt;&lt;img height="484" border="0" width="529" style="border: 0px none ; display: inline;" title="cf form and timer" alt="cf form and timer" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/cf%20form%20and%20timer_f4df9f56-b5fa-499c-acac-fb9a3123a4a3.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Now to this project, we will add a reference to the WCF service. First make sure that the service is running, deploy the ConsoleApp project. Right click on WCFClient_HD2 and select “Add Web Reference..”. In the next dialog, enter the URL of the service and press Go.&lt;/p&gt;
&lt;p&gt; &lt;img height="448" border="0" width="644" style="border: 0px none ; display: inline;" title="adding reference to service" alt="adding reference to service" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/adding%20reference%20to%20service_a448e344-f59e-4f49-ba25-0877c639684d.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;If everything goes well, our AccelerometerService should be found and you will be presented with this dialog,&lt;/p&gt;
&lt;p&gt;&lt;img src="/images/geekswithblogs_net/TechTwaddle/adding%20reference%20to%20service%20found_2_2.jpg" alt="" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;give a name to the web reference, in this case “AccelerometerService” and select “Add Reference” button. Now the functionalities offered by the AccelerometerService are available to our compact framework application. Next, we need to instantiate this service and use it to call the UploadAccelData() function.&lt;/p&gt;
&lt;p align="justify"&gt;Open Form1.cs and add a member variable to the Form1 class which refers to the WCF service,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;WCFClient_HD2.AccelerometerService.AccelerometerService accelService =      &lt;br /&gt;
        new WCFClient_HD2.AccelerometerService.AccelerometerService(); &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;I am going to go a bit off topic here and talk about the Accelerometer implementation on HD2. HTC does not provide an SDK for it’s devices (which is really sad) so the functionalities like Accelerometer, Proximity sensor and other custom features are not exposed to others for use. So some folks had to find out the library, reverse engineer the implementation and figure out how to use these features. The accelerometer driver is implemented in a dll called HTCSensorSDK.dll, this dll also implements a few other sensors as well. To use the accelerometer from a native application you’ll have to load this library [using LoadLibrary()], get pointers to the exported functions and call them. And from a managed application you’ll have to P/Invoke. Take a look at &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/07/15/htc-hd2-and-the-accelerometer.aspx"&gt;this post&lt;/a&gt; for more information on this.&lt;/p&gt;
&lt;p align="justify"&gt;Managed code to access the accelerometer on HD2 is available online and I am using the same code in this project here. Just go ahead and add &lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/HTCSensor.cs"&gt;HTCSensor.cs&lt;/a&gt; file to your project. This file implements HTCSensor class which you can use to read data from the accelerometer. The class implements StartSensor(), ReadDataFromSensor() and StopSensor() functions. Go back to Form1.cs and add two more variables to the class,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;HTCSensor htcSensor = new HTCSensor();     &lt;br /&gt;
HTCSensorData htcSensorData; &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Create handlers for the Start Sensor button and uploadTimer,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void button1_Click(object sender, EventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    Button btn = sender as Button; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if (btn.Text.Contains("Start"))     &lt;br /&gt;
    {      &lt;br /&gt;
        htcSensor.StartSensor();      &lt;br /&gt;
        uploadTimer.Enabled = true; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;        btn.Text = "Stop Sensor";     &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        htcSensor.StopSensor();      &lt;br /&gt;
        uploadTimer.Enabled = false; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;        lblXAxis.Text = "???";     &lt;br /&gt;
        lblYAxis.Text = "???";      &lt;br /&gt;
        lblZAxis.Text = "???"; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;        btn.Text = "Start Sensor";     &lt;br /&gt;
    }      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;and,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void uploadTimer_Tick(object sender, EventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    htcSensor.ReadDataFromSensor(out htcSensorData); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    lblXAxis.Text = htcSensorData.tiltX.ToString();     &lt;br /&gt;
    lblYAxis.Text = htcSensorData.tiltY.ToString();      &lt;br /&gt;
    lblZAxis.Text = htcSensorData.tiltZ.ToString();            &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;The button handler controls the timer and starts and stops it. And whenever the timer fires, we read the data from the accelerometer and display it. Run this application on HD2, if you have one, and check if the values are being read from the sensor. I am guessing that this application should also work on HTC Touch Pro2 and HTC Diamond 2 as well, but I have not tested this on those devices. Now all that is left is hooking this with the WCF Service, so every time the timer function is called we call the UploadAccelData() function of the service and pass the x, y and z values to it. Add the following lines of code to the end of the uploadTimer_Tick() function,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;try     &lt;br /&gt;
{      &lt;br /&gt;
    accelService.UploadAccelData(htcSensorData.tiltX, true,      &lt;br /&gt;
        htcSensorData.tiltY, true,      &lt;br /&gt;
        htcSensorData.tiltZ, true);      &lt;br /&gt;
}      &lt;br /&gt;
catch (Exception ex)      &lt;br /&gt;
{      &lt;br /&gt;
    Console.WriteLine("An exception occured while calling into the service.");      &lt;br /&gt;
    Console.WriteLine("{0}", ex.Message);      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;And that is all, our compact framework application is ready! Lets test whether the values are being passed to the service. Go to the solution properties and select multiple start-up projects and make sure that both the ConsoleApp and WCFClient_HD2 are set to start,&lt;/p&gt;
&lt;p&gt;&lt;img height="404" border="0" width="644" style="border: 0px none ; display: inline;" title="multiple start-up projects" alt="multiple start-up projects" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/multiple%20start-up%20projects_e5afbc85-72ed-42ac-904f-a90a3e6be8f6.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Press F5 to start both the console and the compact framework application. Make sure that you are deploying the compact framework application to the device and that the device is connected to the machine over Active Sync (Windows XP) or WMDC (Vista or Win 7). Once both applications are deployed press the “Start Sensor” button on the phone, the accelerometer data should be getting passed to the service and the console window should output the logs as follows,&lt;/p&gt;
&lt;p&gt;&lt;img height="325" border="0" width="644" style="border: 0px none ; display: inline;" title="console logs showin service accel data" alt="console logs showin service accel data" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/console%20logs%20showin%20service%20accel%20data_82e8278f-0104-41b9-a1ce-a5f39c1623b3.jpg" /&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p align="justify"&gt;&lt;strong&gt;Creating the Silverlight application for Windows Phone 7&lt;/strong&gt;    &lt;br /&gt;
Start Visual Studio 2010 Express for Windows Phone and create a new Silverlight Windows Phone project, WCFClient_WP7Emu.&lt;/p&gt;
&lt;p&gt;&lt;img height="383" border="0" width="644" style="border: 0px none ; display: inline;" title="sl app wp7 project creation" alt="sl app wp7 project creation" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/sl%20app%20wp7%20project%20creation_5dd65fae-40e8-49fd-ae96-cb1db769f866.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Add text blocks and a button to Mainpage.xaml. The application UI looks like this,&lt;/p&gt;
&lt;p&gt;&lt;img height="484" border="0" width="267" style="border: 0px none ; display: inline;" title="sl app running on emu" alt="sl app running on emu" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/sl%20app%20running%20on%20emu_55a2c96f-c1f5-4ae4-985a-89fc667379df.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Now lets add to this project a reference to the WCF Service. Deploy the WCF service from Visual Studio 2008 and make sure that it is running, right click on the silverlight project and select “Add Service Reference..”, and in the next dialog enter the URL of the service and press Go,&lt;/p&gt;
&lt;p&gt;&lt;img height="484" border="0" width="600" style="border: 0px none ; display: inline;" title="sl adding reference to service" alt="sl adding reference to service" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/sl%20adding%20reference%20to%20service_76699263-a931-4f37-ba89-d29eb2ff1988.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;once the service is discovered enter a namespace “AccelerometerService” and press OK to add a reference to it.&lt;/p&gt;
&lt;p align="justify"&gt;Open Mainpage.xaml.cs and add the following member variables to the Mainpage class,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;WCFClient_WP7Emu.AccelerometerService.AccelerometerServiceClient accelService;     &lt;br /&gt;
DispatcherTimer dt; &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Now in the Mainpage() constructor add the following lines of code after the InitializeComponent() call,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;accelService = new WCFClient_WP7Emu.AccelerometerService.AccelerometerServiceClient(); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;accelService.DownloadAccelDataCompleted +=      &lt;br /&gt;
    new EventHandler&amp;lt;AccelerometerService.DownloadAccelDataCompletedEventArgs&amp;gt;(accelService_DownloadAccelDataCompleted); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;dt = new DispatcherTimer();     &lt;br /&gt;
dt.Interval = new TimeSpan(0, 0, 0, 0, 100);      &lt;br /&gt;
dt.Tick += new EventHandler(dt_Tick); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;lblXAxis.Text = "???";     &lt;br /&gt;
lblYAxis.Text = "???";      &lt;br /&gt;
lblZAxis.Text = "???";&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Here we create an instance of the WCF service and register a handler for the DownloadAccelDataCompleted event. In silverlight for windows phone the web service api’s will be called asynchronously, and when the web service function returns, accelService_DownloadAccelDataCompleted() will be called. So we will be getting the accelerometer values in this function. Also, create a dispatch timer which fires every 100ms. Pressing TAB while creating the handlers for DownloadAccelDataCompleted and dt.Tick event will also create the function definitions for the handlers,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;void accelService_DownloadAccelDataCompleted(object sender, AccelerometerService.DownloadAccelDataCompletedEventArgs e)     &lt;br /&gt;
{ &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;void dt_Tick(object sender, EventArgs e)     &lt;br /&gt;
{ &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Add a handler for the button “Start reading data from web service”,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void button1_Click(object sender, RoutedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    Button btn = sender as Button; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if (btn.Content.ToString().Contains("Start"))     &lt;br /&gt;
    {      &lt;br /&gt;
        dt.Start();      &lt;br /&gt;
        btn.Content = "Stop reading data from web service";      &lt;br /&gt;
    }      &lt;br /&gt;
    else      &lt;br /&gt;
    {      &lt;br /&gt;
        dt.Stop();      &lt;br /&gt;
        btn.Content = "Start reading data from web service"; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;        lblXAxis.Text = "???";     &lt;br /&gt;
        lblYAxis.Text = "???";      &lt;br /&gt;
        lblZAxis.Text = "???";      &lt;br /&gt;
    }      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;So again, this handler just starts and stops the dispatch timer. Now fill in the handlers for dt.Tick and the DownloadAccelDataCompleted() functions as below,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;void accelService_DownloadAccelDataCompleted(object sender, AccelerometerService.DownloadAccelDataCompletedEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    lblXAxis.Text = e.Result.ToString();      &lt;br /&gt;
    lblYAxis.Text = e.y.ToString();      &lt;br /&gt;
    lblZAxis.Text = e.z.ToString();      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;void dt_Tick(object sender, EventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    accelService.DownloadAccelDataAsync();      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;So everytime the dispatch timer fires, we call the DownloadAccelData() function (the asynchronous version is created when we add a reference to the WCF service). And in the DownloadAccelDataCompleted() function we update the text blocks to display the accelerometer values.&lt;/p&gt;
&lt;p align="justify"&gt;And we are done, phew! Time to test’em all. Go to visual studio 2008 and start both the ConsoleApp and WCFClient_HD2 projects, once deployed press the “Start Sensor” button on the device. Now start the silverlight application from Visual Studio 2010 and press the “Start reading data from web service” button, and if you did everything right the accelerometer data should be read on the emulator and you should be seeing something like this,&lt;/p&gt;
&lt;p&gt;&lt;img height="424" border="0" width="644" style="border: 0px none ; display: inline;" title="service and sl app final" alt="service and sl app final" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/HTCHD2AccelerometerandWindowsPhone7Sourc_C0CB/service%20and%20sl%20app%20final_1ea223cb-54d3-47ed-809f-6decbe235fc8.jpg" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;The other application which I showed in the video demo where a marble moved around on the screen of the emulator can now be easily implemented using the accelerometer values. I will not be going into the details of it. If you want to know how to implement one take a look at &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/07/15/htc-hd2-and-the-accelerometer.aspx"&gt;my other post&lt;/a&gt; where I have the source code of the application attached, the same logic holds here as well.&lt;/p&gt;
&lt;p&gt;It’s been a really long post, if you have an HD2 and happen to try out the source code do let me know how it goes. And if you have any suggestions on how to make this code better please do leave a comment!&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/141143.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/08/01/htc-hd2-accelerometer-and-windows-phone-7-ndash-source-code.aspx</guid>
            <pubDate>Sun, 01 Aug 2010 17:26:57 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/141143.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/08/01/htc-hd2-accelerometer-and-windows-phone-7-ndash-source-code.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/141143.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/141143.aspx</trackback:ping>
        </item>
        <item>
            <title>Shake detection on HTC HD2</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/07/24/shake-detection-on-htc-hd2.aspx</link>
            <description>&lt;p align="justify"&gt;In the &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/07/15/htc-hd2-and-the-accelerometer.aspx"&gt;previous post&lt;/a&gt; I mentioned that the acceleration vector or the force vector can be used to detect gestures like shake. Here is a crude implementation of shake detection,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private const double SHAKE_THRESHOLD = 5.0f; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private bool IsShakeDetected(HTCSensorData sensorData)      &lt;br /&gt;
{       &lt;br /&gt;
    bool isShake = false; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double x = (double)sensorData.tiltX / 1000;      &lt;br /&gt;
    double y = (double)sensorData.tiltY / 1000;       &lt;br /&gt;
    double z = (double)sensorData.tiltZ / 1000; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double forceSqrd = x * x + y * y + z * z; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    System.Console.WriteLine("Force:{0}", forceSqrd);      &lt;br /&gt;
&lt;br /&gt;
    if (forceSqrd &amp;gt;= SHAKE_THRESHOLD)       &lt;br /&gt;
    {       &lt;br /&gt;
        isShake = true;       &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    return isShake;      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;This function calculates the square of the magnitude of the force vector and if the value exceeds a certain threshold, shake is detected. After experimenting with a few threshold values, 5.0f seemed to work ok. The problem with this implementation is that it makes an assumption that if the force acting on the device is more than 5.0f, it must be experiencing shake. This, of course, can be false. If you hold and swing the device in one fast sweep, shake will be detected. Technically, that’s not a shake if you ask me. The algorithm can be improved by checking the force against the threshold continuously, say 4-5 times, will work on that when I have time. Here’s a video showing shake detection,&lt;/p&gt;
&lt;p&gt;&lt;object width="480" height="385"&gt;
&lt;param value="http://www.youtube.com/v/RVrF0RCypyI&amp;amp;hl=en_US&amp;amp;fs=1?color1=0x3a3a3a&amp;amp;color2=0x999999" name="movie" /&gt;
&lt;param value="true" name="allowFullScreen" /&gt;
&lt;param value="always" name="allowscriptaccess" /&gt;&lt;embed width="480" height="385" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/RVrF0RCypyI&amp;amp;hl=en_US&amp;amp;fs=1?color1=0x3a3a3a&amp;amp;color2=0x999999"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Mark Arteaga wrote a &lt;a href="http://blog.markarteaga.com/ShakeDetectionOnWindowsPhone7.aspx"&gt;post on shake detection&lt;/a&gt; sometime back. The algorithm he used compares (x, y), (x, z) and (y, z) values against a threshold. A shake is registered when the threshold is reached or exceeded 5 times. I replaced my function with his implementation and it works nicely.&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void FuncWhereYouWantToDetectShake()&lt;br /&gt;
{&lt;br /&gt;
    if (!shaking &amp;amp;&amp;amp; CheckForShake(prevSensorData, sensorData, SHAKE_THRESHOLD_LEVEL) &amp;amp;&amp;amp;     &lt;br /&gt;
        shakeCount &amp;gt;= 5)      &lt;br /&gt;
    {      &lt;br /&gt;
        shaking = true;      &lt;br /&gt;
        shakeCount = 0;      &lt;br /&gt;
        &lt;span style="color: rgb(51, 153, 102);"&gt;//SHAKE DETECTED!      &lt;/span&gt;&lt;br /&gt;
    }      &lt;br /&gt;
    else if (CheckForShake(prevSensorData, sensorData, SHAKE_THRESHOLD_LEVEL))      &lt;br /&gt;
    {      &lt;br /&gt;
        shakeCount++;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if (!CheckForShake(prevSensorData, sensorData, 200))      &lt;br /&gt;
    {      &lt;br /&gt;
        shakeCount = 0;      &lt;br /&gt;
        shaking = false;      &lt;br /&gt;
    }      &lt;br /&gt;
    prevSensorData = sensorData;            &lt;/font&gt;&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private bool CheckForShake(HTCSensorData prevData, HTCSensorData currentData, double threshold)     &lt;br /&gt;
{      &lt;br /&gt;
    double deltaX = Math.Abs(prevSensorData.tiltX - sensorData.tiltX);      &lt;br /&gt;
    double deltaY = Math.Abs(prevSensorData.tiltY - sensorData.tiltY);      &lt;br /&gt;
    double deltaZ = Math.Abs(prevSensorData.tiltZ - sensorData.tiltZ); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    return (deltaX &amp;gt; threshold &amp;amp;&amp;amp; deltaY &amp;gt; threshold) ||     &lt;br /&gt;
        (deltaX &amp;gt; threshold &amp;amp;&amp;amp; deltaZ &amp;gt; threshold) ||      &lt;br /&gt;
        (deltaY &amp;gt; threshold &amp;amp;&amp;amp; deltaZ &amp;gt; threshold);      &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Update:&lt;/strong&gt;&lt;br /&gt;
Source Code: &lt;a href="http://cid-65187cfbc4ac6ad2.office.live.com/self.aspx/.Public/SourceCode/AccelerometerDemo-Shake.zip"&gt;AccelerometerDemo-Shake.zip&lt;/a&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/141050.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/07/24/shake-detection-on-htc-hd2.aspx</guid>
            <pubDate>Sat, 24 Jul 2010 12:23:08 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/141050.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/07/24/shake-detection-on-htc-hd2.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/141050.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/141050.aspx</trackback:ping>
        </item>
        <item>
            <title>Applications: The Mathematics of Movement, Part 3</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/06/12/applications-the-mathematics-of-movement-part-3.aspx</link>
            <description>&lt;p&gt;Previously: &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx"&gt;Part 1&lt;/a&gt;, &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx"&gt;Part 2&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;As promised in the &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx"&gt;previous post&lt;/a&gt;, this post will cover two variations of the marble move program. The first one, Infinite Move, keeps the marble moving towards the click point, rebounding it off the screen edges and changing its direction when the user clicks again. The second version, Finite Move, is the same as first except that the marble does not move forever. It moves towards the click point, rebounds off the screen edges and slowly comes to rest. The amount of time that it moves depends on the distance between the click point and marble.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Infinite Move&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;This case is simple (actually both cases are simple). In this case all we need is the direction information which is exactly what the unit vector stores. So when the user clicks, you calculate the unit vector towards the click point and then keep updating the marbles position like crazy. And, of course, there is no stop condition. There’s a little more additional code in the bounds checking conditions. Whenever the marble goes off the screen boundaries, we need to reverse its direction.  Here is the code for mouse up event and UpdatePosition() method,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//stores the unit vector       &lt;br /&gt;
&lt;/font&gt;double unitX = 0, unitY = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;double speed = 6; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//speed times the unit vector       &lt;br /&gt;
&lt;/font&gt;double incrX = 0, incrY = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void Form1_MouseUp(object sender, MouseEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    double x = e.X - marble1.x;      &lt;br /&gt;
    double y = e.Y - marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //calculate distance between click point and current marble position       &lt;br /&gt;
&lt;/font&gt;    double lenSqrd = x * x + y * y;      &lt;br /&gt;
    double len = Math.Sqrt(lenSqrd); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt; &lt;font color="#408080"&gt;   //unit vector along the same direction (from marble towards click point)       &lt;br /&gt;
&lt;/font&gt;    unitX = x / len;      &lt;br /&gt;
    unitY = y / len; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    timer1.Enabled = true;     &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void UpdatePosition()     &lt;br /&gt;
{      &lt;br /&gt;
&lt;/font&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //amount by which to increment marble position     &lt;br /&gt;
&lt;/font&gt;    incrX = speed * unitX;    &lt;br /&gt;
    incrY = speed * unitY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    marble1.x += incrX;     &lt;br /&gt;
    marble1.y += incrY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //check for bounds       &lt;br /&gt;
&lt;/font&gt;    if ((int)marble1.x &amp;lt; MinX + marbleWidth / 2)      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MinX + marbleWidth / 2;      &lt;br /&gt;
        unitX *= -1;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.x &amp;gt; (MaxX - marbleWidth / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MaxX - marbleWidth / 2;      &lt;br /&gt;
        unitX *= -1;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if ((int)marble1.y &amp;lt; MinY + marbleHeight / 2)     &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MinY + marbleHeight / 2;      &lt;br /&gt;
        unitY *= -1;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.y &amp;gt; (MaxY - marbleHeight / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MaxY - marbleHeight / 2;      &lt;br /&gt;
        unitY *= -1;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;So whenever the user clicks we calculate the unit vector along that direction and also the amount by which the marble position needs to be incremented. The speed in this case is fixed at 6. You can experiment with different values. And under bounds checking, whenever the marble position goes out of bounds along the &lt;strong&gt;x&lt;/strong&gt; or &lt;strong&gt;y&lt;/strong&gt; direction we reverse the direction of the unit vector along that direction. Here’s a video of it running;&lt;/p&gt;
&lt;object width="425" height="344"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/Vf03EVbLHgI&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowscriptaccess" value="always" /&gt;&lt;embed width="425" height="344" src="http://www.youtube.com/v/Vf03EVbLHgI&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Finite Move&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;The code for finite move is almost exactly same as that of Infinite Move, except for the difference that the speed is not fixed and there is an end condition, so the marble comes to rest after a while. Code follows,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//unit vector along the direction of click point       &lt;br /&gt;
&lt;/font&gt;double unitX = 0, unitY = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//speed of the marble       &lt;br /&gt;
&lt;/font&gt;double speed = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void Form1_MouseUp(object sender, MouseEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    double x = 0, y = 0;      &lt;br /&gt;
    double lengthSqrd = 0, length = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    x = e.X - marble1.x;     &lt;br /&gt;
    y = e.Y - marble1.y;      &lt;br /&gt;
    lengthSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //length in pixels (between click point and current marble pos)       &lt;br /&gt;
&lt;/font&gt;    length = Math.Sqrt(lengthSqrd); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //unit vector along the same direction as vector(x, y)       &lt;br /&gt;
&lt;/font&gt;    unitX = x / length;      &lt;br /&gt;
    unitY = y / length; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    speed = length / 12; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    timer1.Enabled = true;     &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void UpdatePosition()     &lt;br /&gt;
{      &lt;br /&gt;
    marble1.x += speed * unitX;      &lt;br /&gt;
    marble1.y += speed * unitY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //check for bounds       &lt;br /&gt;
&lt;/font&gt;    if ((int)marble1.x &amp;lt; MinX + marbleWidth / 2)      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MinX + marbleWidth / 2;      &lt;br /&gt;
        unitX *= -1;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.x &amp;gt; (MaxX - marbleWidth / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MaxX - marbleWidth / 2;      &lt;br /&gt;
        unitX *= -1;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if ((int)marble1.y &amp;lt; MinY + marbleHeight / 2)     &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MinY + marbleHeight / 2;      &lt;br /&gt;
        unitY *= -1;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.y &amp;gt; (MaxY - marbleHeight / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MaxY - marbleHeight / 2;      &lt;br /&gt;
        unitY *= -1;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //reduce speed by 3% in every loop       &lt;br /&gt;
&lt;/font&gt;    speed = speed * 0.97f;      &lt;br /&gt;
    if ((int)speed &amp;lt;= 0)      &lt;br /&gt;
    {      &lt;br /&gt;
        timer1.Enabled = false;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;So the only difference is that the speed is calculated as a function of length when the mouse up event occurs. Again, this can be experimented with. Bounds checking is same as before. In the update and draw cycle, we reduce the speed by 3% in every cycle. Since &lt;font color="#000080"&gt;speed&lt;/font&gt; is calculated as a function of &lt;font color="#000080"&gt;length&lt;/font&gt;, &lt;font color="#000080"&gt;speed = length/12&lt;/font&gt;, the amount of time it takes &lt;font color="#000080"&gt;speed&lt;/font&gt; to reach zero is directly proportional to &lt;font color="#000080"&gt;length&lt;/font&gt;. Note that the speed is in ‘pixels per 40ms’ because the timeout value of the timer is 40ms.  The readability can be improved by representing speed in ‘pixels per second’. This would require you to add some more calculations to the code, which I leave out as an exercise. Here’s a video of this second version,&lt;/p&gt;
&lt;object width="425" height="344"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/is6iySs4D80&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowscriptaccess" value="always" /&gt;&lt;embed width="425" height="344" src="http://www.youtube.com/v/is6iySs4D80&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/140373.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/06/12/applications-the-mathematics-of-movement-part-3.aspx</guid>
            <pubDate>Sat, 12 Jun 2010 14:02:08 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/140373.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/06/12/applications-the-mathematics-of-movement-part-3.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/140373.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/140373.aspx</trackback:ping>
        </item>
        <item>
            <title>Applications: The Mathematics of Movement, Part 2</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx</link>
            <description>&lt;p align="justify"&gt;In &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx"&gt;part 1 of this series&lt;/a&gt; we saw how we can make the marble move towards the click point, with a fixed speed. In this post we’ll see, first, how to get rid of Atan2(), sine() and cosine() in our calculations, and, second, reducing the speed of the marble as it approaches the destination, so it looks like the marble is easing into it’s final position. As I mentioned in &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/06/applications-some-more-marble-fun.aspx"&gt;one of the previous posts&lt;/a&gt;, this is achieved by making the speed of the marble a function of the distance between the marble and the destination point.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&lt;strong&gt;Getting rid of Atan2(), sine() and cosine()&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Ok, to be fair we are not exactly getting rid of these trigonometric functions, rather, replacing one form with another. So instead of writing sin(θ), we write y/length. You see the point. So instead of using the trig functions as below,&lt;/p&gt;
&lt;p&gt;&lt;font color="#808080"&gt;double x = destX - marble1.x;      &lt;br /&gt;
double y = destY - marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#808080"&gt;&lt;font color="#408080"&gt;//distance between destination and current position, before updating marble position        &lt;br /&gt;
&lt;/font&gt;distanceSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#808080"&gt;double angle = Math.Atan2(y, x);      &lt;br /&gt;
&lt;br /&gt;
&lt;font color="#408080"&gt;//Cos and Sin give us the unit vector, 6 is the value we use to magnify the unit vector along the same direction&lt;/font&gt;       &lt;br /&gt;
incrX = speed * Math.Cos(angle);       &lt;br /&gt;
incrY = speed * Math.Sin(angle); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#808080"&gt;marble1.x += incrX;      &lt;br /&gt;
marble1.y += incrY;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;we use the following,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;double x = destX - marble1.x;     &lt;br /&gt;
double y = destY - marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//distance between destination and marble (before updating marble position)       &lt;br /&gt;
&lt;/font&gt;lengthSqrd = x * x + y * y;      &lt;br /&gt;
length = Math.Sqrt(lengthSqrd); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//unit vector along the same direction as vector(x, y)&lt;/font&gt;      &lt;br /&gt;
unitX = x / length;      &lt;br /&gt;
unitY = y / length; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;//update marble position&lt;/font&gt;      &lt;br /&gt;
incrX = speed * unitX;      &lt;br /&gt;
incrY = speed * unitY;      &lt;br /&gt;
marble1.x += incrX;      &lt;br /&gt;
marble1.y += incrY;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;so we replaced cos(θ) with x/length and sin(θ) with y/length. The result is the same.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Adding oomph to the way it moves&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt;In the &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx"&gt;last post&lt;/a&gt; we had the speed of the marble fixed at 6,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;double speed = 6;&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;to make the marble decelerate as it moves, we have to keep updating the speed of the marble in every frame such that the speed is calculated as a function of the length. So we may have, &lt;font color="#000080"&gt;speed = length/12;&lt;/font&gt; ‘length’ keeps decreasing as the marble moves and so does &lt;font color="#000080"&gt;speed&lt;/font&gt;. The &lt;font color="#000080"&gt;Form1_MouseUp()&lt;/font&gt; function remains the same as before, here is the &lt;font color="#000080"&gt;UpdatePosition()&lt;/font&gt; method,&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void UpdatePosition()     &lt;br /&gt;
{      &lt;br /&gt;
    double incrX = 0, incrY = 0;      &lt;br /&gt;
    double lengthSqrd = 0, length = 0, lengthSqrdNew = 0;      &lt;br /&gt;
    double unitX = 0, unitY = 0;      &lt;br /&gt;
    double speed = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double x = destX - marble1.x;     &lt;br /&gt;
    double y = destY - marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//distance between destination and marble (before updating marble position)&lt;/font&gt;      &lt;br /&gt;
    lengthSqrd = x * x + y * y;      &lt;br /&gt;
    length = Math.Sqrt(lengthSqrd); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//unit vector along the same direction as vector(x, y)&lt;/font&gt;      &lt;br /&gt;
    unitX = x / length;      &lt;br /&gt;
    unitY = y / length; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//speed as a function of length&lt;/font&gt;      &lt;br /&gt;
    speed = length / 12; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//update marble position&lt;/font&gt;      &lt;br /&gt;
    incrX = speed * unitX;      &lt;br /&gt;
    incrY = speed * unitY;      &lt;br /&gt;
    marble1.x += incrX;      &lt;br /&gt;
    marble1.y += incrY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//check for bounds&lt;/font&gt;      &lt;br /&gt;
    if ((int)marble1.x &amp;lt; MinX + marbleWidth / 2)      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MinX + marbleWidth / 2;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.x &amp;gt; (MaxX - marbleWidth / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MaxX - marbleWidth / 2;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if ((int)marble1.y &amp;lt; MinY + marbleHeight / 2)     &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MinY + marbleHeight / 2;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.y &amp;gt; (MaxY - marbleHeight / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MaxY - marbleHeight / 2;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//distance between destination and marble (after updating marble position)&lt;/font&gt;      &lt;br /&gt;
    x = destX - (marble1.x);      &lt;br /&gt;
    y = destY - (marble1.y);      &lt;br /&gt;
    lengthSqrdNew = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    /*       &lt;br /&gt;
     * End Condition:        &lt;br /&gt;
     * 1. If there is not much difference between lengthSqrd and lengthSqrdNew        &lt;br /&gt;
     * 2. If the marble has moved more than or equal to a distance of totLenToTravel (see Form1_MouseUp)        &lt;br /&gt;
     */        &lt;br /&gt;
&lt;/font&gt;    x = startPosX - marble1.x;      &lt;br /&gt;
    y = startPosY - marble1.y;      &lt;br /&gt;
    double totLenTraveledSqrd = x * x + y * y;      &lt;br /&gt;
    if ((int)totLenTraveledSqrd &amp;gt;= (int)totLenToTravelSqrd)      &lt;br /&gt;
    {      &lt;br /&gt;
        System.Console.WriteLine("&lt;font color="#800000"&gt;Stopping because Total Len has been traveled&lt;/font&gt;");      &lt;br /&gt;
        timer1.Enabled = false;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if (Math.Abs((int)lengthSqrd - (int)lengthSqrdNew) &amp;lt; 4)      &lt;br /&gt;
    {      &lt;br /&gt;
        System.Console.WriteLine("&lt;font color="#800000"&gt;Stopping because no change in Old and New&lt;/font&gt;");      &lt;br /&gt;
        timer1.Enabled = false;      &lt;br /&gt;
    }      &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;A point to note here is that, in this implementation, the marble never stops because it travelled a distance of &lt;font color="#000080"&gt;totLenToTravelSqrd&lt;/font&gt; (first if condition). This happens because speed is a function of the length. During the final few frames &lt;font color="#000080"&gt;length&lt;/font&gt; becomes very small and so does &lt;font color="#000080"&gt;speed&lt;/font&gt;; and so the amount by which the marble shifts is quite small, and the second if condition always hits true first.&lt;/p&gt;
&lt;p align="justify"&gt;I’ll end this series with a third post. In part 3 we will cover two things, one, when the user clicks, the marble keeps moving in that direction, rebounding off the screen edges and keeps moving forever. Two, when the user clicks on the screen, the marble moves towards it, with it’s speed reducing by every frame. It doesn’t come to a halt when the destination point is reached, instead, it continues to move, rebounds off the screen edges and slowly comes to halt. The amount of time that the marble keeps moving depends on how far the user clicks from the marble. I had mentioned this second situation &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/06/applications-some-more-marble-fun.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p align="justify"&gt;Finally, here’s a video of this program running, &lt;/p&gt;
&lt;object width="425" height="344"&gt;
&lt;param value="http://www.youtube.com/v/r5Z4wxcamF0&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" name="movie" /&gt;
&lt;param value="true" name="allowFullScreen" /&gt;
&lt;param value="always" name="allowscriptaccess" /&gt;&lt;embed width="425" height="344" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="http://www.youtube.com/v/r5Z4wxcamF0&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx"&gt;Part 1&lt;/a&gt;, &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/06/12/applications-the-mathematics-of-movement-part-3.aspx"&gt;Part 3&lt;/a&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/140244.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx</guid>
            <pubDate>Sun, 06 Jun 2010 08:23:17 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/140244.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/140244.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/140244.aspx</trackback:ping>
        </item>
        <item>
            <title>Applications: The Mathematics of Movement, Part 1</title>
            <link>http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx</link>
            <description>&lt;p align="justify"&gt;Before you continue reading this post, a suggestion; if you haven’t read “&lt;a href="http://www.charlespetzold.com/phone/index.html"&gt;Programming Windows Phone 7 Series&lt;/a&gt;” by Charles Petzold, go read it. Now. If you find 150+ pages a little too long, at least go through Chapter 5, Principles of Movement, especially the section “A Brief Review of Vectors”. This post is largely inspired from this chapter.&lt;/p&gt;
&lt;p align="justify"&gt;At this point I assume you know what vectors are, how they are represented using the pair &lt;strong&gt;(x, y)&lt;/strong&gt;, what a unit vector is, and given a vector how you would normalize the vector to get a unit vector.&lt;/p&gt;
&lt;p align="justify"&gt;Our task in this post is simple, a marble is drawn at a point on the screen, the user clicks at a random point on the device, say (destX, destY), and our program makes the marble move towards that point and stop when it is reached. The tricky part of this task is the word “towards”, it adds a direction to our problem, making it seem a bit, err, esoteric? Making a marble bounce around the screen is simple, all you have to do is keep incrementing the X and Y co-ordinates by a certain amount and handle the boundary conditions. Here, however, we need to find out exactly how to increment the X and Y values, so that the marble appears to move towards the point where the user clicked. And this is where vectors can be so helpful.&lt;/p&gt;
&lt;p align="justify"&gt;The code I’ll show you here is not ideal, we’ll be working with C# on Windows Mobile 6.x, so there is no built-in vector class that I can use, though I could have written one and done all the math inside the class. I think it is trivial to the actual problem that we are trying to solve and can be done pretty easily once you know what’s going on behind the scenes. In other words, this is an excuse for me being lazy.&lt;/p&gt;
&lt;p align="justify"&gt;The first approach uses the function &lt;a href="http://en.wikipedia.org/wiki/Atan2"&gt;Atan2()&lt;/a&gt; to solve the “towards” part of the problem. Atan2() takes a point (x, y) as input, Atan2(y, x), note that y goes first, and then it returns an angle in radians. What angle you ask. Imagine a line from the origin (0, 0), to the point (x, y). The angle which Atan2 returns is the angle the positive X-axis makes with that line, measured clockwise. The figure below makes it clear, wiki has good &lt;a href="http://en.wikipedia.org/wiki/Atan2"&gt;details about Atan2()&lt;/a&gt;, give it a read. Atan is short for Arc Tangent, the angle, &lt;strong&gt;θ, &lt;/strong&gt; which it returns is such that, tan(&lt;strong&gt;θ&lt;/strong&gt;) = y/x&lt;strong&gt;&lt;br /&gt;
&lt;/strong&gt;&lt;/p&gt;
&lt;p align="justify"&gt; &lt;/p&gt;
&lt;p&gt;&lt;img width="516" height="302" border="0" src="http://geekswithblogs.net/images/geekswithblogs_net/TechTwaddle/WindowsLiveWriter/ApplicationsThemathematicsofmovementPart_10131/image_05f69599-75ab-4525-8e37-0cd11dcb1bff.png" alt="image" title="image" style="border: 0px none; display: inline;" /&gt;&lt;/p&gt;
&lt;p align="justify"&gt;The pair &lt;strong&gt;(x, y)&lt;/strong&gt; also denotes a vector. A vector whose magnitude is the length of that line, which is &lt;a href="http://en.wikipedia.org/wiki/Distance"&gt;Sqrt(x*x + y*y)&lt;/a&gt;, and a direction θ, as measured from positive X axis clockwise. If you’ve read that chapter from Charles Petzold’s book, this much should be clear. Now Sine and Cosine of the angle θ are special. Cosine(θ) divides x by the vectors length (adjacent by hypotenuse), thus giving us a unit vector along the X direction. And Sine(θ) divides y by the vectors length (opposite by hypotenuse), thus giving us a unit vector along the Y direction. Therefore the vector represented by the pair &lt;strong&gt;(cos(θ), sin(θ))&lt;/strong&gt;, is the unit vector (or normalization) of the vector &lt;strong&gt;(x, y)&lt;/strong&gt;. This unit vector has a length of 1 (&lt;em&gt;remember sin&lt;sup&gt;2&lt;/sup&gt;(θ) + cos&lt;sup&gt;2&lt;/sup&gt;(θ) = 1 ?&lt;/em&gt;), and a direction which is the same as vector &lt;strong&gt;(x, y)&lt;/strong&gt;. Now if I multiply this unit vector by some amount, then I will always get a point which is a certain distance away from the origin, but, more importantly, the point will always be on that line. For example, if I multiply the unit vector with the length of the line, I get the point (x, y). Thus, all we have to do to move the marble towards our destination point, is to multiply the unit vector by a certain amount each time and draw the marble, and the marble will magically move towards the click point.&lt;/p&gt;
&lt;p align="justify"&gt;Now time for some code. The application uses a timer based frame draw method to draw the marble on the screen. The timer is disabled initially and whenever the user clicks on the screen, the timer is enabled. The callback function for the timer follows the standard Update and Draw cycle.&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private double totLenToTravelSqrd = 0;     &lt;br /&gt;
private double startPosX = 0, startPosY = 0;       &lt;br /&gt;
private double destX = 0, destY = 0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void Form1_MouseUp(object sender, MouseEventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    destX = e.X;      &lt;br /&gt;
    destY = e.Y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double x = marble1.x - destX;     &lt;br /&gt;
    double y = marble1.y - destY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //calculate the total length to be travelled       &lt;br /&gt;
&lt;/font&gt;    totLenToTravelSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //store the start position of the marble       &lt;br /&gt;
&lt;/font&gt;    startPosX = marble1.x;      &lt;br /&gt;
    startPosY = marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    timer1.Enabled = true;     &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void timer1_Tick(object sender, EventArgs e)     &lt;br /&gt;
{      &lt;br /&gt;
    UpdatePosition();      &lt;br /&gt;
    DrawMarble();      &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;&lt;font color="#000080"&gt;Form1_MouseUp()&lt;/font&gt; method is called when ever the user touches and releases the screen. In this function we save the click point in destX and destY, this is the destination point for the marble and we also enable the timer. We store a few more values which we will use in the &lt;font color="#000080"&gt;UpdatePosition()&lt;/font&gt; method to detect when the marble has reached the destination and stop the timer. So we store the start position of the marble and the square of the total length to be travelled. I’ll leave out the term ‘sqrd’ when speaking of lengths from now on. The time out interval of the timer is set to 40ms, thus giving us a frame rate of about ~25fps. In the timer callback, we update the marble position and draw the marble. We know what &lt;font color="#000080"&gt;DrawMarble()&lt;/font&gt; does, so here, we’ll only look at how &lt;font color="#000080"&gt;UpdatePosition()&lt;/font&gt; is implemented;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;private void UpdatePosition()     &lt;br /&gt;
{      &lt;br /&gt;
&lt;font color="#408080"&gt;    //the vector (x, y)       &lt;br /&gt;
&lt;/font&gt;    double x = destX - marble1.x;      &lt;br /&gt;
    double y = destY - marble1.y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double incrX=0, incrY=0;     &lt;br /&gt;
    double distanceSqrd=0; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double speed = 6; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;&lt;font color="#408080"&gt;    //distance between destination and current position, before updating marble position       &lt;br /&gt;
&lt;/font&gt;    distanceSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    double angle = Math.Atan2(y, x);     &lt;br /&gt;
&lt;br /&gt;
    &lt;font color="#408080"&gt;//Cos and Sin give us the unit vector, 6 is the value we use to magnify the unit vector along the same direction       &lt;br /&gt;
&lt;/font&gt;    incrX = speed * Math.Cos(angle);      &lt;br /&gt;
    incrY = speed * Math.Sin(angle); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    marble1.x += incrX;     &lt;br /&gt;
    marble1.y += incrY; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//check for bounds&lt;/font&gt;      &lt;br /&gt;
    if ((int)marble1.x &amp;lt; MinX + marbleWidth / 2)      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MinX + marbleWidth / 2;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.x &amp;gt; (MaxX - marbleWidth / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.x = MaxX - marbleWidth / 2;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    if ((int)marble1.y &amp;lt; MinY + marbleHeight / 2)     &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MinY + marbleHeight / 2;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if ((int)marble1.y &amp;gt; (MaxY - marbleHeight / 2))      &lt;br /&gt;
    {      &lt;br /&gt;
        marble1.y = MaxY - marbleHeight / 2;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//distance between destination and current point, after updating marble position       &lt;br /&gt;
&lt;/font&gt;    x = destX - marble1.x;      &lt;br /&gt;
    y = destY - marble1.y;      &lt;br /&gt;
    double newDistanceSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//length from start point to current marble position       &lt;br /&gt;
&lt;/font&gt;    x = startPosX - (marble1.x);      &lt;br /&gt;
    y = startPosY - (marble1.y);      &lt;br /&gt;
    double lenTraveledSqrd = x * x + y * y; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;    &lt;font color="#408080"&gt;//check for end conditions       &lt;br /&gt;
&lt;/font&gt;    if ((int)lenTraveledSqrd &amp;gt;= (int)totLenToTravelSqrd)      &lt;br /&gt;
    {      &lt;br /&gt;
        System.Console.WriteLine("&lt;font color="#800000"&gt;Stopping because destination reached&lt;/font&gt;");      &lt;br /&gt;
        timer1.Enabled = false;      &lt;br /&gt;
    }      &lt;br /&gt;
    else if (Math.Abs((int)distanceSqrd - (int)newDistanceSqrd) &amp;lt; 4)      &lt;br /&gt;
    {      &lt;br /&gt;
        System.Console.WriteLine("&lt;font color="#800000"&gt;Stopping because no change in Old and New position&lt;/font&gt;");      &lt;br /&gt;
        timer1.Enabled = false;      &lt;br /&gt;
    } &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#000080"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Ok, so in this function, first we subtract the current marble position from the destination point to give us a vector. The first three lines of the function construct this vector &lt;strong&gt;(x, y)&lt;/strong&gt;. The vector &lt;strong&gt;(x, y)&lt;/strong&gt; has the same length as the line from (marble1.x, marble1.y) to (destX, destY) and is in the direction pointing from (marble1.x, marble1.y) to (destX, destY). Note that marble1.x and marble1.y denote the center point of the marble. Then we use Atan2() to get the angle which this vector makes with the positive X axis and use Cosine() and Sine() of that angle to get the unit vector along that same direction. We multiply this unit vector with 6, to get the values which the position of the marble should be incremented by. This variable, &lt;font color="#000080"&gt;speed&lt;/font&gt;, can be experimented with and determines how fast the marble moves towards the destination. After this, we check for bounds to make sure that the marble stays within the screen limits and finally we check for the end condition and stop the timer.&lt;/p&gt;
&lt;p align="justify"&gt;The end condition has two parts to it. The first case is the normal case, where the user clicks well inside the screen. Here, we stop when the total length travelled by the marble is greater than or equal to the total length to be travelled. Simple enough. The second case is when the user clicks on the very corners of the screen. Like I said before, the values marble1.x and marble1.y denote the center point of the marble. When the user clicks on the corner, the marble moves towards the point, and after some time tries to go outside of the screen, this is when the bounds checking comes into play and corrects the marble position so that the marble stays inside the screen. In this case the marble will never travel a distance of &lt;font color="#000080"&gt;totLenToTravelSqrd&lt;/font&gt;, because of the correction is its position. So here we detect the end condition when there is not much change in marbles position. I use the value 4 in the second condition above. After experimenting with a few values, 4 seemed to work okay. There is a small thing missing in the code above. In the normal case, case 1, when the update method runs for the last time, marble position over shoots the destination point. This happens because the position is incremented in steps (which are not small enough), so in this case too, we should have corrected the marble position, so that the center point of the marble sits exactly on top of the destination point. I’ll add this later and update the post.&lt;/p&gt;
&lt;p align="justify"&gt;This has been a pretty long post already, so I’ll leave you with a video of how this program looks while running. Notice in the video that the marble moves like a bot, without any grace what so ever. And that is because the speed of the marble is fixed at 6. In the next post we will see how to make the marble move a little more elegantly. And also, if Atan2(), Sine() and Cosine() are a little too much to digest, we’ll see how to achieve the same effect without using them, in the next to next post maybe. Ciao!&lt;/p&gt;
&lt;object width="425" height="344"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/qiQpnWZYEHE&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowscriptaccess" value="always" /&gt;&lt;embed width="425" height="344" src="http://www.youtube.com/v/qiQpnWZYEHE&amp;amp;hl=en_US&amp;amp;fs=1&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/06/06/applications-the-mathematics-of-movement-part-2.aspx"&gt;Part 2&lt;/a&gt;, &lt;a href="http://geekswithblogs.net/TechTwaddle/archive/2010/06/12/applications-the-mathematics-of-movement-part-3.aspx"&gt;Part 3&lt;/a&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/TechTwaddle/aggbug/140023.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>TechTwaddle</dc:creator>
            <guid>http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx</guid>
            <pubDate>Sun, 23 May 2010 06:00:19 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/TechTwaddle/comments/140023.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/TechTwaddle/archive/2010/05/23/applications-the-mathematics-of-movement-part-1.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/TechTwaddle/comments/commentRss/140023.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/TechTwaddle/services/trackbacks/140023.aspx</trackback:ping>
        </item>
    </channel>
</rss>
