<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/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>Derik Whittaker : HowTo, XAML</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/XAML/default.aspx</link><description>Tags: HowTo, XAML</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>How to setup a clean error message when your Metro Application does not support Data Sharing</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/10/09/how-to-setup-a-clean-error-message-when-your-metro-application-does-not-support-data-sharing.aspx</link><pubDate>Sun, 09 Oct 2011 11:54:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68259</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=68259</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=68259</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/10/09/how-to-setup-a-clean-error-message-when-your-metro-application-does-not-support-data-sharing.aspx#comments</comments><description>&lt;p&gt;When building a WinRT/Metro application one of the key integration points between your application and others is to allow Data Sharing via the Sharing Contracts.&amp;nbsp; By default applications DO NOT support sharing and when a user taps on the Share Charm they will see an error similar to the following.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_4427342B.png"&gt;&lt;img title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;display:inline;padding-right:0px;" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_11C360AC.png" border="0" height="223" width="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This message is NOT that user friendly and should be avoided.&amp;nbsp; Besides what good does a screenshot of your application really do anyone &lt;img class="wlEmoticon wlEmoticon-smile" style="border-top-style:none;border-bottom-style:none;border-right-style:none;border-left-style:none;" alt="Smile" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/wlEmoticon_2D00_smile_5F00_1F2973B2.png" /&gt;.&lt;/p&gt;
&lt;p&gt;If you are explicitly not going to support sharing from a given view in your application you should provide the user a little better message telling them this, fortunately setting this up is a piece of cake.&lt;/p&gt;
&lt;p&gt;To setup this clean user friendly message we simply need to do 3 things&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Grab the DataTransfer Context for the current view&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Add the following using clause : using Windows.ApplicationModel.DataTransfer&lt;/p&gt;
&lt;p&gt;Grab the context as : var dtm = DataTransferManager.GetForCurrentView();&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Setup the event to handle the DataRequested event&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Wire up the DataRequested event as: dtm.DataRequested += OnDataRequested;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Populate the failure message in the Share Panel with our user friendly message&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Inside your event handler do the following:     &lt;br /&gt;args.Request.FailWithDisplayText( &amp;quot;Data Sharing Not Supported at this time&amp;quot; );&lt;/p&gt;
&lt;p&gt;The args object is of course the EventArgs which is passed into your event handler.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;One thing to pay attention to is that the amount of text which can be displayed is very short.&amp;nbsp; If you provide a value which is too long the value will be truncated and the &amp;hellip; will be appended to the end.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now that we have enabled our view to display a friendly error message when someone tries to invoke the share we should see something like the following;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_33AEC330.png"&gt;&lt;img title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;display:inline;padding-right:0px;" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_535D9CF8.png" border="0" height="151" width="244" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;(please not the &amp;hellip; added to the message.&amp;nbsp; This is what I was referring to above when I said it will truncate long messages)&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68259" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Metro/default.aspx">Metro</category></item><item><title>Lighting up your C# Metro apps by Using the Settings Contract</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/28/lighting-up-your-c-metro-apps-by-using-the-settings-contract.aspx</link><pubDate>Wed, 28 Sep 2011 22:50:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68227</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=68227</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=68227</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/28/lighting-up-your-c-metro-apps-by-using-the-settings-contract.aspx#comments</comments><description>&lt;p&gt;**** NOTE: This post is valid for the Developer Preview of WinRT and MAY change in later releases ****&lt;/p&gt;
&lt;p&gt;If you have been paying attention to any of the Windows 8/Metro stuff that has come out sense BUILD you may have noticed that with Windows 8/Metro application developers can now easily light up their applications by enabling their apps to hook into the various system Contracts.&amp;nbsp; These contracts include things like search, settings, file picker and app selection.&lt;/p&gt;
&lt;p&gt;In this post we will walk though what is needed in order to integrate into the system settings panel in Windows8 and how to present your settings to your user in a manor which makes it feel like it is built into the application.&lt;/p&gt;
&lt;p&gt;Few things before we get started.&amp;nbsp; The Windows 8 UI design guidelines have a few things to say about Settings Panels&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The settings panel should be contextual, meaning that the panel should change based on the active screen &lt;/li&gt;
&lt;li&gt;The panel should be either 346 or 646 pixels wide &lt;/li&gt;
&lt;li&gt;The panel should be able to be closed by the &amp;lsquo;light dismissal&amp;rsquo; concept.&amp;nbsp; Meaning that if someone touched off the panel it should close. &lt;/li&gt;
&lt;li&gt;The panel should use smooth animations/transitions which mimic the look/feel of the native panel in Metro &lt;/li&gt;
&lt;li&gt;The changed settings should take affect immediately, no waiting for a save or commit action. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ok enough of what it &amp;lsquo;should&amp;rsquo; do, lets move on to how to make it do these things.&amp;nbsp; What exactly is it we need to do in order to enable our application to hook into the Settings API?&lt;/p&gt;
&lt;p&gt;Step 1: Create the User Control you are going to use as your Settings Panel for the given page.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We will create our User Control and call it SettingsUserControl&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Step 2: In the Xaml of your page which can trigger the Panel add the following&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_3554D308.png"&gt;&lt;img title="image" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_29BF15C9.png" border="0" height="103" width="644" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are a few things in the above code (sorry it is a screen shot but the XAML was not rendering correctly).&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;local: SttingsUserControl &amp;ndash; local is a xmlns alias to the full namespace of my SettingsUserControl &lt;/li&gt;
&lt;li&gt;Margin=&amp;rdquo;0,0,-346,0 is needed to start the visual element off screen.&amp;nbsp; It will transition on screen later &lt;/li&gt;
&lt;li&gt;.Transitions is a way to setup the transition correctly      &lt;ol&gt;
&lt;li&gt;We use the RepositionThemeTransition which is a built in transition which allows you to avoid using a story board for your animation &lt;/li&gt;
&lt;/ol&gt;   &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Step 3: In the code behind, or better yet the View Model, configure the Panel so that it can be hooked into the Win8 Panel&lt;/p&gt;
&lt;pre class="c-sharp" name="code"&gt;pane = SettingsPane.GetForCurrentView();
var preferencesCommand = new SettingsCommand(KnownSettingsCommand.Preferences,
    (handler) =&amp;gt;
    {
        SettingsUserControl.Margin = ThicknessHelper.FromUniformLenght(0);
&lt;br /&gt;&amp;nbsp;}
);

pane.ApplicationCommands.Add(preferencesCommand);&lt;/pre&gt;
&lt;p&gt;Couple of things about the code above&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;By setting the margin you are triggering it to be visible&lt;/li&gt;
&lt;li&gt;You do not need to manually enable the control to be visible.&lt;/li&gt;
&lt;li&gt;To be a good citizen and to follow the guidelines you need to enable light dismissal and hide the control when the user presses off the control (see step 5)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Step 4: Run your application and open up the Setting Charm&lt;/p&gt;
&lt;p&gt;In order to open up the Settings Charm all you need to do is call the following&lt;/p&gt;
&lt;p&gt;Windows.UI.ApplicationSettings.SettingsPane.Show();&lt;/p&gt;
&lt;p&gt;By calling the line above the pane will pop out and you should see a line which says &amp;#39;Preference&amp;#39;.&amp;nbsp; If you click on this you should see your user control slide into view.&lt;/p&gt;
&lt;p&gt;Step 5: Enabling Light Dismissal&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Handle the PointerPressed Event on the form which is visible&lt;/li&gt;
&lt;li&gt;Check to see if the right margin is 0, if so reset it&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="c-sharp" name="code"&gt;private void LayoutRoot_PointerPressed(object sender, PointerEventArgs args)
{
     if ( SettingsUserControl.Margin.Right == 0 )
     {
          SettginsUserControl.Margin = ThicknessHelper.FromLengths(0,0,-346,0);
     }
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you can see the plumbing for showing a settings screen is not very difficult or lengthy.&lt;/p&gt;
&lt;p&gt;Till next time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68227" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Metro/default.aspx">Metro</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Settings+Charm/default.aspx">Settings Charm</category></item><item><title>Lighting up your C# Metro apps by Enabling Search–Part 3</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/26/lighting-up-your-c-metro-apps-by-enabling-search-part-3.aspx</link><pubDate>Mon, 26 Sep 2011 22:40:23 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68221</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=68221</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=68221</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/26/lighting-up-your-c-metro-apps-by-enabling-search-part-3.aspx#comments</comments><description>&lt;p&gt;If you have been paying attention to any of the Windows 8/Metro stuff that has come out sense BUILD you may have noticed that with Windows 8/Metro application developers can now easily light up their applications by enabling their apps to hook into the system search mechanism. This post (and subsequent ones) will walk though how to enable this in your application. &lt;/p&gt;  &lt;p&gt;Window 8 has enabled developers to hook into various system ‘&lt;a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh464906(v=VS.85).aspx#search_"&gt;Contracts&lt;/a&gt;’ such as Search, Sharing and Settings. Contracts are like agreements between Windows and one or more Metro style apps, called contract participants, to support some kind of user interaction. There is a contract for each type of interaction, like playing music from an application to a connected stereo, and the contract specifies the types of capabilities the participant has.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Enabling Search&lt;/b&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2011/09/25/lighting-up-your-c-metro-apps-by-enabling-search-part-1.aspx"&gt;Enabling Simple Search Integration&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2011/09/25/lighting-up-your-c-metro-apps-by-enabling-search-part-2.aspx"&gt;Enabling Real time Search Integration&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Enabling Search Suggestions (this post) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In this post we will take a look at how your application can provide context sensitive suggestions to the search panel.&amp;#160; Adding these suggestions can provide your &lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_2DAB9B79.png"&gt;&lt;img title="image" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;float:right;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="image" align="right" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_701E3DAF.png" width="244" height="172" /&gt;&lt;/a&gt;user the ability to not have to type the entire search term in order to perform a search.&amp;#160; It also provides a list of known keywords that your application can use which by default provides your user a higher level of confidence that the keyword they are searching on will return data.&lt;/p&gt;  &lt;p&gt;How do we setup the ability to provide keywords to the Search Panel?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 1: Get the instance of the Current Search Pane&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Add a reference to the Windows.ApplicationModel.Search Namespace. &lt;/li&gt;    &lt;li&gt;Grab the instance of the pane by using the following syntax      &lt;br /&gt;SearchPane.GetForCurrentView() &lt;/li&gt;    &lt;li&gt;Store the instance of this pane as a global variable/property &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Step 2: Hookup your applications to the correct eventyo&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Using the Pane instance you gathered in step 1, wire up the following event      &lt;ol&gt;       &lt;li&gt;SuggestionsRequested &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Step 3: Build a list of keywords to provide as suggestions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In my code base I have a repository method which will parse a list of keywords and return them as a List&amp;lt;string&amp;gt;.&amp;#160; One thing to pay attention to is that your string can only contain ONE WORD.&amp;#160; I have tried this with multiple words and it does not appear to work.&amp;#160; I don’t know if this is a Developer Preview limitation or if this is intended behavior but either way only put a single word per line in your search result list&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 4: Handle the SuggestionsRequested Event callback&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In the handler of your SuggestionsRequested event you need to push your list of suggestion keywords into the Requested args as follows&lt;/p&gt;  &lt;p&gt;args.Request.SearchSuggestionCollection.AppendQuerySuggestions(suggestions);&lt;/p&gt;  &lt;p&gt;Once you have pushed your list of suggestions into the SearchSuggestionCollection you should now see your suggestions in the search panel when searching on your application.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68221" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Metro/default.aspx">Metro</category></item><item><title>Setting the Initial Rotation in a C# Metro Applicatio​n</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/24/setting-the-initial-rotation-in-a-c-metro-applicatio-n.aspx</link><pubDate>Sat, 24 Sep 2011 12:06:18 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68206</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=68206</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=68206</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/24/setting-the-initial-rotation-in-a-c-metro-applicatio-n.aspx#comments</comments><description>&lt;p&gt;If you have started to play with the WinRT /Win8 developer preview bits you have noticed that there are several project templates to choose from:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Application &lt;/li&gt;    &lt;li&gt;Grid Application &lt;/li&gt;    &lt;li&gt;Split Application &lt;/li&gt;    &lt;li&gt;Class Library &lt;/li&gt;    &lt;li&gt;Unit Tests (finally they get&amp;#160;&amp;#160;&amp;#160;&amp;#160; the idea that testing is important) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;One thing that you need to pay attention to if you pick the empty &amp;#39;Application&amp;#39; Template is that by default the initial rotation of the UI is set to Portrait (this is true for the Developer Preview, may not be true in future builds).&amp;#160; Having the UI set to Portrait is great if that is your intent, however I am going to speculate that most apps should start in Landscape mode.&lt;/p&gt;  &lt;p&gt;Of course you can set the default or supported orientation for each view in your application, but you should also set the default orientation at the project level as well to avoid disorientating your user if it starts in one orientation and suddenly switches to another.&lt;/p&gt;  &lt;p&gt;To change the default orientation in the application you need to do the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Find the Package.AppManifest&amp;#160;&amp;#160;&amp;#160;&amp;#160; file in your project and double click on it &lt;/li&gt;    &lt;li&gt;Scroll to the very bottom of the page, look for the &amp;#39;Initial Rotation&amp;#39; combo box &lt;/li&gt;    &lt;li&gt;Change the initial rotation to the rotation/orientation you desire.&amp;#160; In most cases I assume this will Landscape. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There you have it,this is how you set the default Initial Rotation for you C# Metro Application.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68206" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WinRT/default.aspx">WinRT</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Metro/default.aspx">Metro</category></item><item><title>Convention Based View Model Location using Ninject in a MVVM/Silverlight Application</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/07/02/view-model-location-using-ninject-in-a-mvvm-silverlight-application.aspx</link><pubDate>Sat, 02 Jul 2011 11:23:53 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:67852</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=67852</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=67852</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/07/02/view-model-location-using-ninject-in-a-mvvm-silverlight-application.aspx#comments</comments><description>***** UPDATE *****   &lt;br /&gt;If you would like to see a video on this topic check out &lt;a href="http://dimecasts.net/Casts/CastDetails/194"&gt;Episode 194 @ Dimecasts.Net&lt;/a&gt;   &lt;br /&gt;*******************  &lt;p&gt;When building out a Silverlight application (or WPF application for that matter) the &lt;a href="http://en.wikipedia.org/wiki/Model_View_ViewModel"&gt;MVVM&lt;/a&gt; pattern is the pattern of choice for most projects I am aware of.&amp;#160; When using MVVM there is a sub pattern which is commonly used which is known as ViewModelLocator.&amp;#160; The ViewModelLocator concept basically tries to handle the repetitive task of coupling the View with its ViewModel but doing so in a way which removes any manual coding and it just ‘happens’.&lt;/p&gt;  &lt;p&gt;Now if you have done any research on the ViewModel Location I am sure you have run across countless other blog posts out there which explain how to implement in this code, and many of them are great.&amp;#160; In fact here are a few which helped me down the path of creating the code I am going to talk about in this post&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum"&gt;Simple ViewModel Locator&lt;/a&gt; for MVVM by John Papa (this is the post which really kick started this pattern) &lt;/li&gt;    &lt;li&gt;Wiring up &lt;a href="http://blog.roboblob.com/tag/viewmodellocator/"&gt;ViewModel Locator&lt;/a&gt; w/ Silverlight &lt;/li&gt;    &lt;li&gt;MVVM and &lt;a href="http://geekswithblogs.net/bdiaz/archive/2010/03/31/kiss-and-tell---mvvm-and-the-viewmodellocator.aspx"&gt;ViewModel Location&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Another MVVM and &lt;a href="http://geekswithblogs.net/bdiaz/archive/2010/03/31/kiss-and-tell---mvvm-and-the-viewmodellocator.aspx"&gt;ViewModel Location&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://caliburnmicro.codeplex.com/"&gt;Caliburn.Micro&lt;/a&gt; has an implementation &lt;/li&gt;    &lt;li&gt;&lt;a href="http://mvvmlight.codeplex.com/"&gt;MVVMLight&lt;/a&gt; has an implementation &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;All of the above posts basically show how to implement this pattern in your application and from what I can tell all of them work and will do the job. However, none of those (minus the Caliburn.Micro) implementations fulfills the one requirement I want which is to totally remove the need to ‘wire’ the ViewModel up to your View if even via XAML.&amp;#160; I want to remove this wiring from the XAML because in my mind this still leaves me hard wiring the relationship rather than using convention based wiring.&amp;#160; One scenario in which the hard wiring could turn around and bite you is in the times where you rename the view and the view model.&amp;#160; If you fail to update your wiring code in XAML you will receive runtime errors.&lt;/p&gt;  &lt;p&gt;Here are a few examples:    &lt;br /&gt;John Papa’s Implementation&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;DataContext=&amp;quot;{Binding Source={StaticResource VMLocator}, Converter={StaticResource VMIndexerConverter}, ConverterParameter=MainPageViewModel}&amp;quot;&lt;/pre&gt;

&lt;p&gt;ViewModelLocator’s Implementation&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;    &amp;lt;UserControl.Resources&amp;gt;
        &amp;lt;ViewModels:EditUsersViewModelLocator x:Key=&amp;quot;viewModelLocator&amp;quot;  /&amp;gt;
    &amp;lt;/UserControl.Resources&amp;gt;
    &amp;lt;UserControl.DataContext&amp;gt;
        &amp;lt;Binding Source=&amp;quot;{StaticResource viewModelLocator}&amp;quot; Path=&amp;quot;ViewModel&amp;quot; /&amp;gt;
    &amp;lt;/UserControl.DataContext&amp;gt;&lt;/pre&gt;

&lt;p&gt;I wanted my implementation of toe ViewModelLocator pattern to NOT need to know anything about the actual View or the ViewModel either in the XAML or in the code behind.&amp;#160; What I wanted as the following:&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;DataContext=&amp;quot;{Binding Source={StaticResource VMLocator}, Converter={StaticResource VMIndexerConverter}}&amp;quot;&lt;/pre&gt;

&lt;p&gt;Now you may be looking at my XAML above and comparing it to the XAML from John Papa’s post and be thinking what is really different?&amp;#160; The difference is in this segment of code &lt;em&gt;ConverterParameter=MainPageViewModel &lt;/em&gt;In his example he ‘hard wires’ the ViewModel which is being used up to the page and I did not want to do that.&amp;#160; I wanted to use a standard naming convention (right, &lt;a href="http://en.wikipedia.org/wiki/Convention_over_configuration"&gt;convention over configuration&lt;/a&gt;) and allow my locator to determine the ViewModel based on the current View.&lt;/p&gt;

&lt;p&gt;So how did I implement my ViewModelLocator pattern, well lets take a look.&lt;/p&gt;

&lt;p&gt;Code for ViewModelLocator&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class ViewModelLocator
{   
    private static readonly IDictionary&amp;lt;type, type&amp;gt; s_viewModelCache = new Dictionary&amp;lt;type, type&amp;gt;();
        
    public Type GetViewModelTypeForView( Type viewType )
    {
        var viewModelName = ResolveViewModelName(viewType.Name).ToLower();

        // We are try to cache the types in order to remove the assembly scan hit, this is optional of course and 
	// you may not want or need this
	// One thing to point out here is that this code ASSUMES that your view and viewmodel classes are in the 
	//  same assembly.  if this is not true for your project this code will not work as expected.  We made this
	//  assumption because it is true in our project
        var foundViewModelType = GetExistingTypeFromCache(viewType) ?? (from type in viewType.Assembly.GetExportedTypes()
                                                                        where viewModelName.Equals(type.Name.ToLower())
                                                                        select type).FirstOrDefault();
    
        // this will throw a custom exception type, you can use your own if you want
        if (foundViewModelType == null) { throw new ViewModelTypeNotResolvedException(viewModelName, viewType); }

        // Add the found view and viewModel to the cache
        AddToCache(viewType, foundViewModelType);

        return foundViewModelType;
    }

    private void AddToCache(Type viewType, Type foundViewModelType)
    {
        if (!s_viewModelCache.ContainsKey(viewType))
        {
            s_viewModelCache.Add(viewType, foundViewModelType);
        }
    }

    private Type GetExistingTypeFromCache( Type viewType )
    {
        if (s_viewModelCache.ContainsKey(viewType))
        {
            return s_viewModelCache[viewType];
        }

        return null;
    }

    private string ResolveViewModelName(string viewTypeName)
    {
        var asLowerViewTypeName = viewTypeName.ToLower();

	// ***** Below is OUR product&amp;#39;s current naming conventions, replace with your own if you want *****
        // check to see if the view ends w/ &amp;quot;View&amp;quot;
        if (asLowerViewTypeName.EndsWith(&amp;quot;view&amp;quot;))
        {
            return asLowerViewTypeName.Replace(&amp;quot;view&amp;quot;, &amp;quot;ViewModel&amp;quot;);
        }

        if (asLowerViewTypeName.EndsWith(&amp;quot;usercontrol&amp;quot;))
        {
            return asLowerViewTypeName.Replace(&amp;quot;usercontrol&amp;quot;, &amp;quot;ViewModel&amp;quot;);
        }

        if (asLowerViewTypeName.EndsWith(&amp;quot;childwindow&amp;quot;))
        {
            return asLowerViewTypeName.Replace(&amp;quot;childwindow&amp;quot;, &amp;quot;ViewModel&amp;quot;);
        }

        return string.Concat(viewTypeName, &amp;quot;ViewModel&amp;quot;);
    }

    public static IDictionary&amp;lt;type, type&amp;gt; ViewModelCache { get { return s_viewModelCache; } }
}&lt;/pre&gt;

&lt;p&gt;Code for IndexConverter&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class IndexerConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    {
        Contract.Requires&amp;lt;ArgumentNullException&amp;gt;(value != null);

        var locator = value as ViewModelLocator;

        Contract.Assume(locator != null,
                        string.Format(
                            &amp;quot;The provided converter value was expected to be of type {0} but was provided as type {1} which is not acceptable for this converter&amp;quot;,
                            typeof(ViewModelLocator).Name,
                            value.GetType().Name));

        var viewType = GetViewType();

        var viewModelType = locator.GetViewModelTypeForView(viewType);

	// We are using the ServiceLocator pattern which is wrapping Ninject
        var viewModelInstance = ServiceLocator.Current.GetInstance(viewModelType);

        return viewModelInstance;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
        
    // This does incur a perf hit by looking at the stack trace, but in 
    //  my opinion this hit is worth it compared to having to manually wire items up
    private Type GetViewType()
    {
        var stack = new StackTrace();

        return stack.GetFrames()
            .Select(f =&amp;gt; f.GetMethod())
            .Where(m =&amp;gt; m.Name == &amp;quot;InitializeComponent&amp;quot;)
            .Select(m =&amp;gt; m.DeclaringType)
            .FirstOrDefault();
    }

}&lt;/pre&gt;

&lt;p&gt;Code needed in your App.Xaml&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;&amp;lt;MVVMLib:ViewModelLocator x:Key=&amp;quot;VMLocator&amp;quot; /&amp;gt;
&amp;lt;MVVMLib:IndexerConverter x:Key=&amp;quot;VMIndexerConverter&amp;quot; /&amp;gt;&lt;/pre&gt;

&lt;p&gt;Code for the custom Exception ViewModelTypeNotResolvedException&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class ViewModelTypeNotResolvedException : Exception
{
    public string ViewModelName { get; set; }
    public Type ViewName { get; set; }

    // Commented this out because i do not want to be able to create this without the VMName or the VName
    //public ViewModelTypeNotResolvedException() {}

    /// 
    /// Will build an instance of the exception and push in a default message based on the viewModelname and view properties.
    /// *** This is the lazy way to create the message ***
    /// 
    /// &lt;param name="viewModelName" /&gt;The name of the viewModel which was not able to be created or found&lt;/param&gt;
    /// &lt;param name="view" /&gt;The type for the view we were trying to bind the VM to&lt;/param&gt;
    public ViewModelTypeNotResolvedException(string viewModelName, Type view)
        : base(string.Format(&amp;quot;When trying to resolve the view model {0} it was not found in the same assembly as the view {1}&amp;quot;, viewModelName, view.Name))
    {
        ViewModelName = viewModelName;
        ViewName = view;            
    }

    /// 
    /// Will build an instance of the exception
    /// 
    /// &lt;param name="viewModelName" /&gt;The name of the viewModel which was not able to be created or found&lt;/param&gt;
    /// &lt;param name="view" /&gt;The type for the view we were trying to bind the VM to&lt;/param&gt;
    /// &lt;param name="message" /&gt;&lt;/param&gt;
    public ViewModelTypeNotResolvedException(string viewModelName, Type view, string message) : base(message)
    {
        ViewModelName = viewModelName;
        ViewName = view;
    }

    /// 
    /// Will build an instance of the exception
    /// 
    /// &lt;param name="viewModelName" /&gt;The name of the viewModel which was not able to be created or found&lt;/param&gt;
    /// &lt;param name="view" /&gt;The type for the view we were trying to bind the VM to&lt;/param&gt;
    /// &lt;param name="message" /&gt;&lt;/param&gt;
    /// &lt;param name="innerException" /&gt;&lt;/param&gt;
    public ViewModelTypeNotResolvedException(string viewModelName, Type view, string message, Exception innerException)
        : base(message, innerException)
    {
        ViewModelName = viewModelName;
        ViewName = view;
    }
}&lt;/pre&gt;

&lt;p&gt;As you can see the code needed for this is not that complicated or difficult to implement.&amp;#160; &lt;/p&gt;

&lt;p&gt;One thing to understand with this post, I am NOT saying that the other implementations are bad or they do not work. In fact I am saying the exact opposite, they all are great and they all work and I used all their implementations as baseline for my implementation.&amp;#160; What I am saying as the other examples did not meet my EXACT needs. &lt;/p&gt;

&lt;p&gt;Please take a look at the code and let me know where I went wrong or what your thoughts are.&amp;#160; &lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=67852" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Best+Practice/default.aspx">Best Practice</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Ninject/default.aspx">Ninject</category></item><item><title>How to Capture User Signatures on WP7</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/03/25/how-to-capture-user-signatures-on-wp7.aspx</link><pubDate>Fri, 25 Mar 2011 10:41:26 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:66734</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=66734</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=66734</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/03/25/how-to-capture-user-signatures-on-wp7.aspx#comments</comments><description>&lt;p&gt;Since the iPhone came out a few years ago the idea of using a stylus on a cell phone to perform any type of input action as become ‘old school’.&amp;#160; Steve Jobs once said that if your phone uses a stylus you are doing it wrong (which by the way if I remember he was taking direct aim at Windows Phone 6.5… and rightfully so).&amp;#160; &lt;/p&gt;  &lt;p&gt;As much as I agree that you should be able to interact with a phone with simply your fingers a stylus has one advantage in ONE area, signature input capture (or simply drawing).&amp;#160; Of course you can use your finger to draw/create a signature but a stylus simply makes it easier as we are more accustom to using a pen not our fingers for these things.&amp;#160; In this post I will walk though how we can capture user drawing/signature input on the WP7 platform and turn this input into a signature.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Step 1 – How to capture the signature&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a Canvas Control and place it on your form &lt;/li&gt;    &lt;li&gt;Hookup the following 3 events on the canvas      &lt;ol&gt;       &lt;li&gt;ManipulationStarted &lt;/li&gt;        &lt;li&gt;ManipulationCompleted &lt;/li&gt;        &lt;li&gt;ManipulationDelta &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Create the logic to handle the 3 events we just registered      &lt;ol&gt;       &lt;li&gt;Pre-Event Setup Code          &lt;br /&gt;          &lt;pre class="c-sharp" name="code"&gt;	private Polyline _lineBeingDrawn;
	private IList&amp;lt;ilist&amp;lt;point&amp;gt;&amp;gt; _points = new List&amp;lt;ilist&amp;lt;point&amp;gt;&amp;gt;();
	&lt;/pre&gt;
      &lt;/li&gt;

      &lt;li&gt;ManipulationStarted 
        &lt;br /&gt;

        &lt;pre class="c-sharp" name="code"&gt;// Here we want to start our new line for drawing and we want to 
// add the polyline as a child item to the SignatureCanvas.
_lineBeingDrawn = new Polyline
	{
	        Stroke = new SolidColorBrush(Colors.Orange),
        	StrokeThickness = SignatureStrokeThickness,
        	Points = new PointCollection { new Point(e.ManipulationOrigin.X, e.ManipulationOrigin.Y) }
	};


SignatureCanvas.Children.Add(_lineBeingDrawn);&lt;/pre&gt;
      &lt;/li&gt;

      &lt;li&gt;ManipulationCompleted 
        &lt;br /&gt;

        &lt;pre class="c-sharp" name="code"&gt;// Here we want to simply add the points information to the list of points we
// are capturing in order to redraw this later if needed.
_points.Add(_lineBeingDrawn.Points);&lt;/pre&gt;
      &lt;/li&gt;

      &lt;li&gt;ManipulationDelta 
        &lt;br /&gt;

        &lt;pre class="c-sharp" name="code"&gt;// Here we are adding new points to the PolyLine we created and added as a child 
// in the Started event above
_lineBeingDrawn.Points.Add(new Point(e.ManipulationOrigin.X, e.ManipulationOrigin.Y));&lt;/pre&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;strong&gt;Step 2 – How to redraw the signature on the screen&lt;/strong&gt; 

  &lt;br /&gt;After we hooked up the events above we should be able to not only draw on the canvas but we should also be storing the points for each movement into our _points collection.&amp;#160; Given this fact we can redraw the image on a clean canvas with very little effort. The code below will show how to redraw the signature.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;// We want to loop through the list of points we have been creating.  With each outer element in the list
// we create a new Polyline and then loop though each segment of that point.
// When we are done with the segments we simply add the PolyLine to the canvas
foreach (var segment in _points)
{
    _lineBeingDrawn = new Polyline
    {
        Stroke = new SolidColorBrush(Colors.Brown),
        StrokeThickness = 3.00,
    };


    foreach (var point in segment)
    {
        _lineBeingDrawn.Points.Add(new Point(point.X, point.Y));
    }

    SignatureCanvas.Children.Add(_lineBeingDrawn);
}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3 – How to create an image from the signature 
    &lt;br /&gt;&lt;/strong&gt;Now that we have been able to capture the signature and redraw it how do we save the information to a bitmap?&amp;#160; Well this is a bit more work (and this code is courtesy of one of my awesome XAML developers on my mobile team so I take NO credit, just blame for this) but is not all the hard.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;var group = new GeometryGroup();
Path path = new Path()
                {
                    Stroke = new SolidColorBrush(Colors.Black),
                    StrokeThickness = SignatureStrokeThickness,
                    Data = group
                };

foreach (var segment in _points)
{
    Point? startPoint = new Point?();
    for (int pIndex = 0; pIndex &amp;lt; segment.Count; pIndex++)
    {
        var point = segment[pIndex];
        if (pIndex == 0)
        {
            startPoint = point;
        }
        else if (startPoint != null)
        {
            var geometry = new LineGeometry
                                {
                                    StartPoint = startPoint.Value,
                                    EndPoint = point
                                };
            group.Children.Add(geometry);
            startPoint = point;
        }
    }
}

path.Measure(SignatureCanvas.DesiredSize);
path.Arrange(new Rect(new Point(), path.DesiredSize));

//fill white background
{
    Path backgroundPath = new Path()
                                {
                                    Fill = new SolidColorBrush(Colors.White),
                                    Data = new RectangleGeometry()
                                                {
                                                    Rect =
                                                        new Rect(0, 0, bitmap.PixelWidth,
                                                                bitmap.PixelHeight)
                                                }
                                };
    bitmap.Render(backgroundPath, null);
}

bitmap.Render(path, null);
bitmap.Invalidate();

//convert to binnary array
MemoryStream imgStream = new MemoryStream();
bitmap.SaveJpeg(imgStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
byte[] bitmapData = imgStream.ToArray();&lt;/pre&gt;

&lt;p&gt;As you can see creating the ability to not only capture signatures (or simply user input) and then converting the data into a bitmap is NOT hard but it does take a bit of effort. Hope this helps someone.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=66734" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WP7/default.aspx">WP7</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WindowsPhone/default.aspx">WindowsPhone</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category></item><item><title>Index was outside the bounds of the array when Obfuscating a LARGE WP7 application</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/02/22/index-was-outside-the-bounds-of-the-array-when-obfuscating-a-large-wp7-application.aspx</link><pubDate>Tue, 22 Feb 2011 18:14:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:66624</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=66624</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=66624</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/02/22/index-was-outside-the-bounds-of-the-array-when-obfuscating-a-large-wp7-application.aspx#comments</comments><description>&lt;p&gt;This is a follow up to my &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2010/12/31/obfuscating-your-wp7-applications.aspx"&gt;prior post&lt;/a&gt; on how to obfuscate a WP7 application, which is really just a call out to &lt;a href="http://weblogs.asp.net/bsimser/archive/2010/12/31/dotfuscator-deep-dive-with-wp7.aspx"&gt;Bil&amp;rsquo;s post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While trying to get a large (when I say large it takes 2+ hours just to obfuscate) WP7 application ready for publication I ran into an error while trying to obfuscate our entire .xap.&amp;nbsp; The error was&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Writing XAML: Phone.Controls.g.resources\themes/generic.xaml     &lt;br /&gt;Index was outside the bounds of the array.      &lt;br /&gt;Build Error.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No of course this error did NOT lead me to a solution so I tried a whole sort of changes to see if I could resolve it.&amp;nbsp; What I did find is that I could obfuscate each assembly on its own, but not the entire .xap.&amp;nbsp; Because I was getting desperate, and twitter was no help, I decided to email support at &lt;a href="http://www.preemptive.com/know-more/windows-phone-7"&gt;PreEmptive&lt;/a&gt;.&amp;nbsp; In my email I provided them with my full build log.&lt;/p&gt;
&lt;p&gt;Well, less than 24 hours later I received an email from their support with a few suggestions.&amp;nbsp; I tried the suggestions below as suggested:&lt;/p&gt;
&lt;p&gt;1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; From the input tab: make sure that all third party assemblies within the XAP package have been marked as &amp;ldquo;Artifact&amp;rdquo;.&amp;nbsp; (Right click on the third party dll and select &amp;ldquo;Exclude Assembly from Package&amp;rdquo;).&amp;nbsp; This will instruct Dotfuscator to obfuscate/instrument only non-third party libraries.&lt;/p&gt;
&lt;p&gt; 2.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; From the settings tab: to start, enable &amp;ldquo;Control Flow&amp;rdquo; and &amp;ldquo;String Encryption&amp;rdquo;, and disable &amp;ldquo;Renaming&amp;rdquo; (i.e. set Disable Renaming to &amp;ldquo;Yes&amp;rdquo;).&lt;/p&gt;
&lt;p&gt; 3.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If the XAP file is signed, you can provide a Certificate file by selecting the XAP file from the input tab, and selecting the properties &lt;/p&gt;
&lt;p&gt;After doing this I was able to get my app obfuscated and loaded onto my emulator. I even went and ran it again but this time turning String Encryption back on and it still worked.&amp;nbsp; Looks like the trick was to remove the 3rd party assemblies from the list of assemblies to scan, which makes sense.&lt;/p&gt;
&lt;p&gt;Anyway, the moral of the story is that when obfuscating your WP7 application make sure you omit any 3rd party assemblies and you should be right as rain.&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=66624" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Help_210021002100_/default.aspx">Help!!!</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WP7/default.aspx">WP7</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WindowsPhone/default.aspx">WindowsPhone</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/XAML/default.aspx">XAML</category></item></channel></rss>