<?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>VerySoftware Blog</title>
	<atom:link href="http://verysoftware.co.uk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://verysoftware.co.uk/blog</link>
	<description></description>
	<lastBuildDate>Thu, 22 Sep 2011 13:53:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Introducing Tunee</title>
		<link>http://verysoftware.co.uk/blog/introducing-tunee/</link>
		<comments>http://verysoftware.co.uk/blog/introducing-tunee/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 13:47:46 +0000</pubDate>
		<dc:creator>The VerySoftware Team</dc:creator>
				<category><![CDATA[Marketing]]></category>
		<category><![CDATA[itunes]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[share]]></category>
		<category><![CDATA[spotify]]></category>
		<category><![CDATA[tunee]]></category>
		<category><![CDATA[zune]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=372</guid>
		<description><![CDATA[Music is something we at VerySoftware love. Whether it&#8217;s music to work to, music to relax to, music to dance to or music to rock out to, we believe that there is a tune for every moment. Most importantly, we &#8230; <a href="http://verysoftware.co.uk/blog/introducing-tunee/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://tun.ee/" target="_blank"><img class="alignnone size-full wp-image-373" style="margin-top: 20px; margin-bottom: 20px;" title="tunee – share the music" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/09/tunee-logo.png" alt="" width="175" height="75" /></a></p>
<p>Music is something we at VerySoftware love. Whether it&#8217;s music to work to, music to relax to, music to dance to or music to rock out to, we believe that there is a tune for every moment. Most importantly, we feel that music is something to be shared. This is the inspiration behind our latest creation – <strong><a href="http://tun.ee" target="_blank">Tunee</a></strong>.</p>
<p><span id="more-372"></span></p>
<p>There are many services available to purchase or stream music over the internet, but while this has made getting access to music easier than ever, it&#8217;s made it hard to share music between people with preferences for different services. Tunee allows you to forget about all that and create a single link (or URL) to share a piece of music with friends who can then instantly access the music on the service of their choice.</p>
<h2>How Does It Work</h2>
<table style="border-style: none; margin-left: -20px;" border="0" cellspacing="4" cellpadding="0">
<tbody>
<tr>
<td style="border-style: none; padding-right: 15px;"><img class="alignnone size-full wp-image-375" title="tunee step 1 – paste your long url or search for music" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/09/tunee-step-1.png" alt="" width="194" height="135" /></td>
<td style="border-style: none; padding-right: 15px;"><img class="size-full wp-image-376" title="tunee step 2 – share the tunee link with your friends" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/09/tunee-step-2.png" alt="" width="192" height="135" /></td>
<td style="border-style: none; padding-right: 15px;"><img class="alignnone size-full wp-image-377" title="tunee step 3 – your friends enjoy the music in any app they like" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/09/tunee-step-3.png" alt="" width="192" height="135" /></td>
</tr>
<tr>
<td style="border-style: none;">
<div style="margin-left: 15px;"><em>paste your long url</em><br />
<em>or search for music</em></div>
</td>
<td style="border-style: none;">
<div style="margin-left: 15px;"><em>share the <a href="http://tun.ee/J" target="_blank">tunee link</a></em><br />
<em>with your friends</em></div>
</td>
<td style="border-style: none;">
<div style="margin-left: 15px;"><em>your friends enjoy the music</em><br />
<em>in any app they like</em></div>
</td>
</tr>
</tbody>
</table>
<p>The best way to see how Tunee works is to give it a try, so head over to <a href="http://tun.ee/">http://tun.ee</a> and take a look. There are two ways to start using Tunee: either search for the artist/album/track you want to share from the Tunee homepage or paste in a &#8216;share&#8217; link/URL from the music service of your choice (i.e. <a href="http://open.spotify.com/album/7Gu2d2XmbFFQQowcOEVYeA">http://open.spotify.com/album/7Gu2d2XmbFFQQowcOEVYeA</a>) and then hit “Go”.</p>
<p>Tunee will generate a short link for you (i.e. <a href="http://tun.ee/1o">http://tun.ee/1o</a>), which can then be Tweeted, Liked, G+&#8217;d or simply copy/pasted to your friends and followers. When your friends click the link they&#8217;re presented with a list of available services to access that piece of music on, so they can click their favourite and start listening. It&#8217;s as simple as that!</p>
<p><a href="http://tun.ee/" target="_blank"><img class="alignnone size-full wp-image-374" title="tunee screenshot" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/09/tunee-screenshot.png" alt="" width="640" height="442" /></a></p>
<h2>The Future</h2>
<p>Tunee has only just been released in Alpha and we&#8217;re still hard at work on it. Over the next few weeks and months we are planning to support additional services (such as Last.FM, Pandora, Amazon MP3, Grooveshark, etc.) where possible as well as iron out any kinks that pop up along the way.</p>
<p>We&#8217;re also working out the best way to allow 3rd Parties to use Tunee via an API, so if you&#8217;re interested in that give us a shout. If you have any requests for additional services, features or just general comments please send us a mail to <a href="mailto:tunee@verysoftware.co.uk">tunee@verysoftware.co.uk</a></p>
<p><strong><em>Go forth and “share the music”, then let us know what you think.</em></strong></p>
<p><em> </em></p>
<p><em> </em></p>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/introducing-tunee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 App Settings Persistence</title>
		<link>http://verysoftware.co.uk/blog/wp7-app-settings-persistence/</link>
		<comments>http://verysoftware.co.uk/blog/wp7-app-settings-persistence/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 08:30:38 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[UKTechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=359</guid>
		<description><![CDATA[The final part of my series of blog posts covering the code tips and tricks I shared at UK Tech.Days 2011, is about providing users with an improved experience when they are forced to re-install your application, whether that be &#8230; <a href="http://verysoftware.co.uk/blog/wp7-app-settings-persistence/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The final part of my <a href="http://verysoftware.co.uk/blog/tag/uktechdays">series of blog posts</a> covering the code tips and tricks I shared at <a href="http://uktechdays.cloudapp.net/techdays-live/blending-delicious-user-experiences-for-windows-phone-7.aspx">UK Tech.Days 2011</a>, is about providing users with an improved experience when they are forced to re-install your application, whether that be due to them migrating to a new phone, or a bug forcing them to uninstall it.</p>
<p>So what&#8217;s the problem? Well when a user reinstalls your app they have to start from scratch, any settings/customisations they make to your app are lost and this barrier to re-entry can sometimes force users to simply go elsewhere. In an ideal world a user should be presented with the option to restore their settings/customisations to you app when they load it for the first time after the uninstall/on the new device. Obviously any settings stored in Isolated Storage are lost so if you want to provide this experience you will need to save these settings in the cloud.</p>
<p><span id="more-359"></span></p>
<p>The process is quite simple, when the app is running a &#8216;backup&#8217; of the user&#8217;s settings are sent to the cloud along with the User ID. Whenever the application loads it checks the isolated storage for the settings, meaning that an app can have fast and offline loading, but if no settings are found locally then a request for the settings matching the current User ID is sent to the cloud. If settings for that user are found in the cloud then they are returned and the application should prompt the user to ask if they want these settings restored. A key point here is to ask the user at this point for 2 reasons, firstly the user may have intentionally uninstalled the application so that they can start from scratch, or the user&#8217;s settings may be causing the bug which forced them to uninstall it to start fresh. Thanks to the simplicity of these steps this code can easily be added to any existing application without any major refactors of code.</p>
<p>At this point many people will say that while this process sounds simple the &#8216;scary&#8217; part is the use of the world &#8216;cloud&#8217;, as cloud services can be complex and expensive which often doesn&#8217;t make sense for a small phone application. However I will demonstrate below that implementing a simple WCF Web Service like this is very simple and is lightweight enough to run on the lowest cost Azure instance (or even share an instance with other cloud services you may be running). For more information on the cost of Azure hosting take a look <a href="http://www.microsoft.com/windowsazure/pricing/">here</a>, or if you are a startup I&#8217;d highly recommend looking into <a href="http://www.microsoft.com/bizspark/">Microsoft&#8217;s BizSpark scheme</a> in which you can get lots of Azure usage (and may other benefits and Microsoft software) for free!</p>
<h2>Implementation &#8211; Client Side</h2>
<p>The implementation of this consists of 2 parts, the client side and the server/cloud side. All code snippets are taken from our TechDays Sample app and are available <a href="http://verysoftware.co.uk/talks/techdays/VerySoftware-TechDays2011-SampleAppSourceCode.zip">here</a>, in this sample the only user settings we have is the list of &#8216;followed&#8217; sessions, but you should be able to see how this can be extended to include whatever settings your app needs.</p>
<p>First, in the background function where we load our settings from Isolated Storage and then apply them, we need to add in the extra logic to fire off a simple Web Service call if no settings are loaded. Then once the Web Service call returns we need to respond appropriatley as explained above. The code to do this is shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> BackgroundLoadFavouriteSessions<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DoWorkEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var favIDs <span style="color: #008000;">=</span> LoadFavouriteSessionsFromFile<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>favIDs <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// We failed when loading from file so check the cloud store</span>
        CloudUserDataServiceClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CloudUserDataServiceClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        client<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStoredTechDaysUserDataCompleted</span> <span style="color: #008000;">+=</span> client_GetStoredTechDaysUserDataCompleted<span style="color: #008000;">;</span>
        client<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStoredTechDaysUserDataAsync</span><span style="color: #008000;">&#40;</span>HelperFuncs<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Loaded from file successfully so use this data</span>
        SetFavourites<span style="color: #008000;">&#40;</span>GetSessionListFromIDs<span style="color: #008000;">&#40;</span>favIDs<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #6666cc; font-weight: bold;">void</span> client_GetStoredTechDaysUserDataCompleted<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, GetStoredTechDaysUserDataCompletedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Would you like to restore your settings for this app?&quot;</span>, <span style="color: #666666;">&quot;No settings found&quot;</span>, MessageBoxButton<span style="color: #008000;">.</span><span style="color: #0000FF;">OKCancel</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> MessageBoxResult<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var favouriteSessionList <span style="color: #008000;">=</span> GetSessionListFromIDString<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            SetFavourites<span style="color: #008000;">&#40;</span>favouriteSessionList<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Also at some other point while the application is running we want to send the app&#8217;s current settings off to the Web Service, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SaveFavouritesToCloud<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> favIDString<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    TechDaysUserData saveData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TechDaysUserData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        UserID <span style="color: #008000;">=</span> HelperFuncs<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span>,
        FavouriteIDs <span style="color: #008000;">=</span> favIDString
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    CloudUserDataServiceClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CloudUserDataServiceClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    client<span style="color: #008000;">.</span><span style="color: #0000FF;">StoreTechDaysUserDataAsync</span><span style="color: #008000;">&#40;</span>saveData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Both these code blocks are fairly standard Web Service calls, the only potentially unknown thing here is how to retrieve the user&#8217;s ID. This is shown in the following block of code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> cachedUserID <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> UserID
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>cachedUserID <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span> anid <span style="color: #008000;">=</span> UserExtendedProperties<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ANID&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>anid <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                cachedUserID <span style="color: #008000;">=</span> anid<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">32</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #008000;">&#123;</span>
                cachedUserID <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;EMULATOR_USER&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> cachedUserID<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is just a wrapper around the Microsoft API to retrieve the User Extended Property but we add some caching here to improve performance, and strip away part of the string to provide an anonymous User ID. More information on this is available via the <a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.info.userextendedproperties.getvalue(v=VS.92).aspx">MSDN page</a>. That is all the client side code that you will need to add into your existing application, now onto the server side.</p>
<h2>Implementation &#8211; Cloud Side</h2>
<p>If you have ever used Azure Table Storage before the following code should look very simple, if you haven&#8217;t then I&#8217;d suggest having a quick read up on it as I won&#8217;t go into great detail about it here. The same also applies here for WCF Web Services.</p>
<p>The first thing we want to do on the Azure/Cloud side is create the structures for holding our User Settings, the code below describes the Azure Table Storage Table and Entity that will be used to hold this data:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TechDaysDataEntryContext <span style="color: #008000;">:</span> TableServiceContext
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> TableName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;TechDaysDataEntrys&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> TechDaysDataEntryContext<span style="color: #008000;">&#40;</span>CloudStorageAccount account<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span>account<span style="color: #008000;">.</span><span style="color: #0000FF;">TableEndpoint</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, account<span style="color: #008000;">.</span><span style="color: #0000FF;">Credentials</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> IQueryable<span style="color: #008000;">&lt;</span>TechDaysDataEntry<span style="color: #008000;">&gt;</span> TechDaysDataEntrys <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CreateQuery</span><span style="color: #008000;">&lt;</span>TechDaysDataEntry<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddTechDaysDataEntry<span style="color: #008000;">&#40;</span>TechDaysDataEntry newEntry<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddObject</span><span style="color: #008000;">&#40;</span>TableName, newEntry<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TechDaysDataEntry <span style="color: #008000;">:</span> TableServiceEntity
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UserID
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> PartitionKey<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        set <span style="color: #008000;">&#123;</span> RowKey <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> PartitionKey <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FavouriteIDs <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> TechDaysDataEntry<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> TechDaysDataEntry<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UserID, <span style="color: #6666cc; font-weight: bold;">string</span> inFavouriteIDs<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        PartitionKey <span style="color: #008000;">=</span> UserID<span style="color: #008000;">;</span>
        RowKey <span style="color: #008000;">=</span> UserID<span style="color: #008000;">;</span>
        FavouriteIDs <span style="color: #008000;">=</span> inFavouriteIDs<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>That is the Azure Table Storage code out of the way, the next thing we need to do is write the WCF Web Service to provide access to/from the storage. The code for those two functions is below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> TechDaysUserData GetStoredTechDaysUserData<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> userID<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var storageAccount <span style="color: #008000;">=</span> GetStorageAccount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var tddeContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TechDaysDataEntryContext<span style="color: #008000;">&#40;</span>storageAccount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreResourceNotFoundException</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    var result <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> entry <span style="color: #0600FF; font-weight: bold;">in</span> tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">TechDaysDataEntrys</span>
                    <span style="color: #0600FF; font-weight: bold;">where</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">PartitionKey</span> <span style="color: #008000;">==</span> userID <span style="color: #008000;">&amp;&amp;</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">RowKey</span> <span style="color: #008000;">==</span> userID
                    <span style="color: #0600FF; font-weight: bold;">select</span> entry<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var entry <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var userData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TechDaysUserData<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            UserID <span style="color: #008000;">=</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span>,
            FavouriteIDs <span style="color: #008000;">=</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span>
        <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> userData<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> StoreTechDaysUserData<span style="color: #008000;">&#40;</span>TechDaysUserData userData<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var storageAccount <span style="color: #008000;">=</span> GetStorageAccount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var tddeContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TechDaysDataEntryContext<span style="color: #008000;">&#40;</span>storageAccount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreResourceNotFoundException</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    var result <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span> entry <span style="color: #0600FF; font-weight: bold;">in</span> tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">TechDaysDataEntrys</span>
                    <span style="color: #0600FF; font-weight: bold;">where</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">PartitionKey</span> <span style="color: #008000;">==</span> userData<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span> <span style="color: #008000;">&amp;&amp;</span> entry<span style="color: #008000;">.</span><span style="color: #0000FF;">RowKey</span> <span style="color: #008000;">==</span> userData<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span>
                    <span style="color: #0600FF; font-weight: bold;">select</span> entry<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var entry <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TechDaysDataEntry<span style="color: #008000;">&#40;</span>userData<span style="color: #008000;">.</span><span style="color: #0000FF;">UserID</span>, userData<span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">AddTechDaysDataEntry</span><span style="color: #008000;">&#40;</span>entry<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        var entry <span style="color: #008000;">=</span> result<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        entry<span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span> <span style="color: #008000;">=</span> userData<span style="color: #008000;">.</span><span style="color: #0000FF;">FavouriteIDs</span><span style="color: #008000;">;</span>
        tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">UpdateObject</span><span style="color: #008000;">&#40;</span>entry<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        tddeContext<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Then the final bit of code is a very simple class used for passing data between our Application and the Web Service:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>DataContract<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TechDaysUserData
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>DataMember<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UserID<span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#91;</span>DataMember<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FavouriteIDs <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s it, everything you need to implement this feature. You will likely want to expand the settings that are saved, as well as consider your privacy policy around storing User&#8217;s data, etc. but I&#8217;ll leave that as an exercise for you.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/wp7-app-settings-persistence.html" target="_blank">http://roybott.com/blog/wp7-app-settings-persistence.html</a> but we brought it here to share with you.</strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/wp7-app-settings-persistence/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Nested MVVM: Creating an Inline Context Menu for WP7</title>
		<link>http://verysoftware.co.uk/blog/nested-mvvm-creating-an-inline-context-menu-for-wp7/</link>
		<comments>http://verysoftware.co.uk/blog/nested-mvvm-creating-an-inline-context-menu-for-wp7/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 08:15:18 +0000</pubDate>
		<dc:creator>Vitalij Kudresov</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[TechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=281</guid>
		<description><![CDATA[In this blog post I will describe how to create an inline context menu for Windows Phone 7. To overcome several issues we are going to use Nested MVVM, which is a slightly altered MVVM pattern. So what about having &#8230; <a href="http://verysoftware.co.uk/blog/nested-mvvm-creating-an-inline-context-menu-for-wp7/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In this blog post I will describe how to create an inline context menu for Windows Phone 7. To overcome several issues we are going to use Nested MVVM, which is a slightly altered MVVM pattern.</p>
<p>So what about having a context menu on Windows Phone 7? Currently the most common approach is to show an overlay containing a set of buttons. This is a good approach when you don&#8217;t access context menu too often. But when used frequently you might find the following issues:</p>
<ul>
<li>Holding an item is way slower compared to a tap</li>
<li>Overlay covers a great portion of the screen</li>
<li>When clicked on the border between two items there is no way to know which item context menu corresponds to</li>
</ul>
<p>That is why I came up with a slightly different approach.</p>
<p><span id="more-281"></span></p>
<p><em>Note: All the screenshots and code snippets are from VerySoftware talk at TechDays UK 2011, where our team was <a href="http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/">Blending Delicious User Experiences for Windows Phone 7</a>, including me talking about nested MVVM and context menu. Feel free to download our <a href="http://verysoftware.co.uk/talks/techdays/VerySoftware-TechDays2011-SampleAppSourceCode.zip">Tech.Days WP7 app source code</a>.</em></p>
<h2>Concept</h2>
<p><em>This type of context menu will mainly be useful in the ListBoxes where tapping an item doesn’t involve any action.</em></p>
<p>The idea of the context menu we are going to create is to attach a set of items (buttons most of the time) to an item in the list. Those buttons will be hidden and shown only when the user selects an item. Showing a context menu shifts other elements in the ListBox so they don’t overlay any of the UI elements.</p>
<p>I will use a demo Tech.Days app from our presentation as an example. Our final app will have an inline context menu that will contain two buttons:</p>
<ul>
<li><em>Share by email</em> – shares selected session via email</li>
<li><em>Add to favourites </em>– adds selected session into a different ListBox, which contains all favourite sessions</li>
</ul>
<p>Screenshot below shows the final result:</p>
<p><img class="alignnone size-full wp-image-296" title="verysoftware-nested-mvvm-screenshot-1" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/07/verysoftware-nested-mvvm-screenshot-1.png" alt="" width="628" height="699" /></p>
<p>So that’s it about the idea, now let’s start punching in some code.</p>
<h2>Step 1: Set Up</h2>
<p>We start by creating a new WP7 project in Visual Studio where we will add our MVVM stuff:</p>
<ul>
<li>View – this is where our ListBox with sessions will be placed</li>
<li>Model – contains a list of sessions available at UK Tech.Days</li>
<li>ParentViewModel – treat this as a standard view model for now</li>
</ul>
<p><strong>View:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;SessionList&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource CoreListBoxStyle}&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">ItemContainerStyle</span>=<span style="color: #ff0000;">&quot;{StaticResource SessionListBoxItemStyle}&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,12,0,60&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding SessionsVMs}&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">SelectionChanged</span>=<span style="color: #ff0000;">&quot;SelectedSessionChanged&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- A beautiful data template to be placed here --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>Model:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Session <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// Other properties…</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ImagePath <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> isInFavourites <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsInFavourites
  <span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> isInFavourites<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      isInFavourites <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      InvokePropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;IsInFavourites&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>ViewModel:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Our model is only going to be used internally in ViewModel</span>
<span style="color: #0600FF; font-weight: bold;">private</span> ReadOnlyObservableCollection<span style="color: #008000;">&lt;</span>Session<span style="color: #008000;">&gt;</span> _sessions<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Nested child view models (will be described later)</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ObservableCollection<span style="color: #008000;">&lt;</span>SessionViewModel<span style="color: #008000;">&gt;</span> SessionsVMs<span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> SessionsViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>    
  SessionsVMs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>SessionViewModel<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// Retrieve items from a service</span>
  _sessions <span style="color: #008000;">=</span> _techDaysDataManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Sessions</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>What we have created now is a pretty standard MVVM, apart from list of child ViewModels inside the ParentViewModel which we will discuss later. Now we are ready to start adding the juicy bits.</p>
<h2>Step 2: DataTemplate = UserControl</h2>
<p>Next thing we are going to add is a DataTemplate for each session, which will also contain our XAML for DataContext menu.</p>
<p>If you remember our intention was to have a context menu which is set to collapse and will be shown when the user taps on an item which is associated with the context menu. Also when user taps <em>Add to favourites</em> buttons we have to execute some code.</p>
<p>Here comes the first challenge: data template doesn&#8217;t have any code behind, and we can&#8217;t handle things like button clicks. I like to think of data template as an inflatable rubber toy which is inflated with data. So our <strong>XAML-only</strong> data template defines how our item looks but not how it behaves.</p>
<p>To overcome that challenge we set our DataTemplate to be a user control, which isn’t pure XAML and contains code behind, so it can handle things like events. Below is the simplified version of how it would look like:</p>
<p><strong>View:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:SessionItem</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>DataTemplate:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">Here goes:</span>
<span style="color: #808080; font-style: italic;">- Data template itself</span>
<span style="color: #808080; font-style: italic;">- Data Context menu</span>
<span style="color: #808080; font-style: italic;">- Button click events on the button controls</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>DataTemplate, Code Behind:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShareButtonClicked<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>At this point we have our DataTemplate and item context menu UI code written. Screenshot below shows how our app should look like:</p>
<p><img class="alignnone size-full wp-image-297" title="verysoftware-nested-mvvm-screenshot-2" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/07/verysoftware-nested-mvvm-screenshot-2.png" alt="" width="340" height="540" /></p>
<p>Now we have all UI elements in place and now we will bring them into life, by adding the logic.</p>
<h2>Step 3: Adding Logic and Selection Problem</h2>
<p>So let&#8217;s write some logic to handle the tapping of <em>Add to favourites</em> and showing/hiding the context menu.</p>
<p>Here comes the second challenge: it’s only the ListBox that is aware which item is selected in the ListBox and when this selection has changed. So we have to notify our items that they were selected/unselected.</p>
<p><img class="alignnone size-full wp-image-298" title="verysoftware-nested-mvvm-screenshot-3" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/07/verysoftware-nested-mvvm-screenshot-3.png" alt="" width="496" height="261" /></p>
<p>Let’s for a moment pretend that we a using standard MVVM and our ViewModel contains an ObservableCollection of Sessions rather that child ViewModels. This means that each item in the ListBox will get its DataContext set to Session.</p>
<p>In that case ViewModel (Parent ViewModel) will have two options how to notify items about the selection:</p>
<ol>
<li>As we have a list of Sessions, we can notify an appropriate session (model) that it was selected and then model would notify the view</li>
<li>Navigate the logical tree and set a visibility of the right context menu to visible</li>
</ol>
<p>Second solution sounds a lot like a nasty hack and the first one adds code which shouldn’t be in the model, and in fact code which handles the state of the view which should go into ViewModel.</p>
<p>That’s when the nested MVVM comes into the picture.</p>
<h2>Step 4: Creating a nested MVVM</h2>
<p>Now I want to go back to our ViewModel code. If you remember there was a collection of ViewModels (SessionVMs), where we started nesting our ViewModels. What we are doing is wrapping a session inside another ViewModel (child ViewModel) and hold a collection of those inside our parent ViewModel.</p>
<p><strong>ViewModel:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Our model is only going to be used internally in ViewModel</span>
<span style="color: #0600FF; font-weight: bold;">private</span> ReadOnlyObservableCollection<span style="color: #008000;">&lt;</span>Session<span style="color: #008000;">&gt;</span> _sessions<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Nested child view models. This will be a CollectionSource of our ListBox</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ObservableCollection<span style="color: #008000;">&lt;</span>SessionViewModel<span style="color: #008000;">&gt;</span> SessionsVMs<span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> SessionsViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>    
  SessionsVMs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>SessionViewModel<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080; font-style: italic;">// Retrieve items from a service</span>
  _sessions <span style="color: #008000;">=</span> _techDaysDataManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Sessions</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Child ViewModel</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> Session Session <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> SessionViewModel<span style="color: #008000;">&#40;</span>Session session, IParentViewModel parentViewModel<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Session <span style="color: #008000;">=</span> session<span style="color: #008000;">;</span>
  _parentViewModel <span style="color: #008000;">=</span> parentViewModel<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Essentially we wrap our existing model inside a ViewModel. Optionally we could provide a link back to the parent so they can communicate both directions. Now communication between our items will happen in the following way:</p>
<ol>
<li>ListBox notifies a Parent ViewModel about newly selected item. Now VM has two options</li>
<li>Parent ViewModel finds an appropriate Child ViewModel and notifies it about selection/de-selection</li>
<li>Child ViewModel notifies the View by exposing a property which indicates if View is selected or not</li>
<li>View updates its visual to show/hide context menu</li>
</ol>
<p><img class="alignnone size-full wp-image-299" title="verysoftware-nested-mvvm-screenshot-4" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/07/verysoftware-nested-mvvm-screenshot-4.png" alt="" width="769" height="441" /></p>
<h2>Step 5: Extra Stuff (Optional)</h2>
<p>As previously mentioned we can pass a parent ViewModel reference to our Child ViewModel. Here are just a couple of scenarios of usage:</p>
<ul>
<li>Use as proxy for communication with the rest of application</li>
<li>Notify a Parent ViewModel about the state of one of its child (for example an error), so the parent (ListBox) can show an appropriate sate</li>
</ul>
<p> </p>
<p>This is an example of how you can use an existing MVVM pattern in a slightly different way to achieve an interesting result. I hope you have found this post useful. You are more than welcome to check out our <a href="http://verysoftware.co.uk/talks/techdays/VerySoftware-TechDays2011-SampleAppSourceCode.zip">Tech.Days WP7 app source code</a>. Stay tuned, there are more posts to come&#8230;</p>
<p>&nbsp;
<p/>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/nested-mvvm-creating-an-inline-context-menu-for-wp7/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WP7 Error Reporting Done Simply</title>
		<link>http://verysoftware.co.uk/blog/wp7-error-reporting-done-simply/</link>
		<comments>http://verysoftware.co.uk/blog/wp7-error-reporting-done-simply/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 11:22:51 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Dev Tips]]></category>
		<category><![CDATA[UKTechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=277</guid>
		<description><![CDATA[Part 3 in my series of blog posts covering the quick code Tips &#038; Tricks I shared at UK Tech.Days, is about enabling your users to provide you with some useful information if things go badly with your Windows Phone &#8230; <a href="http://verysoftware.co.uk/blog/wp7-error-reporting-done-simply/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Part 3 in my <a href="http://verysoftware.co.uk/blog/tag/uktechdays">series of blog posts</a> covering the quick code Tips &#038; Tricks I shared at <a href="http://uktechdays.cloudapp.net/techdays-live/blending-delicious-user-experiences-for-windows-phone-7.aspx">UK Tech.Days</a>, is about enabling your users to provide you with some useful information if things go badly with your Windows Phone 7 app.</p>
<p>Often on the marketplace I have seen apps that have multiple reviews by different users just saying things like &#8220;Crashes on startup!&#8221; or &#8220;Doesn&#8217;t work on my device!&#8221;, and as a developer I know its frustrating to see things like this when the app is working perfectly fine on your phone. While it would be nice for users to provide detailed repro&#8217; steps and associated data, that&#8217;s not really likely to happen, but in my opinion the main thing you want as a developer is the Exception and a Call Stack of whatever is causing the app to crash.</p>
<p><span id="more-277"></span></p>
<p>Luckily, the <strong>App.xaml.cs</strong> file included in the standard Windows Phone project template includes a function named <strong>&#8220;Application_UnhandledException&#8221;</strong> which is called when an unhandled exception occurs and is the last point of call before the app closes and returns the user to the homescreen. This function is primarily used to allow the debugger to break into the app when debugging so that the developer can try to investigate the unhandled exception, however by adding a little bit of code to it you can provide users in the wild the ability to email the exception information to you.</p>
<p>The following code shows this function with the added &#8216;else&#8217; block which asks the user if they would like to send the error details to the developer. If they accept then a new email task is created which contains the exception object information (the exception message and call stack). Clearly if you want to include more information then you can just add it to the body of the email:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_UnhandledException<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ApplicationUnhandledExceptionEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debugger</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsAttached</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// An unhandled exception has occurred; break into the debugger</span>
		<span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debugger</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Break</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080;">#region Error Reporting code</span>
	<span style="color: #0600FF; font-weight: bold;">else</span>
	<span style="color: #008000;">&#123;</span>
		var result <span style="color: #008000;">=</span> MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Would you like to email the details of this error to the Developer?
(No personal information, other than your email address, will be sent to the Developer)&quot;</span>,
			<span style="color: #666666;">&quot;An error has occurred!&quot;</span>,
			MessageBoxButton<span style="color: #008000;">.</span><span style="color: #0000FF;">OKCancel</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result <span style="color: #008000;">==</span> MessageBoxResult<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			var task <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EmailComposeTask<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			task<span style="color: #008000;">.</span><span style="color: #0000FF;">To</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;contact@DeveloperEmailAddress.com&quot;</span><span style="color: #008000;">;</span>
			task<span style="color: #008000;">.</span><span style="color: #0000FF;">Subject</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Error Report: TechDays 2011 App&quot;</span><span style="color: #008000;">;</span>
			task<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;An unhandled exception occurred in TechDays 2011 App:
{0}&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">ExceptionObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			task<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The great things about this approach is that it can very easily be dropped into any app, requires no infrastructure/web service and as this is done via email, the user gets to clearly see and approve all information being sent. This is a good thing from a user&#8217;s perspective as they can see that no personal information (other than their email) is being shared.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/wp7-error-reporting-done-simply.html" target="_blank">http://roybott.com/blog/wp7-error-reporting-done-simply.html</a> but we brought it here to share with you.</strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/wp7-error-reporting-done-simply/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting Ratings for Your WP7 Apps</title>
		<link>http://verysoftware.co.uk/blog/getting-ratings-for-your-wp7-apps/</link>
		<comments>http://verysoftware.co.uk/blog/getting-ratings-for-your-wp7-apps/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 13:36:30 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[UKTechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=255</guid>
		<description><![CDATA[The second post in my series of blog posts covering the code Tips &#38; Tricks I shared at UK Tech.Days, is about getting more positive ratings/reviews for your Windows Phone 7 app. Anyone who has had a bad experience with &#8230; <a href="http://verysoftware.co.uk/blog/getting-ratings-for-your-wp7-apps/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The second post in my <a href="http://verysoftware.co.uk/blog/tag/uktechdays">series of blog posts</a> covering the code Tips &amp; Tricks I shared at <a href="http://uktechdays.cloudapp.net/techdays-live/blending-delicious-user-experiences-for-windows-phone-7.aspx" target="_blank">UK Tech.Days</a>, is about getting more positive ratings/reviews for your Windows Phone 7 app.</p>
<p>Anyone who has had a bad experience with your application is motivated to go and leave it a scathing review, whereas the hundreds of people who loved your app may never even consider leaving it a good review. One way to counter this is to simply ask those users who like your app to leave you a review, however you don&#8217;t want to bug people too often or before they&#8217;ve had a real chance to use your application. So what can you do?</p>
<p><span id="more-255"></span></p>
<p>Well one suggestion that I&#8217;ve heard a few times is to wait until a user has run your application a number of times (say 10) and then throw up a message box asking them to rate your app. By waiting until the user has run the app a number of times you can be more confident in receiving a positive review because at this point chances are they like the app or they wouldn&#8217;t keep returning to it. To avoid bugging users too often you should increase the number of runs between each time you display the message box so that the user doesn&#8217;t feel like they are having to constantly hit &#8216;no&#8217; every time they open the app. Also giving the user the option to never be asked again is a good way to say on their good side.</p>
<p>The following block of code can be dropped into any existing application and helps provide the above functionality:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> PerformedRatingPromptCheck <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> PhoneApplicationPage_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RoutedEventArgs</span> e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>PerformedRatingPromptCheck<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        PerformedRatingPromptCheck <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> numAppLaunches <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        IsolatedStorageSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationSettings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rating_numAppLaunches&quot;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> numAppLaunches<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> numNextRatingPrompt <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>IsolatedStorageSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationSettings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Rating_numNextRatingPrompt&quot;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> numNextRatingPrompt<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            numNextRatingPrompt <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>numAppLaunches <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;=</span> numNextRatingPrompt<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            IsolatedStorageSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationSettings</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Rating_numNextRatingPrompt&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> numNextRatingPrompt <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
&nbsp;
            NotificationBox<span style="color: #008000;">.</span><span style="color: #0000FF;">ShowAgain</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Enjoying the app?&quot;</span>,
                    <span style="color: #666666;">&quot;Would you like to rate this app now?&quot;</span>,
                    <span style="color: #666666;">&quot;Ask me later&quot;</span>,
                    <span style="color: #0600FF; font-weight: bold;">false</span>,
                    Surpressed <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>,
                    <span style="color: #666666;">&quot;Rating_MsgPrompt&quot;</span>,
                    <span style="color: #008000;">new</span> NotificationBoxCommand<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Yes&quot;</span>, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">new</span> MarketplaceReviewTask<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #008000;">new</span> NotificationBoxCommand<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;No&quot;</span>, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        IsolatedStorageSettings<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationSettings</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Rating_numAppLaunches&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> numAppLaunches <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This code simply pulls two values out of the IsolatedStorageSettings, one that tracks the number of times the application has been run and one that tracks when you&#8217;re next going to show the prompt, and then displays the prompt if needed. If the user clicks the &#8216;Yes&#8217; button on the prompt then a &#8220;MarketplaceReviewTask&#8221; is created and shown so that the user can leave your app a review.</p>
<p>You may have noticed that the &#8216;prompt&#8217; the code above shows is not a standard Windows Phone MessageBox but is instead a &#8216;NotificationBox&#8217;. <a href="http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/19/windows-phone-7-custom-message-box.aspx" target="_blank">Tomer Shamam</a> has created an incredibly handy DLL that provides a prompt that looks like the Windows Phone MessageBox but also allows you to have a tick box which can be unticked to cause the prompt to never be shown again. The result will look something like this:</p>
<p><img class="alignnone size-full wp-image-262" title="verysoftware-UKTechDays-GettingWP7UserReviews" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/verysoftware-UKTechDays-GettingWP7UserReviews.png" alt="" width="324" height="580" /></p>
<p>So by simply downloading the <a href="http://blogs.microsoft.co.il/files/folders/732863/download.aspx" target="_blank">NotificationBox DLL</a>, referencing it in your application and then including the code snippet above you can hopefully dramatically increase the number of positive ratings/reviews your app receives.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/getting-ratings-for-your-wp7-app.html" target="_blank">http://roybott.com/blog/getting-ratings-for-your-wp7-app.html</a> but we brought it here to share with you.</strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/getting-ratings-for-your-wp7-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating Video Demo of Your Windows Phone 7 Apps</title>
		<link>http://verysoftware.co.uk/blog/creating-video-demo-of-your-windows-phone-7-apps/</link>
		<comments>http://verysoftware.co.uk/blog/creating-video-demo-of-your-windows-phone-7-apps/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 08:15:50 +0000</pubDate>
		<dc:creator>Sergei Golubev</dc:creator>
				<category><![CDATA[Marketing]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=132</guid>
		<description><![CDATA[We&#8217;ve been asked a few times about how we captured and embedded videos of our WP7 apps for our Polyglot website and Tech.Days presentation. Here is how we do it. Capture We use Screen Capture tool from Microsoft’s Expression Encoder &#8230; <a href="http://verysoftware.co.uk/blog/creating-video-demo-of-your-windows-phone-7-apps/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been asked a few times about how we captured and embedded videos of our WP7 apps for our Polyglot <a href="http://verysoftware.co.uk/polyglot/" target="_blank">website</a> and Tech.Days <a href="http://verysoftware.co.uk/talks/techdays/" target="_blank">presentation</a>. Here is how we do it.</p>
<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-showcase.jpg"><img title="screenshot-createvideodemowp7-showcase" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-showcase.jpg" alt="" width="640" height="452" /></a><br />
<span id="more-132"></span></p>
<h2>Capture</h2>
<p>We use Screen Capture tool from Microsoft’s <a href="http://www.microsoft.com/expression/products/Encoder4_Overview.aspx" target="_blank">Expression Encoder 4</a>. It’s a free, easy-to-use and offers numerous settings. It will help you to select the correct capture area inside phone’s frame.</p>
<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-capture.jpg"><img class="alignnone size-full wp-image-141" title="screenshot-createvideodemowp7-capture" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-capture.jpg" alt="" width="626" height="377" /></a></p>
<h2>Encode</h2>
<p>After the video is captured, send it to Encoder where you can make the final changes before exporting it into variety of encoding formats.</p>
<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-encode.jpg"><img class="alignnone size-full wp-image-143" title="screenshot-createvideodemowp7-encode" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-encode.jpg" alt="" width="632" height="449" /></a></p>
<h2>Embed</h2>
<p>Now it’s time to embed the exported video into your website or presentation. We use standard <a href="http://verysoftware.co.uk/images/windows-phone-emulator-frame.png" target="_blank">Windows Phone Emulator frame</a> with transparent background.</p>
<p>Storyboards for our AppTracker <a href="http://www.youtube.com/verysoftware" target="_blank">promo video</a> were created and exported as WMV simply in PowerPoint:</p>
<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-embed.jpg"><img class="alignnone size-full wp-image-142" title="screenshot-createvideodemowp7-embed" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/screenshot-createvideodemowp7-embed.jpg" alt="" width="635" height="353" /></a></p>
<p>For use on the website we upload it on YouTube. Its latest code for embedding uses HTML5 which allows viewing your video on devices like iPhone which don’t support Flash. A bit of positioning and clipping tweaks using CSS and your video player fits nicely inside the phone frame image.</p>
<h2>Showcase</h2>
<p>As a result you get an effective demo of your app ready to impress your clients, users, friends and colleagues. As simple as that!</p>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/creating-video-demo-of-your-windows-phone-7-apps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 Trial Mode Done Simply</title>
		<link>http://verysoftware.co.uk/blog/wp7-trial-mode-done-simply/</link>
		<comments>http://verysoftware.co.uk/blog/wp7-trial-mode-done-simply/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 12:43:58 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Dev Tips]]></category>
		<category><![CDATA[UKTechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=192</guid>
		<description><![CDATA[In my part of the VerySoftware&#8217;s presentation at UKTechDays I talked a bit about some very easy to code things that can be added to nearly any application existing application. Over the next few posts I&#8217;m going provide the code &#8230; <a href="http://verysoftware.co.uk/blog/wp7-trial-mode-done-simply/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div>In my part of the VerySoftware&#8217;s presentation at <a href="http://roybott.com/blog/verysoftware-at-uktechdays-2011.html">UKTechDays</a> I talked a bit about some very easy to code things that can be added to nearly any application existing application. Over the next few posts I&#8217;m going provide the code and detail of these tips for reference and for anyone who didn&#8217;t see the presentation.</div>
<h2>Trial Mode</h2>
<p>Adding a trial mode to an existing paid application is very easy to do and highly recommended, even if you also have a &#8216;free&#8217; version of you app available on the marketplace, because it lowers the hurdle between a user seeing your application and a user making the purchase. Also improvements to the on phone Marketplace rumoured to be coming with the relelase of Mango this Fall will mean that paid applications with trials are highlighted more than those without.</p>
<p><span id="more-192"></span></p>
<p>The first thing you need to do when implementing a trial mode is figure out whether the current user of your application is using the paid version or the trial. The following code snippet enables you to do this and also provides a few other useful features:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">?</span> cachedIsTrialMode <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsTrialMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> IsTrialMode<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsTrialMode<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> forceFromCache<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// If this code is being run buy VS/Blend design tool, we don't want to MessageBox</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>DesignerProperties<span style="color: #008000;">.</span><span style="color: #0000FF;">IsInDesignTool</span><span style="color: #008000;">&#41;</span>
          <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>forceFromCache<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>cachedIsTrialMode <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
  <span style="color: #008080;">#if DEBUG</span>
          MessageBoxResult result <span style="color: #008000;">=</span> MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Click ok to simulate paid version&quot;</span>,
<span style="color: #666666;">&quot;Simulate paid version?&quot;</span>, MessageBoxButton<span style="color: #008000;">.</span><span style="color: #0000FF;">OKCancel</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
          <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>result <span style="color: #008000;">==</span> MessageBoxResult<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span>
          <span style="color: #008000;">&#123;</span>
              cachedIsTrialMode <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
          <span style="color: #008000;">&#125;</span>
          <span style="color: #0600FF; font-weight: bold;">else</span>
          <span style="color: #008000;">&#123;</span>
              cachedIsTrialMode <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
          <span style="color: #008000;">&#125;</span>
  <span style="color: #008080;">#else</span>
          LicenseInformation licenseInfo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LicenseInformation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
          cachedIsTrialMode <span style="color: #008000;">=</span> licenseInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrial</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008080;">#endif</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> cachedIsTrialMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This block of code provides a wrapper around Microsoft&#8217;s LicenseInformation API and adds 2 key features. Firstly the block of code surrounded by #if DEBUG provides you with a very simple way of testing how a user experiences your app in both Trial and Paid modes, by offering the option when the app is launched in Debug. Secondly the result of the API call is cached, it is important to do this (especially if you are making frequent calls to it) because a call into that API can have a performance hit of <a href="http://msdn.microsoft.com/en-us/library/ff967558(v=VS.92).aspx">~60ms or more</a>.</p>
<p>As we are caching the result of the first call into the LicenseInformaiton API we need the following block of code. By making a call to the IsTrialMode function in the <em>Application_Launching</em> and <em>Application_Activated</em> functions in App.xaml.cs we are ensuring that we are always using a correct result. Imagine if the user left your application, purchased it and then returned by pressing the back button, in this case the app would be rehydrated so we would therefore want to ensure we are not using an old cached value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #008080; font-style: italic;">// Code to execute when the application is launching (eg, from Start)</span>
  <span style="color: #008080; font-style: italic;">// This code will not execute when the application is reactivated</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Launching<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, LaunchingEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #008080;">#region Trial Mode code</span>
      <span style="color: #008080; font-style: italic;">// Force a recheck of the IsTrialMode Flag</span>
      Utilities<span style="color: #008000;">.</span><span style="color: #0000FF;">HelperFuncs</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrialMode</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008080;">#endregion</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// Code to execute when the application is activated (brought to foreground)</span>
  <span style="color: #008080; font-style: italic;">// This code will not execute when the application is first launched</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Activated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ActivatedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #008080;">#region Trial Mode code</span>
     <span style="color: #008080; font-style: italic;">// Force a recheck of the IsTrialMode Flag</span>
      Utilities<span style="color: #008000;">.</span><span style="color: #0000FF;">HelperFuncs</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrialMode</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008080;">#endregion</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Finally we need to use the result of the IsTrialMode function; I&#8217;ve seen some very complicated ways of preventing users from accessing content in an app but in this example we simple display a semi-transparent &#8216;overlay&#8217; (UI element) which literally blocks the user from being able to touch/interact with the content behind it. The overlay contains some text selling the application/feature to the user and a button allowing them to purchase the app. The code behind this button simply creates and shows a &#8220;MarketplaceDetailTask&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> BuyAppButton_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #008000;">new</span> MarketplaceDetailTask<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>To show/hide the Trial overlay we simply check the result of the IsTrialMode call and set the overlay&#8217;s Visibility property to either Visible or collapsed, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #6666cc; font-weight: bold;">void</span> VideosView_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>HelperFuncs<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrialMode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
          VisualStateManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GoToState</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #666666;">&quot;TrialWelcome&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0600FF; font-weight: bold;">else</span>
      <span style="color: #008000;">&#123;</span>
          VisualStateManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GoToState</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #666666;">&quot;DialogBoxesHidden&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So that&#8217;s it, everything you need to do to include a Trial Mode in your application.</p>
<p>For details of our TechDays talk take a look <a href="http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/">here</a>, and for further reference about trial mode check the <a href="See the MSDN page for reference: http://msdn.microsoft.com/en-us/library/ff967554(v=VS.92).aspx">MSDN documentation</a>.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/wp7-trial-mode-done-simply.html" target="_blank">http://roybott.com/blog/wp7-trial-mode-done-simply.html</a> but we brought it here to share with you.</strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/wp7-trial-mode-done-simply/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VerySoftware at Microsoft Tech.Days</title>
		<link>http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/</link>
		<comments>http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/#comments</comments>
		<pubDate>Sun, 19 Jun 2011 23:21:11 +0000</pubDate>
		<dc:creator>The VerySoftware Team</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[TechDays]]></category>
		<category><![CDATA[UKTechDays]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=53</guid>
		<description><![CDATA[A few weeks ago our team was “Blending Delicious User Experiences for Windows Phone 7” at Microsoft Tech.Days in London. Thank you everyone for coming, we really hope you have enjoyed our talk! Our talk We wanted to share experience &#8230; <a href="http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/VerySoftware-TechDays2011-Slides-Intro.png"><img class="alignnone size-full wp-image-80" title="VerySoftware-TechDays2011-Slides-Intro" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/VerySoftware-TechDays2011-Slides-Intro.png" alt="" width="575" height="325" /></a></p>
<p>A few weeks ago our team was “<em>Blending Delicious User Experiences for Windows Phone 7</em>” at Microsoft Tech.Days in London. Thank you everyone for coming, we really hope you have enjoyed our talk!</p>
<p><span id="more-53"></span></p>
<h2>Our talk</h2>
<p>We wanted to share experience developing Windows Phone 7 apps and show tips and tricks for designers and developers to help take their app&#8217;s user experience to the next level.</p>
<p><strong>For starter</strong> we have shared our experience designing for Windows Phone</p>
<ul>
<li>transition from developing for the iPhone to Windows Phone</li>
<li>our love for Expression Blend and great developer-designer workflow</li>
<li>showcase of our latest apps – <a href="http://bit.ly/dJYItw">Polyglot</a> and <a href="http://bit.ly/i2cHRH">Gifts</a></li>
</ul>
<p><strong>For main</strong> we have demonstrated our award-winning <a href="http://bit.ly/hRlfik">AppTracker</a> – the ultimate app for developers to track their apps’ performance on the Marketplace.</p>
<p><strong>For dessert</strong> we served a selection of tips &amp; tricks for designers and developers on the example of building a sample Tech.Days app:</p>
<ul>
<li>design-time data</li>
<li>dummy data in code</li>
<li>nested MVVM</li>
<li>designing graphics</li>
<li>information architecture</li>
<li>transitions: page navigation, animating screen rotation</li>
<li>trial mode</li>
<li>getting ratings</li>
<li>error reporting</li>
<li>user settings in the cloud</li>
</ul>
<h2>Tech.Days sample app</h2>
<p><a href="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/VerySoftware-TechDays2011-Slides-TechDaysSampleApp.png"><img class="alignnone size-full wp-image-85" title="VerySoftware-TechDays2011-Slides-TechDaysSampleApp" src="http://verysoftware.co.uk/blog/wp-content/uploads/2011/06/VerySoftware-TechDays2011-Slides-TechDaysSampleApp.png" alt="" width="575" height="324" /></a></p>
<p>We have made the <a href="http://verysoftware.co.uk/talks/techdays/VerySoftware-TechDays2011-SampleAppSourceCode.zip">source code</a> for our sample Tech.Days Windows Phone 7 app available for download. The app is not fully ready for release onto the Marketplace as it uses locally cached data, but it is a great template/starting point for anyone wanting to create a companion app for a conference or event.</p>
<h2>Slides and video</h2>
<p>Presentation slides available for <a href="http://verysoftware.co.uk/talks/techdays/VerySoftware-TechDays2011-Slides-Compact.pptx">download</a> and on <a href="http://www.slideshare.net/verysoftware/blending-delicious-user-experiences-for-windows-phone-7-by-verysoftware-8260519" target="_blank">slideshare</a>. You can watch the entire talk below or on <a href="http://uktechdays.cloudapp.net/techdays-live/blending-delicious-user-experiences-for-windows-phone-7.aspx">Tech.Days website</a>:</p>
<div style="width: 639px; height: 360px;"><object width="639" height="360" type="application/x-silverlight-2" data="data:application/x-oleobject;base64,QfXq3+HzJEysrJnDBxUISgAJAAAKQgAANSUAABQAAAAjAEYARgAwADAAMAAwADAAMAAAAAAAAAAAAAAAAAAAAIoAAABoAHQAdABwADoALwAvAHcAdwB3AC4AbQBpAGMAcgBvAHMAbwBmAHQALgBjAG8AbQAvAHMAaABvAHcAYwBhAHMAZQAvAHMAaQBsAHYAZQByAGwAaQBnAGgAdAAvAHAAbABhAHkAZQByAC8AMQAvAHAAbABhAHkAZQByAC0AZQBuAC4AeABhAHAAAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAIAAEMAdQBsAHQAdQByAGUAPQBlAG4ALQBHAEIALABVAHUAaQBkAD0AMgAyAGEAMQA2AGIAZAA3AC0AYgAyAGUAZAAtADQAMQBlADUALQBhADYANwBhAC0AZQA4AGIANgBlADIANQAzADUAMABlAGIALABBAHUAdABvAHAAbABhAHkAPQBGAGEAbABzAGUALABTAGgAbwB3AE0AYQByAGsAZQB0AGkAbgBnAE8AdgBlAHIAbABhAHkAPQB0AHIAdQBlACwATQBpAHMAYwBDAG8AbgB0AHIAbwBsAHMAPQBGAHUAbABsAFMAYwByAGUAZQBuADsARABlAHQAYQBjAGgAZQBkACwAUwBoAG8AdwBNAGUAbgB1AD0AVAByAHUAZQAsAFQAYQBiAHMAPQBFAG0AYgBlAGQAOwBFAG0AYQBpAGwAOwBTAGgAYQByAGUAOwBJAG4AZgBvACwAUwBoAG8AdwBDAGEAcAB0AGkAbwBuAD0AZgBhAGwAcwBlACwAVgBpAGQAZQBvAFUAcgBsAD0AaAB0AHQAcAA6AC8ALwB3AHcAdwAuAG0AaQBjAHIAbwBzAG8AZgB0AC4AYwBvAG0ALwBzAGgAbwB3AGMAYQBzAGUALwBlAG4ALwBnAGIALwBkAGUAdABhAGkAbABzAC8AMgAyAGEAMQA2AGIAZAA3AC0AYgAyAGUAZAAtADQAMQBlADUALQBhADYANwBhAC0AZQA4AGIANgBlADIANQAzADUAMABlAGIALABNAG8AZABlAD0AUABsAGEAeQBlAHIAAAAAAAAAAAAAAP//AAD//wAAAAAAAAAAAAAAAAAAGAAAADQALgAwAC4ANQAwADQAMAAxAC4AMAAAAAoAAAB0AHIAdQBlAAAAAAAAAAAAAAAAAAAA"><param name="source" value="http://www.microsoft.com/showcase/silverlight/player/1/player-en.xap" /><param name="initParams" value="Culture=en-GB,Uuid=22a16bd7-b2ed-41e5-a67a-e8b6e25350eb,Autoplay=False,ShowMarketingOverlay=true,MiscControls=FullScreen;Detached,ShowMenu=True,Tabs=Embed;Email;Share;Info,ShowCaption=false,VideoUrl=http://www.microsoft.com/showcase/en/gb/details/22a16bd7-b2ed-41e5-a67a-e8b6e25350eb,Mode=Player" /><param name="enableHtmlAccess" value="true" /><param name="allowHtmlPopupwindow" value="true" /><param name="background" value="#FF000000" /><param name="minRuntimeVersion" value="4.0.50401.0" /><param name="autoUpgrade" value="true" /></object></div>
<p><script type="text/javascript">// <![CDATA[
document.write("<script type='text/javascript' src='" + (window.location.protocol) + "//c.microsoft.com/ms.js'><\/script>");
// ]]&gt;</script></p>
<p><strong>We plan to cover the contents of our talk bit by bit in our future blog posts. Watch this space!</strong></p>
<p>If you have any feedback or questions, feel free to drop us a line: <a href="mailto:contact@verysoftware.co.uk">contact@verysoftware.co.uk</a></p>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/verysoftware-at-microsoft-tech-days/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Serializing to Binary on WP7 &#8211; Part 2</title>
		<link>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part2/</link>
		<comments>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part2/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 08:15:17 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[AppTracker]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=16</guid>
		<description><![CDATA[After investigating a couple of 3rd party Binary Serialization libraries for WP7 and having seen a couple of suggestions online of just writing your own, I decided to do just that. There were 2 main places where I found information &#8230; <a href="http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div>
<p>After <a href="http://roybott.com/blog/serializing-to-binary-on-wp7-part-1.html">investigating</a> a couple of 3rd party Binary Serialization libraries for WP7 and having seen a couple of suggestions online of just writing your own, I decided to do just that.</p>
<p>There were 2 main places where I found information on how to potentially write you own Binary Serializer for WP7, the first I felt took a rather old school approach by adding a &#8220;Write&#8221; function to each class which accepted a BinaryWriter and simply wrote out each of its own properties. The clear problem with this approach is the maintenance involved with adding new properties or if you wanted to serialize a different class.</p>
<p><span id="more-16"></span></p>
<p>The second approach was explained by Eugene Chaikin on <a href="http://www.eugenedotnet.com/2010/12/windows-phone-7-serialization-binary-serialization/">his site</a>, where he attempted to replicate the DataContractSerializer&#8217;s interface and even make use of the &#8216;[DataMember]&#8216; attributes in the same way.</p>
<p>So based on the <a href="http://www.eugenedotnet.com/2010/12/windows-phone-7-serialization-binary-serialization/">simple sample code</a> I started to create my own and call it the <strong>VSBinarySerialiser</strong> (because this was for use on <a href="http://verysoftware.co.uk/">VerySoftware</a> [VS] projects, and Serialise is spelt with an S in the UK). The goal was to be able to use code like the following directly in place of the equivalent DataContractSerializer code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Write</span>
var serialiser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> VSBinarySerialiser<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
serialiser<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteObject</span><span style="color: #008000;">&#40;</span>fs, theData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Read</span>
var serialiser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> VSBinarySerialiser<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var result <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span>serialiser<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadObject</span><span style="color: #008000;">&#40;</span>fs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The first obvious change I needed to make was due to the fact that my classes contained other classes inside of them, so I needed a ReadObject/WriteObject function that I could use internally to recursively serialise objects that were encapsulated within each other. To do this I modified the public ReadObject/WriteObject functions to simply create the BinaryReader/BinaryWriter and then call into the internal function passing the type that was stored when the <strong>VSBinarySerialiser</strong> was created:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteObject<span style="color: #008000;">&#40;</span>Stream stream, <span style="color: #6666cc; font-weight: bold;">object</span> theData<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>stream <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> theData <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
BinaryWriter bw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BinaryWriter<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
InternalWriteObject<span style="color: #008000;">&#40;</span>bw, theData, serializableObjectType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ReadObject<span style="color: #008000;">&#40;</span>Stream stream<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>stream <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
BinaryReader br <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BinaryReader<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">return</span> InternalReadObject<span style="color: #008000;">&#40;</span>br, serializableObjectType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Internal Read/Write Object functions I wrote were similar to those in Eugene&#8217;s example, except for 3 main changes. Firstly I gathered the list of Properties on the object marked with the attribute at this point rather than earlier, as this allows recursive use of the function. Secondly to support the writing out of null objects when calling this function recursively I write a boolean out at the start to indicate if the object is Null or not. Finally I extended the list of type checks to include the additional data types I needed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> InternalWriteObject<span style="color: #008000;">&#40;</span>BinaryWriter bw, <span style="color: #6666cc; font-weight: bold;">object</span> obj, Type type<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>obj <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    var serializableProperties <span style="color: #008000;">=</span> GetMarkedProperties<span style="color: #008000;">&#40;</span>type<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>PropertyInfo pi <span style="color: #0600FF; font-weight: bold;">in</span> serializableProperties<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var value <span style="color: #008000;">=</span> pi<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>obj, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>value <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span> <span style="color: #008000;">??</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToUniversalTime</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Ticks</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>myEnum<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyOtherClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            InternalWriteObject<span style="color: #008000;">&#40;</span>bw, value <span style="color: #0600FF; font-weight: bold;">as</span> Language, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyOtherClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ObservableCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            WriteObsevableCollection<span style="color: #008000;">&#40;</span>bw, value <span style="color: #0600FF; font-weight: bold;">as</span> ObservableCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>
<span style="color: #666666;">&quot;Attempted to WriteObject property of type '{0}' which is currently unsupported!&quot;</span>, pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">object</span> InternalReadObject<span style="color: #008000;">&#40;</span>BinaryReader br, Type type<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadBoolean</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">object</span> deserializedObject <span style="color: #008000;">=</span> Activator<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span>type<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var serializableProperties <span style="color: #008000;">=</span> GetMarkedProperties<span style="color: #008000;">&#40;</span>type<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>PropertyInfo pi <span style="color: #0600FF; font-weight: bold;">in</span> serializableProperties<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Type propType <span style="color: #008000;">=</span> pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadDouble</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadBoolean</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, <span style="color: #008000;">new</span> DateTime<span style="color: #008000;">&#40;</span>br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadInt64</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, DateTimeKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Utc</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>myEnum<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, <span style="color: #008000;">&#40;</span>AppStore<span style="color: #008000;">&#41;</span>br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadInt32</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyOtherClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, InternalReadObject<span style="color: #008000;">&#40;</span>br, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyOtherClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> MyOtherClass, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ObservableCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            pi<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>deserializedObject, ReadObservableCollection<span style="color: #008000;">&#40;</span>br<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>
<span style="color: #666666;">&quot;Attempted to ReadObject property of type '{0}' which is currently unsupported!&quot;</span>, pi<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyType</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> deserializedObject<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><em>I&#8217;ve changed the names of some of the classes here to protect the innocent :)</em></p>
<p>The other change you might notice in the above code is the support for ObservableCollections, as we use a number of these for storing lists of data I created generic functions called WriteObsevableCollection&lt;T&gt; and ReadObservableCollection&lt;T&gt; to handle these:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteObsevableCollection<span style="color: #008000;">&#40;</span>BinaryWriter bw, ObservableCollection collection<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>collection <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> collection<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        bw<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>collection<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var item <span style="color: #0600FF; font-weight: bold;">in</span> collection<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            InternalWriteObject<span style="color: #008000;">&#40;</span>bw, item, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> ObservableCollection ReadObservableCollection<span style="color: #008000;">&#40;</span>BinaryReader br<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> count <span style="color: #008000;">=</span> br<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadInt32</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> count<span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> <span style="color: #008000;">--</span>i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        result<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>InternalReadObject<span style="color: #008000;">&#40;</span>br, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s it, we now have our own Binary Serialiser that works on WP7; but how does it compare performance wise? Below is a table showing the data for our VSBinarySerialiser against the DataContractSerializer (averages are based on 5 runs of each):</p>
<table border="1">
<tbody>
<tr>
<th> </th>
<th>Avg. Save time (seconds)</th>
<th>Avg. Load time (seconds)</th>
</tr>
<tr>
<th>DataContractSerializer</th>
<td>1.20</td>
<td>4.37</td>
</tr>
<tr>
<th>VSBinarySerialiser</th>
<td>1.25</td>
<td>2.63</td>
</tr>
</tbody>
</table>
<p> </p>
<p>Not too bad, the time to write/serialize the data is slightly slower than the DataContractSerializer but the read/deserialize takes just 60% of the original time. I expect the slower write time is possibly down to something relating to how I&#8217;m reflecting over the classes to get the properties each time, rather than caching them for each Type I come across. That optimisation however can wait for another time.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/serializing-to-binary-on-wp7-part-2.html" target="_blank">http://roybott.com/blog/serializing-to-binary-on-wp7-part-2.html</a> but we brought it here to share with you.</strong></p></blockquote>
</div>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serializing to Binary on WP7 &#8211; Part 1</title>
		<link>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part1/</link>
		<comments>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part1/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 08:05:27 +0000</pubDate>
		<dc:creator>Roy Herrod</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[AppTracker]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://verysoftware.co.uk/blog/?p=13</guid>
		<description><![CDATA[Serializing data out to a file on WP7 is a very easy task thanks to the DataContractSerializer which allows you to tag properties in your class with &#8216;[DataMember]&#8216; and then use a few simple line of code to save/load that &#8230; <a href="http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Serializing data out to a file on WP7 is a very easy task thanks to the DataContractSerializer which allows you to tag properties in your class with &#8216;[DataMember]&#8216; and then use a few simple line of code to save/load that class to file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Save</span>
MyClass theData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFileStream fs <span style="color: #008000;">=</span> isoStore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyClass.xml&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	DataContractSerializer ser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataContractSerializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	ser<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteObject</span><span style="color: #008000;">&#40;</span>fs, theData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Load</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFileStream fs <span style="color: #008000;">=</span> isoStore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyClass.xml&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	DataContractSerializer ser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataContractSerializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	MyClass result <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MyClass<span style="color: #008000;">&#41;</span>ser<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadObject</span><span style="color: #008000;">&#40;</span>fs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-13"></span></p>
<p>This is all well and good for getting something up and running quickly, but once you start having more complex classes and/or more data, performance starts to become an issue. A quick test of how long <a href="http://social.zune.net/redirect?type=phoneApp&amp;id=ceb1e261-9909-e011-9264-00237de2db9e">AppTracker</a> currently takes to deserialize the cache file when following just 3 apps (Polyglot, Gifts and AppTracker &#8211; consisting of a total of about 544 reviews) resulted in the following timings:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="205" valign="top"><strong>DataContractSerializer</strong></td>
<td width="205" valign="top"><strong>Save Time (seconds)</strong></td>
<td width="205" valign="top"><strong>Load Time (seconds)</strong></td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.154</td>
<td width="205" valign="top">4.415</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.297</td>
<td width="205" valign="top">4.315</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.186</td>
<td width="205" valign="top">4.384</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.214</td>
<td width="205" valign="top">4.487</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.161</td>
<td width="205" valign="top">4.262</td>
</tr>
<tr>
<td width="205" valign="top"><strong>Average:</strong></td>
<td width="205" valign="top"><strong>1.2024</strong></td>
<td width="205" valign="top"><strong>4.3726</strong></td>
</tr>
</tbody>
</table>
<p>As you will generally be loading up your data when the app loads (in a background thread and using a loading indicator I&#8217;d hope) the time it takes to load the data from file is noticeable to the user because it is the time between them launching the app and being able to actually use it.</p>
<h2>Binary Serialization (Serialisation)</h2>
<p>The DataContractSerializer stores data in the file in XML form which is one cause of the performance issues on larger/more complex datasets. Therefore it makes sense to use binary serialization instead, however WP7 doesn&#8217;t have support for a Binary Serializer so you are left with two choices &#8211; Use a 3rd party library for Binary serialisation or Role your own!</p>
<p>This first post is going to cover 3rd Party solutions, and then later I will blog about creating your own.</p>
<p>After digging around on the net for a while I came across less information than I was hoping to find. A few places suggested using custom &#8216;database&#8217; solutions but with <a href="http://jesseliberty.com/2011/05/10/coming-in-mangosql-server-ce/">SQL CE support coming for WP7 in the Mango update</a> I decided to avoid that option. So in the end I was left with 2 possible solutions:</p>
<h2>SharpSerializer</h2>
<p>On it&#8217;s website <a href="http://www.sharpserializer.com/en/index.html">SharpSerializer</a> is described as &#8220;<em>an open source XML and binary serializer for .NET Framework, .NET Compact Framework and Silverlight</em>&#8221; which sounds pretty good. It also offers a very simple interface making serializing just as easy as the DataContractSerializer, and can be dropped in place with little effort.</p>
<p>Unfortunately when I tried to just drop SharpSerializer into place I hit an exception and ultimately had to download the source and step through it to discover that if you have a class that implements the IEnumerable interface it must also implement the ICollection interface or you get an InvalidCastException. After fixing that issue I did the same timing tests as with the DataContractSerializer and got the following results:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="205" valign="top"><strong>SharpSerializer</strong></td>
<td width="205" valign="top"><strong>Save Time (seconds)</strong></td>
<td width="205" valign="top"><strong>Load Time (seconds)</strong></td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">2.084</td>
<td width="205" valign="top">4.522</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">2.009</td>
<td width="205" valign="top">4.549</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">2.107</td>
<td width="205" valign="top">4.391</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">2.064</td>
<td width="205" valign="top">4.424</td>
</tr>
<tr>
<td width="205" valign="top"> </td>
<td width="205" valign="top">1.907</td>
<td width="205" valign="top">4.568</td>
</tr>
<tr>
<td width="205" valign="top"><strong>Average:</strong></td>
<td width="205" valign="top"><strong>2.0342</strong></td>
<td width="205" valign="top"><strong>4.4908</strong></td>
</tr>
</tbody>
</table>
<p>As you can see the timings were in fact slightly slower, now this isn&#8217;t to say that SharpSerializer is slow as I have seen a fair few sites show test data with results very different to these. What this test does show is that in <strong>my case</strong> SharpSerializer performs less well than DataContractSerializer, I&#8217;m not sure on the exact cause of that as I haven&#8217;t yet ran it through a profiler to see if the issue is with the library or the objects I&#8217;m saving out (<em>there&#8217;s a good chance the issue could be on my side</em>).</p>
<h2>Silverlight Serializer</h2>
<p>The other library that I found a few references to is called &#8216;<a href="http://whydoidoit.com/silverlight-serializer/">Silverlight Serializer</a>&#8216; which is described on it&#8217;s website as &#8220;<em>a binary serializer that works in .NET and Silverlight, allowing you to move your objects between the two instances or store and retrieve them locally in either</em>&#8221; and is created by <a href="http://whydoidoit.com/">Mike Talbot</a>. This library also offers a simple interface allowing it to again be dropped into place with minimal effort. Unfortunately I once again hit some exceptions (this time a MissingMethodException and another one I&#8217;ve forgotten now), again potentially not the fault of the library and probably something I could have fixed with a bit of investigation.</p>
<p>However, at this point however I decided to give up and take a look at writing my own.</p>
<blockquote><p><strong>This post was originally published on Roy’s personal blog at <a href="http://roybott.com/blog/serializing-to-binary-on-wp7-part-1.html" target="_blank">http://roybott.com/blog/serializing-to-binary-on-wp7-part-1.html</a> but we brought it here to share with you.</strong></p></blockquote>
<div id="_mcePaste" class="mcePaste" style="position: absolute; width: 1px; height: 1px; overflow: hidden; top: 0px; left: -10000px;">﻿</div>
]]></content:encoded>
			<wfw:commentRss>http://verysoftware.co.uk/blog/serializing-to-binary-on-wp7-part1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

