<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mike Battista's Blog</title><link>http://blogs.msdn.com/b/mikebattista/</link><description /><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Respond to User Input</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/04/22/respond-to-user-input.aspx</link><pubDate>Sun, 22 Apr 2012 21:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10296266</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10296266</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/04/22/respond-to-user-input.aspx#comments</comments><description>&lt;p&gt;The fourth of the four principles I mentioned in &lt;a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;Optimizing Apps for Lower Cost Devices&lt;/a&gt; is Respond to User Input.&lt;/p&gt;
&lt;p&gt;A responsive UI is a basic expectation that users have of apps.&amp;nbsp; Failing to respond to user input can frustrate users and can ultimately drive them away to other apps that are more responsive.&lt;/p&gt;
&lt;p&gt;The basic guidance here is to keep as much activity off of the UI thread as possible until absolutely necessary.&amp;nbsp; The UI thread is what processes user input for you, so any code you execute on that thread will interfere with this processing.&amp;nbsp; Unless you're updating elements of your UI, you generally should not be executing code on the UI thread.&lt;/p&gt;
&lt;p&gt;A quick way to check whether a block of code is running on the UI thread is to call &lt;a title="Deployment.Current.Dispatcher.CheckAccess()" href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.checkaccess(v=vs.95).aspx"&gt;Deployment.Current.Dispatcher.CheckAccess()&lt;/a&gt;&amp;nbsp;inside that block of code (Intellisense won't show this method but it is there and useful).&amp;nbsp; This will return true if the code is executing on the UI thread, so if this returns true and you're not updating your UI, then you've found code that&amp;nbsp;could be moved to&amp;nbsp;background threads.&lt;/p&gt;
&lt;p&gt;An easy way to move code to background threads is to wrap it in a call to &lt;a title="ThreadPool.QueueUserWorkItem()" href="http://msdn.microsoft.com/en-us/library/kbf0f1ct(v=vs.95).aspx"&gt;ThreadPool.QueueUserWorkItem()&lt;/a&gt;.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;// Running on the UI thread.&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;&lt;/code&gt;&lt;code class="csharp"&gt;// Running on a background thread.&lt;br /&gt;ThreadPool.QueueUserWorkItem((o) =&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;a title="ThreadPool" href="http://msdn.microsoft.com/en-us/library/y5htx827(v=vs.95).aspx"&gt;ThreadPool&lt;/a&gt; is (as its name suggests) a pool of background threads that are waiting to execute work items for you.&amp;nbsp; &lt;a title="QueueUserWorkItem" href="http://msdn.microsoft.com/en-us/library/kbf0f1ct(v=vs.95).aspx"&gt;QueueUserWorkItem&lt;/a&gt; enables you to register blocks of code that will execute on the next available background thread in the ThreadPool.&amp;nbsp; Note that use of ThreadPools is common and encouraged&amp;nbsp;across many platforms (including &lt;a title="Windows 8" href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.aspx"&gt;Windows 8&lt;/a&gt;) so familiarizing yourself with them will serve you well in future projects.&lt;/p&gt;
&lt;p&gt;When you are ready to update your UI, you will need to return to the UI thread.&amp;nbsp; You can return to the UI thread at any time from a background thread by wrapping your code in a&amp;nbsp;call to&amp;nbsp;&lt;a title="Deployment.Current.Dispatcher.BeginInvoke()" href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher(v=vs.95).aspx"&gt;Deployment.Current.Dispatcher.BeginInvoke()&lt;/a&gt;.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;// Running on a background thread.&lt;br /&gt;ThreadPool.QueueUserWorkItem((o) =&amp;gt;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Running on the UI thread.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Deployment.Current.Dispatcher.BeginInvoke(() =&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you try to update your UI from a background thread, you will receive an UnauthorizedAccessException with the message "Invalid cross-thread access."&amp;nbsp; In these cases, wrap the offending code in a call&amp;nbsp;to &lt;a title="Deployment.Current.Dispatcher.BeginInvoke()" href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher(v=vs.95).aspx"&gt;Deployment.Current.Dispatcher.BeginInvoke()&lt;/a&gt; as shown above to execute it on the UI thread as required.&lt;/p&gt;
&lt;p&gt;In addition to being able to control whether your code executes on the UI thread or on background threads, you also have options to do the same with framework code in several places.&lt;/p&gt;
&lt;p&gt;Image decoding is an expensive operation that can harm interactivity significantly if&amp;nbsp;executed on the UI thread, particularly if you're loading many images at a time.&amp;nbsp; The framework allows you to specify &lt;a title="BitmapCreateOptions" href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapcreateoptions(v=vs.95).aspx"&gt;BitmapCreateOptions&lt;/a&gt; to control whether image decoding executes on the UI thread or on background threads.&amp;nbsp; Be sure to use the &lt;a title="BackgroundCreation" href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapcreateoptions(v=vs.95).aspx"&gt;BackgroundCreation&lt;/a&gt; option to move image decoding to background threads.&lt;/p&gt;
&lt;p&gt;The built-in ProgressBar control has &lt;a title="known performance problems" href="http://www.jeff.wilcox.name/2010/08/performanceprogressbar/"&gt;known performance problems&lt;/a&gt; that can harm UI performance as a result of its&amp;nbsp;animations executing on the UI thread.&amp;nbsp; Use the SystemTray &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; for the best performance as the &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; is a shell component that will not interfere with your UI thread.&amp;nbsp; A bonus to using the &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; is that your UX will more closely match the UX of the built-in experiences.&lt;/p&gt;
&lt;p&gt;If you're using animations in your app, use &lt;a title="Storyboards" href="http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard(v=vs.95).aspx"&gt;Storyboards&lt;/a&gt; wherever possible as these run on an important background thread known as the compositor thread and will not interfere with your UI thread.&amp;nbsp; If you're familiar with &lt;a title="Expression Blend" href="http://msdn.microsoft.com/en-us/library/cc295346(v=expression.40).aspx"&gt;Expression Blend&lt;/a&gt; it can be particularly useful for generating Storyboards.&lt;/p&gt;
&lt;p&gt;With code now running off of the UI thread for a given page, you should also focus on optimizing navigation between pages.&lt;/p&gt;
&lt;p&gt;To keep page load times and in-app navigation responsive, defer loading activities until the first frame is rendered. The guidance for &lt;a title="optimizing startup time of your MainPage" href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/04/03/optimize-startup-time.aspx"&gt;optimizing startup time of your MainPage&lt;/a&gt; applies to optimizing the startup time of subsequent page loads as well.&amp;nbsp;Enabling the &lt;a href="http://msdn.microsoft.com/en-us/library/ff941094(v=VS.92).aspx"&gt;TiltEffect&lt;/a&gt; is a great way to acknowledge user input and indicate that navigation is in progress. This will make your app look and feel more like the first party experiences as well.&lt;/p&gt;
&lt;p&gt;Many apps add page transition animations to add style to in-app navigations. While this is a great practice to make your apps more dynamic, excessive use of transition animations can delay load times, particularly when the generation/execution of the animations result in spikes in memory/CPU usage.&amp;nbsp; If transition animations are causing performance problems in your app, disabling them completely, or at least &lt;a href="http://msdn.microsoft.com/en-us/library/hh855083(v=vs.92).aspx"&gt;on low-memory devices only&lt;/a&gt;, can significantly improve in-app navigation performance.&lt;/p&gt;
&lt;p&gt;Maintaining a responsive UI is important regardless of the platform you are targeting.&amp;nbsp; By offloading work to background threads and keeping in-app navigation fast, you'll provide the best possible experience to your users.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10296266" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/best+practices/">best practices</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/interactivity/">interactivity</category></item><item><title>Handle Feature Reductions</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/04/15/handle-feature-reductions.aspx</link><pubDate>Sun, 15 Apr 2012 21:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10293928</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10293928</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/04/15/handle-feature-reductions.aspx#comments</comments><description>&lt;p&gt;The third of the four principles I mentioned in &lt;a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;Optimizing Apps for Lower Cost Devices&lt;/a&gt; is Handle Feature Reductions.&lt;/p&gt;
&lt;p&gt;With the &lt;a title="Windows Phone 7.5 Refresh" href="http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-33--Windows-Phone-75-Refresh--60-more-Opportunity"&gt;Windows Phone 7.5 Refresh&lt;/a&gt;, we refactored the OS in several areas to reduce memory usage and free up more RAM for apps.&amp;nbsp; Part of this exercise included assessing the memory usage of features in the developer platform and assessing which, if any, we could afford to live without.&lt;/p&gt;
&lt;p&gt;To free up the most RAM while also being as minimally disruptive as possible,&amp;nbsp;we disabled generic background agents (&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.scheduler.periodictask(v=VS.92).aspx"&gt;PeriodicTasks&lt;/a&gt;/&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.scheduler.resourceintensivetask(v=vs.92).aspx"&gt;ResourceIntensiveTasks&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;On today&amp;rsquo;s devices, users can disable background agents for an app manually via the Settings control panel, and the system can disable background agents for an app if the maximum number of supported background agents is exceeded. These conditions are surfaced to the app via an &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.scheduler.scheduledactionservice.add(v=VS.92).aspx"&gt;InvalidOperationException&lt;/a&gt; which the app must handle.&amp;nbsp; On 256MB devices, the app receives the same InvalidOperationException received in the maximum exceeded case above when trying to &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.scheduler.scheduledactionservice.add(v=VS.92).aspx"&gt;schedule a background agent&lt;/a&gt; (since the maximum number of supported background agents is 0). This means if an app is written today to &lt;a href="http://msdn.microsoft.com/en-us/library/hh202944(v=VS.92).aspx"&gt;handle the maximum exceeded case&lt;/a&gt;, it will continue to work on 256MB devices unchanged.&amp;nbsp; Make sure your apps handle this exception path and degrade gracefully when these features are unavailable. This will benefit your app experience both on today&amp;rsquo;s generation of devices as well as on new lower cost devices. The 256MB emulator introduced in the &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=29233"&gt;WPSDK 7.1.1&lt;/a&gt; enables you to easily test this code path.&lt;/p&gt;
&lt;p&gt;If you're using PeriodicTasks today to power your live tiles, there are a few options available to you.&amp;nbsp; The first option is to switch to &lt;a title="push notifications" href="http://msdn.microsoft.com/en-us/library/ff402558%28v=VS.92%29.aspx"&gt;push notifications&lt;/a&gt;.&amp;nbsp;&amp;nbsp;A second option is to&amp;nbsp;create &lt;a title="ShellTileSchedules" href="http://msdn.microsoft.com/en-us/library/ff769548(v=vs.92).aspx"&gt;ShellTileSchedules&lt;/a&gt; to update your tiles with remote images.&amp;nbsp; A third option is to leverage the &lt;a title="ShellTile API's" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.shelltile(v=vs.92).aspx"&gt;ShellTile API's&lt;/a&gt; from your foreground app to update your tiles whenever the app is launched.&amp;nbsp; While technically the tiles wouldn't be live in this last case, the content would be refreshed, which can give the impression of live tiles.&amp;nbsp; If you allow users to flag content, for instance, you could turn your tiles into a permanent reminder space and&amp;nbsp;update&amp;nbsp;them to reflect the most recently flagged content (or toggle through flagged content with every app launch).&amp;nbsp; If these options aren't feasible for you, then&amp;nbsp;you can still always take advantage of &lt;a title="secondary tiles" href="http://msdn.microsoft.com/en-us/library/hh202948(v=VS.92).aspx"&gt;secondary tiles&lt;/a&gt; to provide convenient access to deeper experiences in your apps.&lt;/p&gt;
&lt;p&gt;In addition to PeriodicTasks and ResourceIntensiveTasks being disabled, the lower cost 7x27a chipset itself has reduced media playback capabilities that may impact playback of some of your content.&amp;nbsp; Attempting to playback unsupported content on the device will result in either (1)&amp;nbsp;a&amp;nbsp;"Sorry, we can't play this file on your phone" error, if played via the &lt;a title="MediaPlayerLauncher" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.mediaplayerlauncher(v=VS.92).aspx"&gt;MediaPlayerLauncher&lt;/a&gt;, (2)&amp;nbsp;a &lt;a title="MediaFailed" href="http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediafailed(v=vs.95).aspx"&gt;MediaFailed&lt;/a&gt; event if played via &lt;a title="MediaElement" href="http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(v=VS.95).aspx"&gt;MediaElement&lt;/a&gt;, or (3) suboptimal video playback in some cases.&amp;nbsp; While most apps in our testing during the &lt;a title="Windows Phone 7.5 Refresh" href="http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-33--Windows-Phone-75-Refresh--60-more-Opportunity"&gt;Windows Phone 7.5 Refresh&lt;/a&gt; were not impacted by these reduced capabilities, you may be impacted if&amp;nbsp;you do&amp;nbsp;exceed them.&amp;nbsp; Be sure to &lt;a title="understand the new baseline" href="http://msdn.microsoft.com/en-us/library/ff462087(v=VS.92).aspx"&gt;understand the new baseline&lt;/a&gt; and encode your content to work well across all Windows Phone devices.&amp;nbsp; To conditionally offer higher quality vs. lower quality content depending on the device, leverage the &lt;a title="MediaCapabilities.IsMultiResolutionVideoSupported" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.info.mediacapabilities.ismultiresolutionvideosupported(v=vs.92).aspx"&gt;MediaCapabilities.IsMultiResolutionVideoSupported&lt;/a&gt; flag.&amp;nbsp; This flag will be false on devices with reduced media capabilities, so be sure to fall back to the &lt;a title="baseline specs" href="http://msdn.microsoft.com/en-us/library/ff462087(v=VS.92).aspx"&gt;baseline specs&lt;/a&gt; in this case.&lt;/p&gt;
&lt;p&gt;While feature reductions are never ideal, these reductions bring with them an opportunity to reach a large new audience with your apps.&amp;nbsp; Be sure to account for these reductions when targeting&amp;nbsp;lower cost&amp;nbsp;devices to provide the best possible experience to your users.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10293928" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/best+practices/">best practices</category></item><item><title>Reduce Memory Usage</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/04/08/reduce-memory-usage.aspx</link><pubDate>Sun, 08 Apr 2012 20:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10291751</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10291751</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/04/08/reduce-memory-usage.aspx#comments</comments><description>&lt;p&gt;The second of the four principles I mentioned in &lt;a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;Optimizing Apps for Lower Cost Devices&lt;/a&gt; is Reduce Memory Usage.&lt;/p&gt;
&lt;p&gt;Per &lt;a href="http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx"&gt;certification requirement 5.2.5&lt;/a&gt;, apps should not exceed 90MB of memory usage on 256MB devices.&amp;nbsp; When thinking about targeting 256MB devices, your first order of business should be to check whether your apps exceed 90MB today.&amp;nbsp; If they don't, then reducing memory usage is less urgent (but still important as I'll mention later).&amp;nbsp; If they do, then memory tuning will be required.&lt;/p&gt;
&lt;p&gt;Leverage the &lt;a title="memory profiler" href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/02/01/memory-profiling-for-application-performance.aspx"&gt;memory profiler&lt;/a&gt; and &lt;a title="memory-related API's" href="http://msdn.microsoft.com/en-us/library/ff941122(v=vs.92).aspx"&gt;memory-related API's&lt;/a&gt; to identify areas of opportunity to improve memory usage in your app.&amp;nbsp; These tools can help you understand pretty quickly what the peak memory usage of your app is as well as what the breakdown of memory usage is across&amp;nbsp;various states of&amp;nbsp;your app.&lt;/p&gt;
&lt;p&gt;A general principle to keep in mind is that loading less will result in less memory usage.&amp;nbsp; Loading less could mean loading less data at a time, loading less/smaller content, or fixing leaks which result in more objects residing in memory over the lifetime of your app.&lt;/p&gt;
&lt;p&gt;Many apps load and display lists of data (news articles, recipes, events, search results, etc.) often across several pivots on a page.&amp;nbsp; If each of these pivots loads hundreds of list items (usually with images attached to each one), this can result in wasted memory usage particularly if (1) the user never views particular pivots or (2) the user never scrolls below 20 items in any given list.&amp;nbsp; If several pages in your app display lists of data&amp;nbsp;in this way, then memory usage can add up quickly as users navigate through your app.&amp;nbsp; Wherever possible, load less data at a time.&amp;nbsp; Defaulting to a small amount of items in each list and then loading more data as the user requests it can enable you to&amp;nbsp;load the most relevant content in your UI at once (rather than potentially harm user interactivity by loading data in response to user input like pivot selection changes)&amp;nbsp;while&amp;nbsp;keeping your app's memory footprint at a reasonable level.&lt;/p&gt;
&lt;p&gt;Asset memory (for images, sound effects, etc.) can quickly add up if you're not careful about how you load assets.&amp;nbsp; The more assets you load into memory at a time, the larger your memory footprint will become.&amp;nbsp; Not only does the amount of content you load affect memory usage, but also does&amp;nbsp;the size of the content.&amp;nbsp; Memory usage of images, for instance, can be approximated by multiplying width&amp;nbsp;* height * bpp (bits/pixel).&amp;nbsp; Higher resolution images will naturally translate into higher memory usage.&amp;nbsp; Loading a 1024x1024 image into memory only to scale it down to a 100x100 container will consume more memory than is necessary.&amp;nbsp; If you're doing this for every image in a list or&amp;nbsp;in&amp;nbsp;a photo gallery feature, then you're using a lot more memory than you need to be.&amp;nbsp; Load thumbnails where it makes sense, and load higher resolution content as the user requests it.&amp;nbsp; If your content is preloaded in your app, make sure that the resolution of the content aligns with the sizes of the containers that will host the content.&amp;nbsp; If you're loading images from a web service, check for options to load lower resolution images instead of higher&amp;nbsp;resolution versions.&amp;nbsp; &lt;a title="PictureDecoder.DecodeJpeg" href="http://msdn.microsoft.com/en-us/library/ff708027(v=vs.92).aspx"&gt;PictureDecoder.DecodeJpeg&lt;/a&gt; can generate thumbnails from high resolution image streams so use this to your advantage.&amp;nbsp;If you're &lt;a title="building a game" href="http://msdn.microsoft.com/en-us/library/hh855082(v=vs.92).aspx"&gt;building a game&lt;/a&gt;, load assets for the on-screen experience only and flush those assets when they are no longer necessary.&amp;nbsp; Hanging on to assets unnecessarily can result in OOM (out-of-memory) exceptions or general performance problems as the size of your game grows.&lt;/p&gt;
&lt;p&gt;Memory leaks can bloat your app footprint and should be identified and addressed.&amp;nbsp; Common sources of memory leaks are building circular navigation loops and failing to deregister event handlers to long-lived objects.&amp;nbsp; Apps that use a home button can result in circular navigation loops which can fill the back stack with redundant page instances. These page instances consume memory and could result in your app crashing due to an OOM (out-of-memory) exception while users navigate through your app. Avoid circular navigation loops by removing home button functionality or by leveraging the new &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.removebackentry(v=vs.92).aspx"&gt;back stack manipulation API&amp;rsquo;s&lt;/a&gt; exposed in Windows Phone 7.5.&amp;nbsp; Neglecting to deregister event handlers to static objects can prevent objects from being reclaimed by the garbage collector. Imagine a search results page where the user navigates back and forth between the details of a search result and the search results page itself. If the details pages hold on to references to long-lived objects, they could continue to reside in memory even after the user navigates back from them. Be sure to deregister event handlers to static objects when they are no longer needed to prevent such leaks. &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.phoneapplicationpage.onremovedfromjournal(v=vs.92).aspx"&gt;OnRemovedFromJournal&lt;/a&gt; is the recommended place to perform this task since it handles page closure both via backward navigations as well as via the &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.removebackentry(v=vs.92).aspx"&gt;back stack manipulation API&amp;rsquo;s&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Additionally, you should be careful with the use of memory-intensive controls like the &lt;a title="WebBrowser" href="http://msdn.microsoft.com/en-us/library/ff431812(v=VS.92).aspx"&gt;WebBrowser&lt;/a&gt; and &lt;a title="Maps" href="http://msdn.microsoft.com/en-us/library/ff941096(v=VS.92).aspx"&gt;Maps&lt;/a&gt; controls.&amp;nbsp; IE can consume a lot of memory when rendering complex websites.&amp;nbsp; This is true even today.&amp;nbsp; The WebBrowser control essentially embeds IE in your app, so if you allow users to navigate to arbitrary complex websites in your app, the memory that IE uses will be attributed to your app.&amp;nbsp; This could result in your app running out of memory if the sum of your app's memory usage plus IE's memory usage exceeds the recommended 90MB limit.&amp;nbsp; If you'd like to use the WebBrowser control, just be mindful of the content that users are allowed to load.&amp;nbsp; If the content isn't very complex, then performance/memory issues shouldn't be a concern.&amp;nbsp; If users can load complex content in the control and your app footprint is already large, you may want to consider using the &lt;a title="WebBrowserTask" href="http://msdn.microsoft.com/en-us/library/hh394020(v=vs.92).aspx"&gt;WebBrowserTask&lt;/a&gt; on 256MB devices to reduce the memory usage inside of your app.&amp;nbsp; Similarly, for the Maps control, you may want to leverage the &lt;a title="BingMapsTask" href="http://msdn.microsoft.com/en-us/library/hh394026(v=vs.92).aspx"&gt;BingMapsTask&lt;/a&gt;/&lt;a title="BingMapsDirectionsTask" href="http://msdn.microsoft.com/en-us/library/hh394024(v=vs.92).aspx"&gt;BingMapsDirectionsTask&lt;/a&gt; to offload map memory to a system process.&amp;nbsp; If a customized map experience is critical to your app, then loading fewer overlays or otherwise simplifying your map experience can help save some memory as well.&lt;/p&gt;
&lt;p&gt;As I mentioned before, if you're exceeding 90MB today, then you're likely violating some of these principles and tuning will be required to target 256MB devices.&amp;nbsp; If you're not exceeding 90MB, tuning the memory usage of your app is less urgent but&amp;nbsp;still has its benefits.&lt;/p&gt;
&lt;p&gt;To enable apps to use up to 90MB on 256MB devices without jeopardizing the overall integrity of the system, we improved paging support in the OS with the &lt;a title="Windows Phone 7.5 Refresh" href="http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-33--Windows-Phone-75-Refresh--60-more-Opportunity"&gt;Windows Phone 7.5 Refresh&lt;/a&gt; to better manage and distribute memory between background services/processes and the foreground app.&amp;nbsp; While paging is generally abstracted from you, apps may perform slower or glitch occasionally&amp;nbsp;if they push the device to its limits.&amp;nbsp; Apps that use less than 60MB of memory will generally not be impacted by paging.&amp;nbsp; Apps that use between 60MB and 90MB of memory will participate in paging, more so as you approach 90MB.&amp;nbsp; Apps that fall in this range will be more likely to suffer from slower performance or glitches depending on the rest of the activity on the system.&amp;nbsp; The smaller your app's memory footprint, the less likely it will be impacted by this variability.&lt;/p&gt;
&lt;p&gt;In addition to having better performance, apps that use less memory are more likely to &lt;a title="fast resume" href="http://msdn.microsoft.com/en-us/library/hh202866(v=VS.92).aspx"&gt;fast resume&lt;/a&gt;.&amp;nbsp; A precondition to fast resume is that the OS has enough free RAM available to keep apps dormant (in memory) in the back stack.&amp;nbsp; On 256MB devices, free RAM is not as abundant as on 512MB devices, so an app that approaches 90MB of memory usage will be &lt;a title="tombstoned" href="http://msdn.microsoft.com/en-us/library/ff817008(v=VS.92).aspx"&gt;tombstoned&lt;/a&gt; immediately as it leaves the foreground to free up RAM for the incoming app.&amp;nbsp; Apps that use less memory are more likely to be kept alive in the back stack.&amp;nbsp; It should be noted that the Task Switcher (tap and hold Back to visualize the back stack and quickly navigate to a given app)&amp;nbsp;will always be available on 256MB devices.&amp;nbsp; What we're discussing here is merely the mechanism by which a 7.1 app will fast resume (the existing app instance is returned to the foreground from memory) vs. resume from tombstoning (where the app instance is torn down when it is placed in the back stack, and a new app instance is created when the app returns to the foreground).&amp;nbsp; Apps that use less memory are more likely to benefit from fast resume.&lt;/p&gt;
&lt;p&gt;Tuning memory usage is a critical component of optimizing apps&amp;nbsp;for 256MB devices.&amp;nbsp; After becoming comfortable with these guidelines, it will become quite natural for you to build very efficient apps.&amp;nbsp; It's a lot easier to build efficient apps from the beginning than to try and tack on efficiency after you've architected your app, so take comfort in knowing that the knowledge you gain tuning your existing apps (and seeing the results you achieve) will be directly applicable to your future Windows Phone projects.&amp;nbsp; At the end of the day, your users will notice and appreciate the efficiency gains.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10291751" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/best+practices/">best practices</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/memory/">memory</category></item><item><title>Optimize Startup Time</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/03/29/optimize-startup-time.aspx</link><pubDate>Wed, 28 Mar 2012 23:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10288719</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10288719</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/03/29/optimize-startup-time.aspx#comments</comments><description>&lt;p&gt;The first of the four principles I mentioned in &lt;a title="Optimizing Apps for Lower Cost Devices" href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;Optimizing Apps for Lower Cost Devices&lt;/a&gt; is Optimize Startup Time.&lt;/p&gt;
&lt;p&gt;Fast startup is an essential component of any mobile application.&amp;nbsp; It is the first impression a user has of your app and is the first chance you have to either impress or frustrate your users.&lt;/p&gt;
&lt;p&gt;If you're wondering just how fast your startup time can get on Windows Phone, use the VS Project Templates as a starting point.&amp;nbsp; Create a new project, deploy it to the device, and experience how quickly the app starts up.&lt;/p&gt;
&lt;p&gt;A general principle to keep in mind is that any regressions in startup time from the VS Project Templates are the result of app code delaying the rendering of the first frame.&amp;nbsp; The more app code you can remove or delay before the first frame is rendered, the closer your startup time will match the baseline startup time of the VS Project Templates.&lt;/p&gt;
&lt;p&gt;To fully understand the areas of opportunity for improving startup time, it's important to understand the workflow of a launching application.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The App constructor is called in App.xaml.cs.&lt;/li&gt;
&lt;li&gt;XAML in App.xaml is parsed.&lt;/li&gt;
&lt;li&gt;Application_Launching is called in App.xaml.cs.&lt;/li&gt;
&lt;li&gt;The Page constructor of your MainPage is called.&lt;/li&gt;
&lt;li&gt;XAML in your MainPage is parsed.&lt;/li&gt;
&lt;li&gt;OnNavigatedTo is called in your MainPage.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once you understand this workflow, it&amp;nbsp;is easy to conclude&amp;nbsp;that any app code or XAML introduced in this workflow will result in startup time regressions from the VS Project Templates.&lt;/p&gt;
&lt;p&gt;To minimize the impact of app code in this startup path, you should either remove code from these events or &lt;a title="execute code on background threads" href="http://msdn.microsoft.com/en-us/library/4yd16hza(v=vs.95).aspx"&gt;execute code on background threads&lt;/a&gt; to unblock the UI thread.&amp;nbsp; Only code executing on the UI thread will have a severe impact on startup performance.&amp;nbsp; Deferring activity to&amp;nbsp;background threads will allow your first frame to render quickly.&amp;nbsp; Use &lt;a title="Dispatcher.BeginInvoke" href="http://msdn.microsoft.com/en-us/library/cc190259%28v=VS.95%29.aspx"&gt;Dispatcher.BeginInvoke&lt;/a&gt; from background threads to return back to the UI thread when necessary.&lt;/p&gt;
&lt;p&gt;To minimize the impact of XAML parsing on startup time, you can simplify and remove unnecessary XAML from both your MainPage and from App.xaml.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Remove unnecessary namespaces declared in &amp;lt;phone:PhoneApplicationPage&amp;gt;.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;If your XAML elements&amp;nbsp;aren't prefixed by a&amp;nbsp;declared namespace, then the namespace declaration can be safely removed.&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Avoid explicit declarations of default attribute values.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Grid.Row="0", for instance, is the default value if Grid.Row is not declared, so just don't declare it.&lt;/li&gt;
&lt;li&gt;SupportedOrientations="Portrait" and Orientation="Portrait" can be removed as well for portrait only pages since this is the default.&lt;/li&gt;
&lt;li&gt;Understand default values and remove redundant XAML.&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Avoid retemplating controls in App.xaml.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Retemplating controls can require complex XAML which is expensive to parse.&lt;/li&gt;
&lt;li&gt;For Metro-style apps that are theme-aware, this is less of a concern.&amp;nbsp; If you need to retemplate controls to support a branded experience, for instance, then&amp;nbsp;understand what is necessary to override and what can be omitted.&lt;/li&gt;
&lt;li&gt;If templates are only needed for particular pages, consider moving the custom templates &lt;a title="to the pages that need them" href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.resources(v=vs.95).aspx"&gt;to the pages that need them&lt;/a&gt;.&amp;nbsp; This will delay the cost of parsing the XAML until the necessary page is loaded rather than require the cost to be paid at startup.&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Normalize Style declarations.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;If you are consistently setting FontFamily, FontSize, and Foreground in a similar way on several TextBoxes, for instance, then consider &lt;a title="using Styles instead" href="http://msdn.microsoft.com/en-us/library/system.windows.style(v=VS.95).aspx"&gt;using Styles instead&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Simplify page layouts.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Don't use nested Grids and StackPanels when one root container would be sufficient to &lt;a title="layout your page" href="http://msdn.microsoft.com/en-us/library/cc645025%28v=VS.95%29.aspx#LayoutSystem_PanelsCustom"&gt;layout your page&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Declare your app bar in code.&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;This will be faster to process than parsing the XAML and is actually the only way to localize your app bar components.&lt;/li&gt;
&lt;/ol&gt;&lt;/ol&gt;
&lt;p&gt;With your first frame rendered quickly, you can then turn your attention to indicating progress to the user while the rest of your content loads.&amp;nbsp; Perceived startup time can go a long way to minimize the amount of frustration content load times can impose on users.&amp;nbsp; The first frame of your app should always include the chrome of your application (system tray, app title, page title, pivot titles, app bar, etc.) as well as a &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; to indicate that your content is loading.&amp;nbsp; You'll want to use the &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; over other progress bars as (1)&amp;nbsp;the ProgressBar control has &lt;a title="known performance issues" href="http://www.jeff.wilcox.name/2010/08/performanceprogressbar/"&gt;known performance issues&lt;/a&gt; and (2) the &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; will provide the most consistent experience with the rest of the built-in experiences on the phone.&lt;/p&gt;
&lt;p&gt;Note that the system tray and app bar are shell components and so will render more quickly than the content in your PhoneApplicationPage.&amp;nbsp; You can take advantage of this to improve perceived startup time even further.&amp;nbsp;&amp;nbsp;You'll notice in &lt;a title="the Weather app" href="http://blogs.msdn.com/b/mikebattista/archive/2012/03/11/weather-v2-0.aspx"&gt;the Weather app&lt;/a&gt; that&amp;nbsp;the system tray and app bar appear first followed by the rest of the page content.&amp;nbsp; If you're using the system tray &lt;a title="ProgressIndicator" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.progressindicator(v=VS.92).aspx"&gt;ProgressIndicator&lt;/a&gt; as recommended above to indicate progress, it will appear as well immediately when the system tray is rendered.&lt;/p&gt;
&lt;p&gt;A bonus to using both the system tray and app bar is that these components render together and provide a recognizable chrome for your app while it loads.&amp;nbsp; Many apps hide the system tray and use only an app bar.&amp;nbsp; If you've used these apps, you'll notice often that the app bar renders first before the rest of the content loads.&amp;nbsp; The lack of symmetry in this case introduced by not showing the system tray actually contributes to the perception of lag that the user experiences.&amp;nbsp; By adding the system tray, you can eliminate this perception while also providing a consistent chrome that is recognizable across apps.&amp;nbsp; If you don't want to show the system tray but want to take advantage of it for the reasons mentioned above, show the system tray on launch with an &lt;a title="opacity of 0" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.systemtray.opacity(v=vs.92).aspx"&gt;opacity of 0&lt;/a&gt;.&amp;nbsp; Using an opacity of 0 means the system tray won't affect your page layout at all.&amp;nbsp; Once the rest of your content loads, you can then &lt;a title="hide the system tray" href="http://msdn.microsoft.com/en-us/library/microsoft.phone.shell.systemtray.isvisible(v=VS.92).aspx"&gt;hide the system tray&lt;/a&gt; if you'd like.&lt;/p&gt;
&lt;p&gt;Many apps &lt;a title="use a splash screen" href="http://msdn.microsoft.com/en-us/library/ff769511(v=VS.92).aspx"&gt;use a splash screen&lt;/a&gt; to solve the perceived startup problem as it is cheap to implement and can promote the brand of the app.&amp;nbsp; While this is a great practice, you may find that after following the guidance above, the splash screen will actually slow down your startup time.&amp;nbsp; Adding a splash screen to &lt;a title="the Weather app" href="http://blogs.msdn.com/b/mikebattista/archive/2012/03/11/weather-v2-0.aspx"&gt;the Weather app&lt;/a&gt;, for instance, would actually harm its startup performance.&amp;nbsp; With your startup time optimized, assess whether a splash screen is really necessary.&amp;nbsp; Your apps will actually feel more like integrated experiences if they don't use a splash screen, so don't be afraid to leave it off completely.&amp;nbsp; The combination of fast startup time, no splash screen, and extensibility points like &lt;a title="secondary tiles" href="http://msdn.microsoft.com/en-us/library/hh202948(v=VS.92).aspx"&gt;secondary tiles&lt;/a&gt; will really make your apps shine on Windows Phone.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10288719" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/best+practices/">best practices</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/startup/">startup</category></item><item><title>Weather v2.0</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/03/11/weather-v2-0.aspx</link><pubDate>Sun, 11 Mar 2012 01:44:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10280942</guid><dc:creator>Mike Battista</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10280942</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/03/11/weather-v2-0.aspx#comments</comments><description>&lt;p&gt;Recently I updated the &lt;a title="Microsoft Weather app " href="http://windowsphone.com/s?appid=ace44e54-1dd8-df11-a844-00237de2db9e"&gt;Microsoft Weather app&lt;/a&gt; to take advantage of new Windows Phone 7.5 features like background agents, secondary tiles, and FAS.&amp;nbsp; The update hit the Marketplace today.&lt;/p&gt;
&lt;p&gt;In addition to having new features, this version was architected from the ground up for performance following many of the guidelines I call out in my recent blog post &lt;a title="Optimizing Apps for Lower Cost Devices" href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;Optimizing Apps for Lower Cost Devices&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I encourage you to follow the same performance/UX principles demonstrated by this new Weather update.&amp;nbsp; Following these principles will ensure your apps perform great regardless of the device.&lt;/p&gt;
&lt;p&gt;Hope you like the new update and have fun building for Windows Phone!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10280942" width="1" height="1"&gt;</description></item><item><title>Optimizing Apps for Lower Cost Devices</title><link>http://blogs.msdn.com/b/mikebattista/archive/2012/03/09/optimizing-apps-for-lower-cost-devices.aspx</link><pubDate>Fri, 09 Mar 2012 18:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10280534</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10280534</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2012/03/09/optimizing-apps-for-lower-cost-devices.aspx#comments</comments><description>&lt;p&gt;The recent &lt;a title="announcement" href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/02/27/get-ready-for-60-more-potential-customers.aspx"&gt;announcement&lt;/a&gt; of lower cost Windows Phones like the Lumia 610 brings with it a great opportunity for you to reach many more users with your apps.&lt;/p&gt;
&lt;p&gt;See &lt;a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx"&gt;http://windowsteamblog.com/windows_phone/b/wpdev/archive/2012/03/07/optimizing-apps-for-lower-cost-devices.aspx&lt;/a&gt; for some high-level guidance regarding how to optimize your apps for these lower cost devices.&lt;/p&gt;
&lt;p&gt;Also watch &lt;a href="http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-33--Windows-Phone-75-Refresh--60-more-Opportunity"&gt;http://channel9.msdn.com/Shows/Inside+Windows+Phone/Inside-Windows-Phone-33--Windows-Phone-75-Refresh--60-more-Opportunity&lt;/a&gt; for some more context about the release and what it means for you and your apps.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10280534" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/development/">development</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/performance/">performance</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/best+practices/">best practices</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/nokia/">nokia</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/lumia+610/">lumia 610</category></item><item><title>Geocoding and Routing with Bing Maps</title><link>http://blogs.msdn.com/b/mikebattista/archive/2011/01/18/geocoding-and-routing-with-bing-maps.aspx</link><pubDate>Tue, 18 Jan 2011 00:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10116832</guid><dc:creator>Mike Battista</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10116832</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2011/01/18/geocoding-and-routing-with-bing-maps.aspx#comments</comments><description>&lt;p&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;Location-aware applications offer some of the richest experiences on a smartphone that really showcase the smartphone's unique blend of portability and network connectivity.&amp;nbsp; These applications often use geocoding and route calculation to power their experiences.&amp;nbsp; With the Bing Maps Control and Bing Maps Web Services, you can incorporate these features into Windows Phone applications to bring your location-aware concepts to life.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Get a Bing Maps Key&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In order to access the Bing Maps Web Services for geocoding and route calculation, you'll first need to &lt;a href="http://msdn.microsoft.com/en-us/library/ff428642.aspx"&gt;get a Bing Maps key&lt;/a&gt; for your application.&amp;nbsp; This key identifies your application to Bing and is sent with every request to the Bing Maps Web Services.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Configure Service References&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The next step is to configure service references in your application to create proxy classes for interfacing with the Bing Maps Web Services.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In Visual Studio, right click on your application project and select "Add Service Reference...".&lt;/li&gt;
&lt;li&gt;Enter &lt;a href="http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc"&gt;http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc&lt;/a&gt; for the Geocode Service and click Go.&lt;/li&gt;
&lt;li&gt;Once the service is discovered, enter "GeocodeService" for the namespace and click OK.&lt;/li&gt;
&lt;li&gt;Repeat steps 1-3 using &lt;a href="http://dev.virtualearth.net/webservices/v1/RouteService/RouteService.svc"&gt;http://dev.virtualearth.net/webservices/v1/RouteService/RouteService.svc&lt;/a&gt; for the Routing Service and "RouteService" for the namespace.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;Add a Reference to the Bing Maps Control&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The Bing Maps Control ships with the Windows Phone Developer Tools but resides in an assembly that is not referenced by default in the Visual Studio project templates.&amp;nbsp; Right click on your application project, select "Add Reference", and then select the Microsoft.Phone.Controls.Maps assembly to add the necessary reference.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Import the MapHelper Class&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://www.microsoft.com/maps/isdk/silverlight/#MapControlInteractiveSdk.Tutorials.Services.Geocode"&gt;Bing Maps Interactive SDK&lt;/a&gt; provides sample code for interacting with the Bing Maps Web Services.&amp;nbsp; I felt that the necessary modularity needed to make this code truly reusable was missing, so I wrapped the sample code in a helper class called &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-10-11-68-32/Attachments.zip"&gt;MapHelper&lt;/a&gt;&amp;nbsp;that exposes three public methods of interest.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Geocode(string address, Map map)&lt;/li&gt;
&lt;li&gt;ReverseGeocode(double latitude, double longitude, Map map)&lt;/li&gt;
&lt;li&gt;CalculateRoute(string source, string destination, Map map)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These methods wrap the results from the service proxy responses into EventArgs objects and expose them in the GeocodeResultAvailable, ReverseGeocodeResultAvailable, and RouteResultAvailable events, respectively.&amp;nbsp; Simply add the attached MapHelper.cs file to your project, update its namespace to match the default namespace of your project, and then call the above methods with the desired parameters like below.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;var Map = new Map()
{
    CredentialsProvider = new ApplicationIdCredentialsProvider("[ApplicationId]")
};

Observable.FromEvent&amp;lt;GeocodeResultAvailableEventArgs&amp;gt;(
    ev =&amp;gt; MapHelper.GeocodeResultAvailable += ev,
    ev =&amp;gt; MapHelper.GeocodeResultAvailable -= ev).
    Where(args =&amp;gt; args.EventArgs.Address.Equals("One Microsoft Way Redmond WA")).
    Take(1).
    Subscribe(
    (args) =&amp;gt;
    {
        if (args.EventArgs.GeocodeResult != null)
        {
            MessageBox.Show(args.EventArgs.Address + " = " + args.EventArgs.GeocodeResult.DisplayName);
        }
        else
        {
            MessageBox.Show("No match for " + args.EventArgs.Address);
        }
    });


MapHelper.Geocode("One Microsoft Way Redmond WA", Map);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above example, I create the Map control in code for simplicity.&amp;nbsp; If you already have a Map control in XAML, you can reference it via its x:Name.&amp;nbsp; I also use the &lt;a href="http://blogs.msdn.com/b/mikebattista/archive/2010/12/02/handling-events-with-reactive-extensions.aspx"&gt;Reactive Extensions for Windows Phone&lt;/a&gt; to provide more robust event handling.&lt;/p&gt;
&lt;p&gt;To verify that you can in fact talk to the Bing Maps Web Service at this point, copy the above code into your main page's constructor.&amp;nbsp; Replace "[ApplicationId]" with your Bing Maps key created earlier, add references in your project to Microsoft.Phone.Controls.Maps, Microsoft.Phone.Reactive and System.Observable if they are not already there, and add using statements for Microsoft.Phone.Controls.Maps and Microsoft.Phone.Reactive.&amp;nbsp; If all goes well, you should receive a message box with the geocode result for "One Microsoft Way Redmond WA" when you launch your application.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Plot the Results on a Map&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The above example simply displays the results in a message box in order to demonstrate how the MapHelper class simplifies talking to the Bing Maps Web Services.&amp;nbsp; In many scenarios, you will want to plot the results on the Bing Maps Control.&lt;/p&gt;
&lt;p&gt;Attached is a &lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-10-11-68-32/Attachments.zip"&gt;Windows Phone Application Page&lt;/a&gt; I created for a local events application that contains a Bing Maps Control, a Progress Bar, and some additional map layers used for plotting results.&amp;nbsp; The code behind file contains callbacks for plotting geocodes as well as route calculations.&amp;nbsp; This particular page will try to calculate a route first and fall back to a geocode of the destination if a route cannot be calculated.&amp;nbsp; The page also contains logic so that the map will remember its state during &lt;a href="http://msdn.microsoft.com/en-us/library/ff769557(v=VS.92).aspx"&gt;tombstone scenarios&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While I don't expect this page to compile for you, I feel that it is a good reference for how to use the Bing Maps Control and MapHelper class in practice.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The possibilities for location-aware applications on smartphones are endless.&amp;nbsp; With the Bing Maps Control and the MapHelper class, you should be well equipped to light up the location-aware features of your Windows Phone applications.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10116832" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-11-68-32/Attachments.zip" length="6573" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/development/">development</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/geocoding/">geocoding</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/bing+maps/">bing maps</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/routing/">routing</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/helper/">helper</category></item><item><title>Handling Events with Reactive Extensions</title><link>http://blogs.msdn.com/b/mikebattista/archive/2010/12/02/handling-events-with-reactive-extensions.aspx</link><pubDate>Thu, 02 Dec 2010 21:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10099715</guid><dc:creator>Mike Battista</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/mikebattista/rsscomments.aspx?WeblogPostID=10099715</wfw:commentRss><comments>http://blogs.msdn.com/b/mikebattista/archive/2010/12/02/handling-events-with-reactive-extensions.aspx#comments</comments><description>&lt;p&gt;If you've had a chance to play around with &lt;a href="http://msdn.microsoft.com/en-us/library/ff402535(v=VS.92).aspx"&gt;Silverlight for Windows Phone&lt;/a&gt;, you've likely realized the importance of asynchronous programming and event handling.&amp;nbsp; Pushing tasks off thread keeps your UI responsive and allows users to interact with your application while you perform important tasks in the background.&lt;/p&gt;
&lt;p&gt;While standard event handler patterns work well for simple scenarios, you may have at some point wished you had more flexibility in the way you handle events.&amp;nbsp; Maybe you'd like to filter the events firing at you and only respond to a subset of those events based on criteria that you specify.&amp;nbsp; Maybe you've listened for a stream of web service responses only to find that a user action has invalidated that stream, and you'd like to ignore any stale responses that might come back and just start listening for new ones.&lt;/p&gt;
&lt;p&gt;You might typically solve these problems by adding Boolean flags and other state handling artifacts into your code to manage all the state required to describe your scenarios.&amp;nbsp; Though achievable this way, these solutions add bloat to your code and distract someone reading the code from its primary objective.&lt;/p&gt;
&lt;p&gt;Over my experience building Windows Phone applications, I've encountered several common scenarios like the ones above that I was able to implement elegantly using the &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for .NET&lt;/a&gt;.&amp;nbsp; These extensions support a different model for handling events that has the flexibility and power to handle the above scenarios elegantly without requiring much effort.&amp;nbsp; The resulting solutions succinctly describe their desired behavior and at the same time avoid some of the common pitfalls associated with the standard event handler patterns.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Deregistering Event Handlers&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Let me start with a simple example of how Reactive Extensions handles a common pitfall with the standard event handler patterns.&lt;/p&gt;
&lt;p&gt;Have you ever forgotten to deregister an event handler?&amp;nbsp; If you have, you may have experienced the unpredictable behavior caused by event handlers firing an unexpected number of times.&lt;/p&gt;
&lt;p&gt;With Reactive Extensions, the deregistering of event handlers happens automatically once the behavior you describe is accomplished.&amp;nbsp; If you're only interested in one instance of an event, Reactive Extensions will deregister that event handler for you once the event is raised.&amp;nbsp; This is accomplished with code like the following.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;Observable.FromEvent(
    (EventHandler&amp;lt;EventArgs&amp;gt; ev) =&amp;gt; new EventHandler(ev),
    ev =&amp;gt; App.Initialized += ev,
    ev =&amp;gt; App.Initialized -= ev).
    Take(1).
    Subscribe(
    (args) =&amp;gt;
    {
        SearchInProgress = false;
    });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since you specify that you are only interested in one Initialized event with Take(1), the Reactive Extensions will deregister the event handler for you once it receives that one instance of the event.&amp;nbsp; The discipline of deregistering event handlers is enforced by the declaration of the behavior.&lt;/p&gt;
&lt;p&gt;Another advantage of this model is that the deregistering of the event handler is grouped along with the qualifiers for the event and the definition of the event handler, making a compact unit that clearly describes the trigger for the behavior as well as the behavior itself.&amp;nbsp; This improves code readability and is easy to extend and modify later.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Filtering Events&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;With standard event handling patterns, your event handlers are triggered every time an event is raised.&amp;nbsp; This requires that you add logic to your event handlers to ignore undesirable events, which can obscure your code's intentions.&lt;/p&gt;
&lt;p&gt;With Reactive Extensions, you can easily define qualifiers for an event stream that will filter undesirable events for you automatically.&amp;nbsp; This means that your event handler will only be triggered when a desirable event occurs, saving you from having to add filter logic.&amp;nbsp; This keeps your event handlers clean and compact and removes the risk of introducing bugs with custom filter logic.&lt;/p&gt;
&lt;p&gt;I've leveraged Reactive Extensions this way in a helper class I use to get the phone's current location.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;Observable.FromEvent&amp;lt;GeoPositionStatusChangedEventArgs&amp;gt;(
    ev =&amp;gt; GeoCoordinateWatcher.StatusChanged += ev,
    ev =&amp;gt; GeoCoordinateWatcher.StatusChanged -= ev).
    Where(args =&amp;gt; args.EventArgs.Status == GeoPositionStatus.Ready || args.EventArgs.Status == GeoPositionStatus.Disabled).
    Take(1).
    Subscribe(
    (args) =&amp;gt;
    {
        if (args.EventArgs.Status == GeoPositionStatus.Ready)
        {
            RaiseCurrentLocationAvailable(new CurrentLocationAvailableEventArgs(GeoCoordinateWatcher.Position.Location));
        }
        else
        {
            RaiseLocationServicesDisabled();
        }
    },
    () =&amp;gt;
    {
        GeoCoordinateWatcher.Stop();

        RequestInProcess = false;
    });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code is similar in structure to the previous example with a few important additions.&lt;/p&gt;
&lt;p&gt;You'll notice that before the Take(1) I have added a Where clause that specifies that I am only interested in the Ready and Disabled statuses.&amp;nbsp; With this line of code, I add qualifiers to the StatusChanged event so that my event handler doesn't have to care about extraneous events.&amp;nbsp; Not only is this clean, but the event handlers will be deregistered automatically as described above once either a Ready or Disabled status is reached.&lt;/p&gt;
&lt;p&gt;Another important addition here is the inclusion of a finalizer action in the second parameter of the Subscribe method.&amp;nbsp; The first parameter of the Subscribe method contains your typical event handler code.&amp;nbsp; This code will execute once for every interesting event that is raised.&amp;nbsp; The finalizer action will execute once after all of the interesting events have been raised.&amp;nbsp; This is your chance to perform any cleanup like you would in the finally block of a try catch statement.&amp;nbsp; You'll notice above I take this opportunity to stop the &lt;a href="http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinatewatcher.aspx"&gt;GeoCoordinateWatcher&lt;/a&gt; and update some state.&lt;/p&gt;
&lt;p&gt;MSDN has a great article about using Reactive Extensions in other ways to &lt;a href="http://msdn.microsoft.com/en-us/library/ff637517(VS.92).aspx"&gt;emulate and filter location data from the phone's location services&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Composing Multiple Web Service Requests &lt;/b&gt;&lt;/p&gt;
&lt;p&gt;A common feature among Windows Phone applications is a search page.&amp;nbsp; In some cases, a single search query might seed multiple web service requests that should all be joined and presented to the user as a single set of search results.&lt;/p&gt;
&lt;p&gt;A solution to this would be to track all of the intermediate results and present the entire set of results to the user once all of the intermediate results are obtained.&amp;nbsp; The ideal solution would not only compose the multiple web service requests into a single set of search results but would also ignore stale results from potentially old search queries.&lt;/p&gt;
&lt;p&gt;This scenario is actually called out in the &lt;a href="http://msdn.microsoft.com/en-us/library/ff431792(v=VS.92).aspx"&gt;Reactive Extensions for .NET Overview for Windows Phone&lt;/a&gt; as a typical mobile scenario for Reactive Extensions.&amp;nbsp; I was able to implement this solution with the following.&lt;/p&gt;
&lt;pre class="scroll"&gt;&lt;code class="csharp"&gt;Observable.FromEvent&amp;lt;SearchResultsAvailableEventArgs&amp;gt;(
    ev =&amp;gt; Provider.SearchResultsAvailable += ev,
    ev =&amp;gt; Provider.SearchResultsAvailable -= ev).
    Where(args =&amp;gt; args.EventArgs.Query.Equals(query)).
    Take(3).
    Subscribe(
    (args) =&amp;gt;
    {
        if (ActiveSearchQuery.Equals(query))
        {
             SearchResults[args.EventArgs.Type] = args.EventArgs.SearchResults;
        }
    },
    () =&amp;gt;
    {
        if (ActiveSearchQuery.Equals(query))
        {
            var List = new List&amp;lt;ISearchable&amp;gt;();

            List.AddRange(SearchResults[typeof(Type1)]);
            List.AddRange(SearchResults[typeof(Type2)]);
            List.AddRange(SearchResults[typeof(Type3)]);

            RaiseSearchResultsAvailable(List);
        }
    });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The basic structure of this code should look familiar as it is essentially identical to the previous example.&amp;nbsp; In this scenario, I am using one search query to seed three searches for three different types of results.&lt;/p&gt;
&lt;p&gt;With the Where clause and the Take(3), I specify that I am interested in three instances of the SearchResultsAvailable event where the results are for the search query just entered.&amp;nbsp; By passing the search query through as the Query property in the SearchResultsAvailableEventArgs, I can reference it in the Where clause with args.EventArgs.Query, which allows me to compare the entered query against the query associated with the returned search results.&lt;/p&gt;
&lt;p&gt;In both the event handler code and the finalizer action, I ignore stale results by comparing the query associated with the returned search results against the active query I have stored in the ActiveSearchQuery property.&amp;nbsp; While the results from all search queries will be processed, the search results will only be composed for results from the active search query.&amp;nbsp; This protects against overlapping search queries when the network is slow or when the user otherwise initiates overlapping search queries.&lt;/p&gt;
&lt;p&gt;Once all of the search results for the active search query are obtained, I compose the results in the finalizer action and raise an event to notify listeners that the search results are available.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The Reactive Extensions for .NET&amp;nbsp;are a powerful tool to have in your arsenal.&amp;nbsp; The examples above are practical applications of handling events with Reactive Extensions that I have encountered in my experience building Windows Phone applications.&lt;/p&gt;
&lt;p&gt;While no one article can really do justice to the power of Reactive Extensions, I hope the above examples have shed some light on the elegance and expressiveness that Reactive Extensions has to offer.&lt;/p&gt;
&lt;p&gt;For more information about what Reactive Extensions can do for you, check out &lt;a href="http://rxwiki.wikidot.com/"&gt;Rx Wiki&lt;/a&gt; as well as &lt;a href="http://channel9.msdn.com/Tags/reactive+extensions"&gt;Channel 9&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10099715" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/reactive+extensions/">reactive extensions</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/windows+phone/">windows phone</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/development/">development</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/fromevent/">fromevent</category><category domain="http://blogs.msdn.com/b/mikebattista/archive/tags/observable/">observable</category></item></channel></rss>