{"id":1913,"date":"2014-02-06T15:00:00","date_gmt":"2014-02-06T15:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2014\/02\/06\/triggering-prefetch-for-windows-store-apps-in-visual-studio-2013-update-2\/"},"modified":"2022-07-18T05:54:15","modified_gmt":"2022-07-18T13:54:15","slug":"triggering-prefetch-for-windows-store-apps-in-visual-studio-2013-update-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/triggering-prefetch-for-windows-store-apps-in-visual-studio-2013-update-2\/","title":{"rendered":"Triggering Prefetch for Windows Store Apps in Visual Studio 2013 Update 2"},"content":{"rendered":"<p>Windows 8.1 introduced a method for Windows Store App developers to pre-register content with the Windows API to cache resources to be used by your apps before your app has been launched. This was to address the issue where your application loads slowly on startup while it waits to pull down the latest information. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/apps\/windows.networking.backgroundtransfer.contentprefetcher.aspx\">You can read more about ContentPrefetcher on MSDN.<\/a><\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/bharry\/archive\/2014\/02\/05\/vs-tfs-2013-2-update-2-ctp-released.aspx\" title=\"Visual Studio 2013 Update 2\">Visual Studio 2013 Update 2<\/a> adds a way to manually trigger Prefetch from the Debug menu. Normally, the Windows system will cache your resources based off of an internal heuristic determined by how often your app has been run. During development, however, the programmer may want to manually trigger this caching to test their program\u2019s behavior to validate that ContentPrefetcher is properly registered. Since the code path that registers these resources are contained within your app itself, there are a number of steps to take to properly test your app.<\/p>\n<h3>Walkthrough<\/h3>\n<p>We will be using the <a href=\"http:\/\/code.msdn.microsoft.com\/windowsapps\/ContentPrefetcher-Sample-432c8309\">Content prefetch sample app<\/a>. For the purposes of this post, I will be showing examples from the C# app.<\/p>\n<h4>Step 1 \u2013 Create or Open Your Windows Store App with Prefetch<\/h4>\n<p>In the sample app, we can do this from the menu system. Enter the URI you wish to cache and add it to the list.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2014\/02\/3312.prefetch1.png\" alt=\"\" width=\"277\" height=\"382\" border=\"0\" \/><\/p>\n<p>For your own app, you can programmatically add URIs to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/apps\/windows.networking.backgroundtransfer.contentprefetcher.aspx\">ContentPrefetcher API<\/a>. Here is some example code.<\/p>\n<p>\u00a0<\/p>\n<pre class=\"scroll\"><code class=\"csharp\"> Windows.Networking.BackgroundTransfer.ContentPrefetcher.ContentUris.Clear();&lt;br \/> string url = \"xhttp:\/\/i.s-microsoft.com\/global\/ImageStore\/PublishingImages\/logos\/hp\/logo-lg-1x.png\";&lt;br \/> Windows.Networking.BackgroundTransfer.ContentPrefetcher.ContentUris.Add(new Uri(url));<\/code><\/pre>\n<p>\u00a0<\/p>\n<h4>Step 2 \u2013 Build and Launch your App<\/h4>\n<p>Before you can trigger Prefetch, you need to register your resources with the Windows API. Build and launch your app and make sure the code containing your registrations has been executed. Now you\u2019re free to trigger Prefetch. To investigate your startup behavior, close your app to re-launch later.<\/p>\n<p>Using the sample project, we will execute this code path when clicking Add Uri.<\/p>\n<h4>Step 3 \u2013 Trigger Prefetch<\/h4>\n<p>The Trigger Prefetch option is found under Debug -> Debug Other Targets -> Trigger Prefetch. When executed successfully, your cache now contains all the items you have registered. You can now launch your app to observe the preloaded cache behavior.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2014\/02\/8780.prefetch2.png\" alt=\"\" border=\"0\" \/><\/p>\n<h4>Step 4 \u2013 Ensure Prefetch has worked correctly<\/h4>\n<p>After triggering Prefetch, relaunch your app to see how it behaves with your resources cached. In the sample app, you can select a different scenario and click back to see the table update.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2014\/02\/6557.prefetch3.png\" alt=\"\" width=\"264\" height=\"127\" border=\"0\" \/><\/p>\n<h4>Step 5 \u2013 Edit and Relaunch<\/h4>\n<p>Now that you\u2019ve seen how Prefetch works, you can now choose to add or edit items in your registration. Before you try to trigger Prefetch again, however, the previous resource registrations are still saved. To update the registrations with your changes, you will need to re-launch your app before attempting Step 3 again.<\/p>\n<p><em>[note] For the sample app, the cache does not refresh unless you uninstall the app from the Start Menu. Right click the app and select uninstall before trying again.<\/em><\/p>\n<h3>Status Dialogs<\/h3>\n<p>Here are a list of status dialogs you may encounter for specific corner cases that may cause Prefetch to fail. Use this reference to diagnose your issue if you encounter one of these dialogs.<\/p>\n<h4>\u201cYour startup project is not a Windows Store App project.\u201d<\/h4>\n<p>ContentPrefetcher uses a Windows System API call to be used by Windows Store Apps. If your startup project is not a Store App, ContentPrefetcher is not available for you to use. We surface this dialog when we detect your startup project type does not match.<\/p>\n<h4>\u201cPrefetch cannot be started. Start and close your app before trying again.\u201d<\/h4>\n<p>Before we can determine if you have resources registered for Prefetch, we need to have your app deployed from the current instance of Visual Studio. If you just opened your Visual Studio solution or if you haven\u2019t built and run your app yet, we need you to do so before triggering Prefetch.<\/p>\n<h4>\u201cNo content has been registered or content has been incorrectly registered.\u201d<\/h4>\n<p>In this case, we just tried to trigger Prefetch, but there weren\u2019t any resources registered to cache. This is caused when the code path that contains your resource registration was not executed, or your resource registration did not execute properly. Double check your code to make sure it correctly follows the API syntax, then re-launch your app and ensure you execute the registration code.<\/p>\n<h4>\u201cVisual Studio cannot reach the device\u201d<\/h4>\n<p>If you are remote debugging, this means that we could not reach the target device to trigger Prefetch. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh441469.aspx#BKMK_Choosing_the_remote_device_for_C__and_Visual_Basic_projects\">Check your settings in the Project Properties<\/a> to ensure that you have specified the correct remote device.<\/p>\n<h4>\u201cYour Prefetch resources might be stale\u201d<\/h4>\n<p>This is a general non-blocking warning that displays if you have edited your project but haven\u2019t launched your app before triggering Prefetch. Registering your resources for caching occurs whenever you launch your app. The Windows system does not reflect any changes you\u2019ve made to the registration code until you\u2019ve relaunched your app after making edits. Relaunch your app to update resource registrations.<\/p>\n<h3>In Closing<\/h3>\n<p>For Windows Store App developers, I hope this feature enable you to create more responsive apps for your customers. If you have any feedback on this, please let me know in the comments or in our <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/vstudio\/en-US\/home?forum=vsdebug\">MSDN forum<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows 8.1 introduced a method for Windows Store App developers to pre-register content with the Windows API to cache resources to be used by your apps before your app has been launched. This was to address the issue where your application loads slowly on startup while it waits to pull down the latest information. You [&hellip;]<\/p>\n","protected":false},"author":74,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,225],"tags":[],"class_list":["post-1913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-git"],"acf":[],"blog_post_summary":"<p>Windows 8.1 introduced a method for Windows Store App developers to pre-register content with the Windows API to cache resources to be used by your apps before your app has been launched. This was to address the issue where your application loads slowly on startup while it waits to pull down the latest information. You [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/1913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/74"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=1913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/1913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=1913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=1913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=1913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}