<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eclipsed4utoo&#039;s Blog&#187; WP7</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/wp7/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Thu, 08 Sep 2011 17:09:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WP7 &#8211; Using IsolatedStorageSettings</title>
		<link>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 23:00:07 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>
		<category><![CDATA[wpdev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=698</guid>
		<description><![CDATA[In a previous tutorial, I wrote about using the Isolated Storage to store files on a WP7 device. Let&#8217;s say that instead of storing data, you wanted to store settings for your application. This where the IsolatedStorageSettings comes in. The IsolatedStorageSettings is a place to store Key/Value pairs of data. So let&#8217;s say that my [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/" target="_blank">previous tutorial</a>, I wrote about using the Isolated Storage to store files on a WP7 device. Let&#8217;s say that instead of storing data, you wanted to store settings for your application. This where the IsolatedStorageSettings comes in.</p>
<p>The <strong>IsolatedStorageSettings </strong>is a place to store Key/Value pairs of data.</p>
<p>So let&#8217;s say that my application used the Microsoft Ad control, but I wanted to be nice and give the user the ability to turn the ads on and off.</p>
<p>On my Settings screen, I would have a checkbox(<strong>AdsCheckbox</strong>) for allowing the ads. In code, I would save the value by doing this&#8230;</p>
<pre class="brush: csharp;">var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains(&quot;AllowAds&quot;))
    settings[&quot;AllowAds&quot;] = AdsCheckbox.IsChecked.Value;
else
    settings.Add(&quot;AllowAds&quot;, AdsCheckbox.IsChecked.Value);

settings.Save();</pre>
<p>If you will notice, the <strong>ApplicationSettings </strong>object does have a method that allows you to check to see if the key already exists in the Settings. An exception will be thrown if you try to add a key that already exists, so it&#8217;s necessary to do this check before assigning a value.</p>
<p>Also, as you can see, it&#8217;s very easy to get the value out of the settings&#8230;</p>
<pre class="brush: csharp;">// when the form opens, I want to set the Checkbox to being checked
//   or unchecked depending on the setting
var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains(&quot;AllowAds&quot;))
    AdsCheckbox.IsChecked = bool.Parse(settings[&quot;AllowAds&quot;].ToString());</pre>
<p>However, the IsolatedStorageSettings class isn&#8217;t only for simply datatypes. It can also store your custom class objects.</p>
<p><strong>Employee </strong>class</p>
<pre class="brush: csharp;">public class Employee
{
    public string FullName { get; set; }
    public decimal Salary { get; set; }
}</pre>
<p>Then on my form, I have a button to save an instance of the <strong>Employee </strong>class to the <strong>IsolatedStorageSettings</strong>&#8230;</p>
<pre class="brush: csharp;">private void btnSave_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp = new Employee()
    {
        FullName = &quot;John Doe&quot;,
        Salary = 250000
    };

    settings.Add(&quot;Employee1&quot;, emp);
    settings.Save();
}</pre>
<p>I also have a button that will load the object from the <strong>IsolatedStorageSettings</strong>&#8230;</p>
<pre class="brush: csharp;">private void btnLoad_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp;

    if (settings.TryGetValue&lt;Employee&gt;(&quot;Employee1&quot;, out emp))
    {
         MessageBox.Show(string.Format(&quot;Full Name: {0}\nSalary: {1:c}&quot;, emp.FullName, emp.Salary));
    }
}</pre>
<p>Notice that the <strong>IsolatedStorageSettings.ApplicationSettings</strong> class also has the <strong>TryGetValue </strong>method. This method behaves exactly like the <strong>TryParse </strong>methods of the .Net datatypes. It will return a boolean specifying whether the key was found and whether the conversion from object &#8211;&gt; Employee was successful. If it&#8217;s successful, it will return the instance as the out parameter.</p>
<p>After running the application, clicking the <strong>Save </strong>button, then clicking the <strong>Load </strong>button, we will get this message box&#8230;</p>
<p><img class="alignnone size-full wp-image-699" title="2" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2011/07/2.png" alt="2" width="332" height="170" /></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Working With Isolated Storage</title>
		<link>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 23:00:28 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>
		<category><![CDATA[wpdev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=692</guid>
		<description><![CDATA[In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it. So first, we need to add these [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it.</p>
<p>So first, we need to add these using statements to the top of the code(if they aren&#8217;t already there):</p>
<pre class="brush: csharp;">using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;</pre>
<p>Next we will have our form. Very basic. It has two <strong>Textboxes </strong>and a <strong>Button</strong>. The first <strong>TextBox </strong>will allow the user to type in some text. The <strong>Button </strong>will save the data to <strong>IsolatedStorage</strong>, then read the data from the file and put it into the second <strong>TextBox</strong>.</p>
<p>Here is the XAML:</p>
<pre class="brush: xml;">&lt;Grid x:Name=&quot;ContentGrid&quot; Grid.Row=&quot;1&quot;&gt;

    &lt;TextBox
        Name=&quot;txtText&quot;
        Height=&quot;128&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;8,71,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;460&quot;
        TextWrapping=&quot;Wrap&quot;
        FontSize=&quot;16&quot; /&gt;

    &lt;Button
        Name=&quot;btnSave&quot;
        Content=&quot;Save&quot;
        Click=&quot;btnSave_Click&quot;
        Height=&quot;115&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;62,203,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;336&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock1&quot;
        Text=&quot;Text to add to file:&quot;
        Height=&quot;30&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;24,49,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock2&quot;
        Text=&quot;Data currently in file:&quot;
        Height=&quot;30&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;24,342,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

    &lt;TextBox
        Name=&quot;txtCurrentText&quot;
        Height=&quot;128&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;6,364,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;460&quot;
        TextWrapping=&quot;Wrap&quot;
        FontSize=&quot;16&quot; /&gt;

&lt;/Grid&gt;</pre>
<p>So now we move to the code. We need to create an <strong>IsolatedStorageFile </strong>object:</p>
<pre class="brush: csharp;">using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{

}</pre>
<p>Since we will always be appending to the file, we need to check to make sure the file exists. If it doesn&#8217;t exist, we need to create it.</p>
<pre class="brush: csharp;">// we need to check to see if the file exists
if (!isoStorage.FileExists(fileName))
{
    // file doesn't exist...time to create it.
    isoStorage.CreateFile(fileName);
}</pre>
<p>Once we have done the <strong>FileExists </strong>check, we can now open the file. Once the file has been opened, we can now use the <strong>StreamWriter </strong>class to write to it.</p>
<pre class="brush: csharp;">// since we are appending to the file, we must use FileMode.Append
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
{
    // opens the file and writes to it.
    using (var fileStream = new StreamWriter(isoStream))
    {
        fileStream.WriteLine(txtText.Text);
    }
}</pre>
<p><span style="color: #ff0000;"><strong>Note: notice that we opened the file using FileMode.Append. Because we used this mode, we can&#8217;t read from the file, we can only write to it.</strong></span></p>
<p>So to read from the file, we will need to open the file again, this time using <strong>FileMode.Open</strong>. We are going to do this in another <strong>using </strong>block. Once the file is opened, we use the <strong>StreamReader </strong>class to read it.</p>
<pre class="brush: csharp;">// you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
//   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
//   we are simply reading the file, we use FileMode.Open
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
{
    // opens the file and reads it.
    using (var fileStream = new StreamReader(isoStream))
    {
        txtCurrentText.Text = fileStream.ReadToEnd();
    }
}</pre>
<p>And that&#8217;s it. If you run the application, type in the first textbox, hit Save, the data will show up in the bottom textbox. Do it again, and you will see that it has appended to the file.</p>
<p>As you can see, writing to Isolated Storage is really easy.</p>
<p>Here is the entire code for the form&#8230;</p>
<pre class="brush: csharp;">using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WP7IsoStorageDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string fileName = &quot;MyTextfile.txt&quot;;

            using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // we need to check to see if the file exists
                if (!isoStorage.FileExists(fileName))
                {
                    // file doesn't exist...time to create it.
                    isoStorage.CreateFile(fileName);
                }

                // since we are appending to the file, we must use FileMode.Append
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
                {
                    // opens the file and writes to it.
                    using (var fileStream = new StreamWriter(isoStream))
                    {
                        fileStream.WriteLine(txtText.Text);
                    }
                }

                // you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
                //   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
                //   we are simply reading the file, we use FileMode.Open
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
                {
                    // opens the file and reads it.
                    using (var fileStream = new StreamReader(isoStream))
                    {
                        txtCurrentText.Text = fileStream.ReadToEnd();
                    }
                }
            }
        }
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Using the ToggleSwitch</title>
		<link>http://eclipsed4utoo.com/blog/wp7-toggleswitch/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-toggleswitch/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 15:00:55 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[ToggleSwitch]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7 toolkit]]></category>
		<category><![CDATA[wp7dev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=547</guid>
		<description><![CDATA[This will be the first of a number of posts highlighting the new controls that have been added to the WP7 Toolkit.  It&#8217;s a really great set of tools that makes life easier for us developers. Download WP7 Toolkit So the first control I am going to talk about is the ToggleSwitch control.  It&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>This will be the first of a number of posts highlighting the new controls that have been added to the WP7 Toolkit.  It&#8217;s a really great set of tools that makes life easier for us developers.</p>
<p><a href="http://silverlight.codeplex.com/releases/view/55034" target="_blank">Download WP7 Toolkit</a></p>
<p>So the first control I am going to talk about is the <strong>ToggleSwitch </strong>control.  It&#8217;s a control that resembles a lightswitch and is a nice was of showing an On/Off or True/False setting instead of using a plain checkbox.</p>
<p>So let&#8217;s create a Windows Phone 7 project called <strong>ToggleSwitchDemo</strong>.</p>
<p>Open the Toolbox and find the <strong>ToggleSwitch </strong>control.  If it&#8217;s not listed, <strong>right-click</strong> on the <strong>Toolbox </strong>&#8211;&gt; <strong>Choose Items</strong> &#8211;&gt; then scroll down until you find the <strong>ToggleSwitch </strong>control in the list.</p>
<p>Once the control is in the <strong>Toolbox</strong>, drag it onto the page.  By default, it will look like this&#8230;.</p>
<p><img class="alignnone size-full wp-image-548" title="11-14-2010 10-21-33 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-10-21-33-AM.png" alt="11-14-2010 10-21-33 AM" width="248" height="410" /></p>
<p>Run the application using F5, then click on the ToggleSwitch.  It will change from Off to On.</p>
<p><img class="alignnone size-full wp-image-549" title="11-14-2010 10-24-16 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-10-24-16-AM.png" alt="11-14-2010 10-24-16 AM" width="221" height="370" /> <img class="alignnone size-full wp-image-550" title="11-14-2010 10-24-28 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-10-24-28-AM.png" alt="11-14-2010 10-24-28 AM" width="221" height="370" /></p>
<p>There are a number of options that are available for the text for the <strong>ToggleControl</strong>.  The <strong>Header </strong>property will change the heading of the control.  The <strong>Content </strong>property will set the content of the control(On/Off).</p>
<p>In one of my application, I used the ToggleSwitch to allow the user to turn on/off Ads that I have in the app.  So for me, I changed the <strong>Header </strong>to <strong>&#8220;Ads&#8221;</strong>, and since I default the ToggleSwitch to being On(<strong>IsChecked </strong>is <strong>True</strong>), I set the <strong>Content </strong>to <strong>&#8220;Ads are enabled</strong>&#8220;<strong>.</strong></p>
<p>The <strong>ToggleSwitch </strong>also allows you to handle when the control is checked and unchecked.  So here is how my <strong>ToggleSwitch </strong>would look in <strong>XAML</strong>&#8230;</p>
<pre class="brush: csharp;">
&lt;toolkit:ToggleSwitch
    Name=&quot;AdsToggleSwitch&quot;
    Header=&quot;Ads&quot;
    Content=&quot;Ads are enabled&quot;
    Height=&quot;111&quot;
    HorizontalAlignment=&quot;Left&quot;
    Margin=&quot;1,197,0,0&quot;
    VerticalAlignment=&quot;Top&quot;
    Width=&quot;456&quot;
    IsChecked=&quot;True&quot;
    Checked=&quot;AdsToggleSwitch_Checked&quot;
    Unchecked=&quot;AdsToggleSwitch_Unchecked&quot; /&gt;
</pre>
<p>Then the code for the <strong>Checked </strong>and <strong>Unchecked </strong>events&#8230;</p>
<pre class="brush: csharp;">
private void AdsToggleSwitch_Checked(object sender, RoutedEventArgs e)
{
     AdsToggleSwitch.Content = &quot;Ads are enabled&quot;;
}

private void AdsToggleSwitch_Unchecked(object sender, RoutedEventArgs e)
{
     AdsToggleSwitch.Content = &quot;Ads are disabled&quot;;
}
</pre>
<p>Now my control would look like this&#8230;</p>
<p><img class="alignnone size-full wp-image-554" title="11-14-2010 10-41-21 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-10-41-21-AM.png" alt="11-14-2010 10-41-21 AM" width="221" height="370" /> <img class="alignnone size-full wp-image-555" title="11-14-2010 10-41-30 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-10-41-30-AM.png" alt="11-14-2010 10-41-30 AM" width="221" height="370" /></p>
<p>The ToggleSwitch is a great control that allows your app to keep the &#8220;Metro&#8221; look and feel that a plain CheckBox wouldn&#8217;t allow you to do without custom coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-toggleswitch/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Using the Microsoft AdControl</title>
		<link>http://eclipsed4utoo.com/blog/wp7-microsoft-adcontrol/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-microsoft-adcontrol/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 17:00:23 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[AdControl]]></category>
		<category><![CDATA[Advertising]]></category>
		<category><![CDATA[Microsoft Advertising]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=529</guid>
		<description><![CDATA[For the Windows Phone 7 platform, Microsoft decided to roll-out it&#8217;s own mobile ad service.  This service is Microsoft Advertising PubCenter.  Along with the service, they also added a custom control that we developers can add to our Windows Phone 7 applications. First, we will need to download the Advertising SDK.  That can be done [...]]]></description>
			<content:encoded><![CDATA[<p>For the Windows Phone 7 platform, Microsoft decided to roll-out it&#8217;s own mobile ad service.  This service is <a href="https://pubcenter.microsoft.com" target="_blank">Microsoft Advertising PubCenter</a>.  Along with the service, they also added a custom control that we developers can add to our Windows Phone 7 applications.</p>
<p>First, we will need to download the Advertising SDK.  That can be done <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b0f00afc-9709-4cc2-ba2c-57728db6cbd6" target="_blank">from here</a>.  After downloading, install it as you would any other .msi, but you will want to note the install directory.  On Windows 7 64-bit, the install directory will be &#8220;<strong>C:\Program Files (x86)\Microsoft Advertising SDK for Windows Phone 7</strong>&#8220;.</p>
<p>So first, you will need to go to the PubCenter, create an account(which is free).  Once your account is created, login, then go to the Setup tab.  From here, you will want to register your mobile application and create an application ad unit.  It may take some time for the Ad Unit or application to be approved, so we are allowed to use some testing values for development purposes.</p>
<p><img class="alignnone size-full wp-image-533" title="11-13-2010 10-10-17 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-13-2010-10-10-17-AM.png" alt="11-13-2010 10-10-17 AM" width="755" height="237" /></p>
<p>You will want to note the application ID and the Ad Unit ID.  We will need both of these.</p>
<p>So let&#8217;s create a new WP7 project.  I just created a simple project and named it <strong>AdControlDemo</strong>.</p>
<p>Once the project has been created, open the Toolbox.  We are going to add a new tab for the <strong>AdControl</strong>.  Right-click somewhere in the Toolbox, then choose <strong>Add Tab</strong>.</p>
<p><img class="alignnone size-full wp-image-535" title="11-14-2010 9-47-51 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-47-51-AM.png" alt="11-14-2010 9-47-51 AM" width="201" height="209" /></p>
<p>I gave my tab the name<strong> Advertising Control</strong>.</p>
<p><img class="alignnone size-full wp-image-534" title="11-14-2010 9-46-38 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-46-38-AM.png" alt="11-14-2010 9-46-38 AM" width="200" height="182" /></p>
<p>Now right-click in the new Tab area, and click <strong>Choose Items</strong>.</p>
<p><img class="alignnone size-full wp-image-536" title="11-14-2010 9-49-28 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-49-28-AM.png" alt="11-14-2010 9-49-28 AM" width="207" height="194" /></p>
<p>When the Choose Items dialog comes up, click the <strong>Browse </strong>button.</p>
<p><img class="alignnone size-full wp-image-537" title="11-14-2010 9-50-48 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-50-48-AM.png" alt="11-14-2010 9-50-48 AM" width="424" height="317" /></p>
<p>Navigate to the install directory of the Advertising SDK that was installed.  You will want to add the <strong>Microsoft.Advertising.Mobile.UI.dll</strong> file.</p>
<p><img class="alignnone size-full wp-image-538" title="11-14-2010 9-52-34 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-52-34-AM.png" alt="11-14-2010 9-52-34 AM" width="400" height="307" /></p>
<p>You will now see that the <strong>AdControl </strong>has been added to our list of items.  Now click <strong>OK</strong>.</p>
<p><img class="alignnone size-full wp-image-539" title="11-14-2010 9-54-11 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-54-11-AM.png" alt="11-14-2010 9-54-11 AM" width="404" height="302" /></p>
<p>Now the <strong>AdControl </strong>has been added to our Toolbox.</p>
<p><img class="alignnone size-full wp-image-540" title="11-14-2010 9-55-24 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/11/11-14-2010-9-55-24-AM.png" alt="11-14-2010 9-55-24 AM" width="213" height="150" /></p>
<p>Now we are going to simply drag the control onto our screen.  Now open the <strong>Properties </strong>window, and you will see two properties for the <strong>AdUnitId </strong>and the <strong>ApplicationId</strong>.  For testing purposes, you can use <strong>&#8220;test_client&#8221;</strong> as the <strong>ApplicationId </strong>and <strong>&#8220;TextAd&#8221; </strong>as the <strong>AdUnitId</strong>.</p>
<p>So let&#8217;s run our application in the emulator by hitting F5, and you will see test ads showing up.</p>
<p>Now, once you setup your own application and ad unit and replaced the test values with your values, you will need to add this code to one of the initialization methods in the <strong>App.cs</strong>.  I put it at the bottom of the constructor.</p>
<pre class="brush: csharp;">
Microsoft.Advertising.Mobile.UI.AdControl.TestMode = false;
</pre>
<p>Without this code, the control will not show on the screen.</p>
<p>You now have advertising in your WP7 application.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-microsoft-adcontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Context Menu From Listbox</title>
		<link>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 11:58:40 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=428</guid>
		<description><![CDATA[In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox. Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it. To my surprise, even though the OS seems to [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).  So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].  Add these two using statements to the top of the code&#8230;
<pre class="brush: plain;"> using System.Collections.ObjectModel; using System.Windows.Threading; </pre>
<p>So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</p>
<pre class="brush: plain;"> public class Person {     public string FirstName { get; set; }     public string LastName { get; set; } } </pre>
<p>Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</p>
<pre class="brush: plain;"> &lt;ListBox      Name=&quot;lbNames&quot;      Height=&quot;240&quot;      HorizontalAlignment=&quot;Left&quot;      Margin=&quot;10,119,0,0&quot;      VerticalAlignment=&quot;Top&quot;      Width=&quot;460&quot;     ItemsSource=&quot;{Binding}&quot;&gt;                      &lt;ListBox.ItemTemplate&gt;         &lt;DataTemplate&gt;             &lt;StackPanel&gt;                 &lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;             &lt;/StackPanel&gt;         &lt;/DataTemplate&gt;     &lt;/ListBox.ItemTemplate&gt;                  &lt;/ListBox&gt; </pre>
<p>Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</p>
<pre class="brush: plain;"> public partial class MainPage : PhoneApplicationPage {     ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();      // Constructor     public MainPage()     {         InitializeComponent();          this.Loaded += new RoutedEventHandler(MainPage_Loaded);     }      void MainPage_Loaded(object sender, RoutedEventArgs e)     {         personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });         personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });         personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });          lbNames.ItemsSource = personList;     } } </pre>
<p>Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {  }  private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {  } </pre>
<p>We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</p>
<pre class="brush: plain;"> ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();  DispatcherTimer timer;  Person selectedPerson = null;  // Constructor public MainPage() {     InitializeComponent();      this.Loaded += new RoutedEventHandler(MainPage_Loaded);      timer = new DispatcherTimer();     timer.Tick += delegate(object s, EventArgs e)     {      }; } </pre>
<p>Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</p>
<pre class="brush: plain;"> &lt;Popup      x:Name=&quot;DeleteContextMenu&quot;      Height=&quot;200&quot;      Width=&quot;400&quot;&gt;      &lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;     &lt;ListBox          x:Name=&quot;lbDeleteContextMenu&quot;         Background=&quot;White&quot;         SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;          &lt;ListBoxItem             Content=&quot;Delete Person&quot;              Foreground=&quot;Red&quot;             FontSize=&quot;25&quot;             FontWeight=&quot;Bold&quot;/&gt;      &lt;/ListBox&gt;  &lt;/Popup&gt; </pre>
<p>And the event handler for selecting the Delete item&#8230;</p>
<pre class="brush: plain;"> private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {  } </pre>
<p>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.  So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {     // if there is no person selected, then there is no person to delete     //   no need to do any code if nothing is selected     if (selectedPerson == null)         return;      // gets the position of the mouse cursor to set the Margin     //    of the Popup to show at the mouse coordinates.  You     //    may need to tweak these values to get it to display in     //    the correct location.     Point position = e.GetPosition((UIElement)this);     DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);      // sets the interval to 1.1 seconds.  This means the user will need      //    to hold down on the screen for 1.1 seconds before we determine     //    to show the ContextMenu.     timer.Interval = TimeSpan.FromMilliseconds(1100);     timer.Start(); } </pre>
<p>Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {     // stop the timer when the user releases the screen     timer.Stop();      // sets the class level variable to the selected row     selectedPerson = lbNames.SelectedItem as Person; } </pre>
<p>Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</p>
<pre class="brush: plain;"> // Constructor public MainPage() {     InitializeComponent();      this.Loaded += new RoutedEventHandler(MainPage_Loaded);      timer = new DispatcherTimer();     timer.Tick += delegate(object s, EventArgs e)     {         // stop the timer so that it doesn't popup the Context menu again         timer.Stop();          // since we are using the same ListBox over and over, this will         //   make it so when the Context Menu is shown, there will be no         //   selected item from any previous showing of the Context Menu         lbDeleteContextMenu.SelectedIndex = -1;          // opens the Context Menu         DeleteContextMenu.IsOpen = true;     }; } </pre>
<p>Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</p>
<pre class="brush: plain;"> private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {     // in the Timer's Tick event, we set the SelectedIndex of the     //   Context Menu's Listbox back to -1.  However, this does     //   fire the SelectionChanged event.  This code will handle that.     if (lbDeleteContextMenu.SelectedIndex == -1)         return;      // closes the Context Menu     DeleteContextMenu.IsOpen = false;      if (selectedPerson == null)         return;      // removes the selected person from the list     personList.Remove(selectedPerson);      // since we are using an ObservableCollection, we do not have     //   to rebind the list to the ListBox.      selectedPerson = null; } </pre>
<p>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</p>
<div>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</div>
<div>So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].</div>
<div>Add these two using statements to the top of the code&#8230;</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;using System.Collections.ObjectModel;&lt;/div&gt;
&lt;div&gt;using System.Windows.Threading;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;public class Person&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;public string FirstName { get; set; }&lt;/div&gt;
&lt;div&gt;public string LastName { get; set; }&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;ListBox&lt;/div&gt;
&lt;div&gt;Name=&quot;lbNames&quot;&lt;/div&gt;
&lt;div&gt;Height=&quot;240&quot;&lt;/div&gt;
&lt;div&gt;HorizontalAlignment=&quot;Left&quot;&lt;/div&gt;
&lt;div&gt;Margin=&quot;10,119,0,0&quot;&lt;/div&gt;
&lt;div&gt;VerticalAlignment=&quot;Top&quot;&lt;/div&gt;
&lt;div&gt;Width=&quot;460&quot;&lt;/div&gt;
&lt;div&gt;ItemsSource=&quot;{Binding}&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;DataTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;StackPanel&gt;&lt;/div&gt;
&lt;div&gt;&lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;&lt;/div&gt;
&lt;div&gt;&lt;/StackPanel&gt;&lt;/div&gt;
&lt;div&gt;&lt;/DataTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;public partial class MainPage : PhoneApplicationPage&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;void MainPage_Loaded(object sender, RoutedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });&lt;/div&gt;
&lt;div&gt;lbNames.ItemsSource = personList;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div&gt;DispatcherTimer timer;&lt;/div&gt;
&lt;div&gt;Person selectedPerson = null;&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;};&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;Popup&lt;/div&gt;
&lt;div&gt;x:Name=&quot;DeleteContextMenu&quot;&lt;/div&gt;
&lt;div&gt;Height=&quot;200&quot;&lt;/div&gt;
&lt;div&gt;Width=&quot;400&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBox&lt;/div&gt;
&lt;div&gt;x:Name=&quot;lbDeleteContextMenu&quot;&lt;/div&gt;
&lt;div&gt;Background=&quot;White&quot;&lt;/div&gt;
&lt;div&gt;SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBoxItem&lt;/div&gt;
&lt;div&gt;Content=&quot;Delete Person&quot;&lt;/div&gt;
&lt;div&gt;Foreground=&quot;Red&quot;&lt;/div&gt;
&lt;div&gt;FontSize=&quot;25&quot;&lt;/div&gt;
&lt;div&gt;FontWeight=&quot;Bold&quot;/&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div&gt;&lt;/Popup&gt;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>And the event handler for selecting the Delete item&#8230;</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</div>
<div>So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// if there is no person selected, then there is no person to delete&lt;/div&gt;
&lt;div&gt;//   no need to do any code if nothing is selected&lt;/div&gt;
&lt;div&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// gets the position of the mouse cursor to set the Margin&lt;/div&gt;
&lt;div&gt;//    of the Popup to show at the mouse coordinates.  You&lt;/div&gt;
&lt;div&gt;//    may need to tweak these values to get it to display in&lt;/div&gt;
&lt;div&gt;//    the correct location.&lt;/div&gt;
&lt;div&gt;Point position = e.GetPosition((UIElement)this);&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);&lt;/div&gt;
&lt;div&gt;// sets the interval to 1.1 seconds.  This means the user will need&lt;/div&gt;
&lt;div&gt;//    to hold down on the screen for 1.1 seconds before we determine&lt;/div&gt;
&lt;div&gt;//    to show the ContextMenu.&lt;/div&gt;
&lt;div&gt;timer.Interval = TimeSpan.FromMilliseconds(1100);&lt;/div&gt;
&lt;div&gt;timer.Start();&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// stop the timer when the user releases the screen&lt;/div&gt;
&lt;div&gt;timer.Stop();&lt;/div&gt;
&lt;div&gt;// sets the class level variable to the selected row&lt;/div&gt;
&lt;div&gt;selectedPerson = lbNames.SelectedItem as Person;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// stop the timer so that it doesn't popup the Context menu again&lt;/div&gt;
&lt;div&gt;timer.Stop();&lt;/div&gt;
&lt;div&gt;// since we are using the same ListBox over and over, this will&lt;/div&gt;
&lt;div&gt;//   make it so when the Context Menu is shown, there will be no&lt;/div&gt;
&lt;div&gt;//   selected item from any previous showing of the Context Menu&lt;/div&gt;
&lt;div&gt;lbDeleteContextMenu.SelectedIndex = -1;&lt;/div&gt;
&lt;div&gt;// opens the Context Menu&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.IsOpen = true;&lt;/div&gt;
&lt;div&gt;};&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// in the Timer's Tick event, we set the SelectedIndex of the&lt;/div&gt;
&lt;div&gt;//   Context Menu's Listbox back to -1.  However, this does&lt;/div&gt;
&lt;div&gt;//   fire the SelectionChanged event.  This code will handle that.&lt;/div&gt;
&lt;div&gt;if (lbDeleteContextMenu.SelectedIndex == -1)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// closes the Context Menu&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.IsOpen = false;&lt;/div&gt;
&lt;div&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// removes the selected person from the list&lt;/div&gt;
&lt;div&gt;personList.Remove(selectedPerson);&lt;/div&gt;
&lt;div&gt;// since we are using an ObservableCollection, we do not have&lt;/div&gt;
&lt;div&gt;//   to rebind the list to the ListBox.&lt;/div&gt;
&lt;div&gt;selectedPerson = null;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</div>
<p>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</p>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Add these two using statements to the top of the code&#8230;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;using System.Collections.ObjectModel;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;using System.Windows.Threading;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public class Person&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public string FirstName { get; set; }&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public string LastName { get; set; }&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Name=&quot;lbNames&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Height=&quot;240&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;HorizontalAlignment=&quot;Left&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Margin=&quot;10,119,0,0&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;VerticalAlignment=&quot;Top&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Width=&quot;460&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ItemsSource=&quot;{Binding}&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;DataTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;StackPanel&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/StackPanel&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/DataTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public partial class MainPage : PhoneApplicationPage&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;void MainPage_Loaded(object sender, RoutedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;lbNames.ItemsSource = personList;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DispatcherTimer timer;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Person selectedPerson = null;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;};&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;Popup&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;x:Name=&quot;DeleteContextMenu&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Height=&quot;200&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Width=&quot;400&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;x:Name=&quot;lbDeleteContextMenu&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Background=&quot;White&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBoxItem&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Content=&quot;Delete Person&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Foreground=&quot;Red&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;FontSize=&quot;25&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;FontWeight=&quot;Bold&quot;/&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/Popup&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">And the event handler for selecting the Delete item&#8230;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// if there is no person selected, then there is no person to delete&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   no need to do any code if nothing is selected&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// gets the position of the mouse cursor to set the Margin&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    of the Popup to show at the mouse coordinates.  You&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    may need to tweak these values to get it to display in&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    the correct location.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Point position = e.GetPosition((UIElement)this);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// sets the interval to 1.1 seconds.  This means the user will need&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    to hold down on the screen for 1.1 seconds before we determine&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    to show the ContextMenu.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Interval = TimeSpan.FromMilliseconds(1100);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Start();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// stop the timer when the user releases the screen&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Stop();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// sets the class level variable to the selected row&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;selectedPerson = lbNames.SelectedItem as Person;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// stop the timer so that it doesn't popup the Context menu again&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Stop();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// since we are using the same ListBox over and over, this will&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   make it so when the Context Menu is shown, there will be no&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   selected item from any previous showing of the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;lbDeleteContextMenu.SelectedIndex = -1;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// opens the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.IsOpen = true;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;};&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// in the Timer's Tick event, we set the SelectedIndex of the&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   Context Menu's Listbox back to -1.  However, this does&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   fire the SelectionChanged event.  This code will handle that.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (lbDeleteContextMenu.SelectedIndex == -1)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// closes the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.IsOpen = false;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// removes the selected person from the list&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Remove(selectedPerson);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// since we are using an ObservableCollection, we do not have&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   to rebind the list to the ListBox.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;selectedPerson = null;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</div>
<p>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.</p>
<p>To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</p>
<div>So I decided to roll my own using the <strong>MouseLeftButtonDown </strong>and <strong>MouseLeftButtonUp </strong>events, and a <strong>DispatcherTimer</strong>.</div>
<div>Add these two using statements to the top of the code&#8230;</div>
<div>
<pre class="brush: csharp;">
using System.Collections.ObjectModel;
using System.Windows.Threading;
</pre>
</div>
<p>So first, we need our initial code to get the <strong>ListBox </strong>populated.  I created a <strong>Person </strong>class just for simplistic reasons.</p>
<div>
<pre class="brush: csharp;">
public class Person
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}
</pre>
</div>
<p>Next, I will set up the <strong>ListBox </strong>to simply show the <strong>FirstName</strong> of the <strong>Person</strong>.</p>
<div>
<pre class="brush: csharp;">
&lt;ListBox
     Name=&quot;lbNames&quot;
     Height=&quot;240&quot;
     HorizontalAlignment=&quot;Left&quot;
     Margin=&quot;10,119,0,0&quot;
     VerticalAlignment=&quot;Top&quot;
     Width=&quot;460&quot;
     ItemsSource=&quot;{Binding}&quot;&gt;

     &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
               &lt;StackPanel&gt;
                    &lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;
               &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
     &lt;/ListBox.ItemTemplate&gt;
&lt;/ListBox&gt;
</pre>
</div>
<p>Now I will create an generic <strong>ObservableCollection </strong>object to store the <strong>Person </strong>objects.  This will be a class level variable since it will be accessed from multiple events.</p>
<div>
<pre class="brush: csharp;">public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });
        personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });
        personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });

        lbNames.ItemsSource = personList;
    }
}</pre>
</div>
<p>Now I need to create the event handlers for the <strong>MouseLeftButtonDown </strong>and <strong>MouseLeftButtonUp</strong> events.  This can simply be done by using the <strong>Events </strong>list from the <strong>Properties </strong>window in the designer.</p>
<div>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

}

private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

}</pre>
</div>
<p>We now need to create two class level objects: a <strong>DispatcherTimer </strong>object, and a <strong>Person </strong>object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</p>
<div>
<pre class="brush: csharp;">ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();

DispatcherTimer timer;

Person selectedPerson = null;

// Constructor
public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    timer = new DispatcherTimer();
    timer.Tick += delegate(object s, EventArgs e)
    {

    };
}</pre>
</div>
<p>Next, we will add a <strong>Popup </strong>in XAML.  This can go above or below the <strong>ListBox </strong>that is currently holding the names.</p>
<div>
<pre class="brush: csharp;">&lt;Popup
    x:Name=&quot;DeleteContextMenu&quot;
    Height=&quot;200&quot;
    Width=&quot;400&quot;&gt;

    &lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;
    &lt;ListBox
        x:Name=&quot;lbDeleteContextMenu&quot;
        Background=&quot;White&quot;
        Selectionchanged=&quot;DeleteContextMenu_Selectionchanged&quot;&gt;

        &lt;ListBoxItem
            Content=&quot;Delete Person&quot;
            Foreground=&quot;Red&quot;
            FontSize=&quot;25&quot;
            FontWeight=&quot;Bold&quot;/&gt;

    &lt;/ListBox&gt;

&lt;/Popup&gt;</pre>
</div>
<p>And the event handler for selecting the Delete item&#8230;</p>
<div>
<pre class="brush: csharp;">private void DeleteContextMenu_Selectionchanged(object sender, System.Windows.Controls.SelectionchangedEventArgs e)
{

}</pre>
</div>
<p>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</p>
<p>So now we move to our code.  First we are going to handle the <strong>MouseLeftButtonDown </strong>event.</p>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // if there is no person selected, then there is no person to delete
    //   no need to do any code if nothing is selected
    if (selectedPerson == null)
        return;

    // gets the position of the mouse cursor to set the Margin
    //    of the Popup to show at the mouse coordinates.  You
    //    may need to tweak these values to get it to display in
    //    the correct location.
    Point position = e.GetPosition((UIElement)this);
    DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);

    // sets the interval to 1.1 seconds.  This means the user will need
    //    to hold down on the screen for 1.1 seconds before we determine
    //    to show the ContextMenu.
    timer.Interval = TimeSpan.FromMilliseconds(1100);
    timer.Start();
}</pre>
<p><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">Next, we will do our code for the <strong>MouseLeftButtonUp</strong> event</span></p>
<div>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // stop the timer when the user releases the screen
    timer.Stop();

    // sets the class level variable to the selected row
    selectedPerson = lbNames.SelectedItem as Person;
}</pre>
</div>
<p>Now for our code in the <strong>Tick </strong>event for the <strong>Timer</strong>.</p>
<div>
<pre class="brush: csharp;">// Constructor
public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    timer = new DispatcherTimer();
    timer.Tick += delegate(object s, EventArgs e)
    {
        // stop the timer so that it doesn't popup the Context menu again
        timer.Stop();

        // since we are using the same ListBox over and over, this will
        //   make it so when the Context Menu is shown, there will be no
        //   selected item from any previous showing of the Context Menu
        lbDeleteContextMenu.SelectedIndex = -1;

        // opens the Context Menu
        DeleteContextMenu.IsOpen = true;
    };
}</pre>
</div>
<p>Last, we have our code from the <strong>SelectionChanged </strong>event for the <strong>ListBox </strong>that is part of the Context Menu.</p>
<div>
<pre class="brush: csharp;">private void DeleteContextMenu_Selectionchanged(object sender, System.Windows.Controls.SelectionchangedEventArgs e)
{
    // in the Timer's Tick event, we set the SelectedIndex of the
    //   Context Menu's Listbox back to -1.  However, this does
    //   fire the Selectionchanged event.  This code will handle that.
    if (lbDeleteContextMenu.SelectedIndex == -1)
        return;

    // closes the Context Menu
    DeleteContextMenu.IsOpen = false;

    if (selectedPerson == null)
        return;

    // removes the selected person from the list
    personList.Remove(selectedPerson);

    // since we are using an ObservableCollection, we do not have
    //   to rebind the list to the ListBox.

    selectedPerson = null;
}</pre>
</div>
<p>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF RIA Services &#8211; Error When Moving Service To Server</title>
		<link>http://eclipsed4utoo.com/blog/wcf-ria-services-error-moving-service-server/</link>
		<comments>http://eclipsed4utoo.com/blog/wcf-ria-services-error-moving-service-server/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 21:40:45 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[RIA Services]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=356</guid>
		<description><![CDATA[I recently tried publishing a WCF RIA Service to my server, and came across an error.  I was using all the latest Windows Phone 7 and RIA Services Tools, and targeting the .Net 4 framework.  However, since I have both .Net 3.5 and .Net 4 installed on my server, I ran into this error&#8230; &#8220;System.Configuration.ConfigurationErrorsException: [...]]]></description>
			<content:encoded><![CDATA[<p>I recently tried publishing a WCF RIA Service to my server, and came across an error.  I was using all the latest Windows Phone 7 and RIA Services Tools, and targeting the .Net 4 framework.  However, since I have both .Net 3.5 and .Net 4 installed on my server, I ran into this error&#8230;</p>
<p><img class="alignnone size-full wp-image-359" title="WCFError1" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/06/WCFError1.PNG" alt="WCFError1" width="1254" height="624" /></p>
<p><strong><em>&#8220;System.Configuration.ConfigurationErrorsException: The value for the &#8216;compilerVersion&#8217; attribute in the provider options must be &#8216;v4.0&#8242; or later if you are compiling for version 4.0 or later of the .NET Framework. To compile this Web application for version 3.5 or earlier of the .NET Framework, remove the &#8216;targetFramework&#8217; attribute from the &lt;compilation&gt; element of the Web.config file.&#8221;</em></strong></p>
<p>It threw me for a loop because it worked fine in Visual Studio 2010 when testing on my development PC, but when moving it to the server, it had this problem.</p>
<p>The fix was fairly easy, once I was able to find it.  Basically, you need to add the a section to the web.config that doesn&#8217;t get added when adding the RIA Services DomainService to the project.</p>
<pre>
<pre class="brush: xml;">
&lt;system.codedom&gt;
  &lt;compilers&gt;
     &lt;compiler language=&quot;c#;cs;csharp&quot; extension=&quot;.cs&quot; warningLevel=&quot;4&quot; type=&quot;Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot;&gt;
       &lt;providerOption name=&quot;CompilerVersion&quot; value=&quot;v4.0&quot; /&gt;
       &lt;providerOption name=&quot;WarnAsError&quot; value=&quot;false&quot; /&gt;
     &lt;/compiler&gt;
  &lt;/compilers&gt;
&lt;/system.codedom&gt;
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wcf-ria-services-error-moving-service-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

