<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Nathan Nesbit's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/nathannesbit/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/nathannesbit/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:23:33 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Reusing ViewModels in a Universal App – Part 5</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/reusing-viewmodels-in-a-universal-app-part-5</link>
      <pubDate>Thu, 28 Aug 2014 17:40:40 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2014/08/28/reusing-viewmodels-in-a-universal-app-part-5/</guid>
      <description><![CDATA[Wow, its been quite the journey but here we are finally at the end.
To restate the previous...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">Wow, <span class="SpellE"><span class="GramE">its</span></span> been quite the journey but here we are finally at the end.</p>
<p class="MsoNormal">To restate the previous articles &ndash; We took a Windows store app that was coded without a <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>We refactored the code to have a <span class="SpellE">ViewModel</span> which is placed in a shared location, along with the Model.<span style="mso-spacerun: yes;">&nbsp; </span>The final View has almost no code and is decoupled from the XAML.<span style="mso-spacerun: yes;">&nbsp; </span>The interaction between the <span class="SpellE">ViewModel</span> and View is all facilitated by bindings, keeping the <span class="SpellE">ViewModel</span> logic abstract from the details of the View.</p>
<p class="MsoNormal">The last thing we are going to do is to develop a new View for Windows Phone and reuse both the Model and <span class="SpellE">ViewModel</span>.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;"><span style="line-height: 107%; font-size: 12pt; mso-bidi-font-size: 11.0pt;">The Phone View</span></strong></p>
<p class="MsoNormal">Coding the phone view was really simple.<span style="mso-spacerun: yes;">&nbsp; </span>I &ldquo;cheated&rdquo; a little and made the phone view very similar to the desktop view.<span style="mso-spacerun: yes;">&nbsp; </span>It consists of a <span class="SpellE">ListBox</span>, a <span class="SpellE">TextBox</span> and a bunch of buttons.<span style="mso-spacerun: yes;">&nbsp; </span>The <span class="SpellE">MainPage</span> class does the same thing as the Windows version &ndash; constructs an instance of the <span class="SpellE">ViewModel</span> and sets the <span class="SpellE">DataContext</span> to point to it.<span style="mso-spacerun: yes;">&nbsp; </span>The XAML code looks very similar &ndash; just things are in different positions and the styles are different.</p>
<p class="MsoNormal">The whole thing took me maybe 10 <span class="SpellE">mins</span> to code up and tweak the layout to get the sizes and spacing right.<span style="mso-spacerun: yes;">&nbsp;&nbsp; </span>Which shows how empowering a reusable <span class="SpellE">ViewModel</span> is.</p>
<p class="MsoNormal"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/5734.Phone%20Portrait.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/5734.Phone-Portrait.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/5734.Phone%20Portrait.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/5734.Phone-Portrait.jpg" alt="" border="0" /></a></p>
<p class="MsoNormal">I wanted to make the phone view at least a little different, so instead of 2 radio buttons to switch between Hex and Decimal I used a button on the command bar.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">Due to that difference we can&rsquo;t deal with the button the same way <span class="GramE">the we</span> did in Windows.<span style="mso-spacerun: yes;">&nbsp; </span>So I enhanced the <span class="SpellE">ViewModel</span> to support a <span class="SpellE">SwitchModeCommand</span> which toggles between the 2 modes.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">I wanted to call that out because, in my opinion, it is OK for a <span class="SpellE">ViewModel</span> to have pieces which are targeted at one platform vs the other.<span style="mso-spacerun: yes;">&nbsp; </span>When used infrequently it seems like a very practical way to develop your application.<span style="mso-spacerun: yes;">&nbsp; </span>However it would be concerning if the shared part of your <span class="SpellE">ViewModel</span> starts becoming a smaller and smaller percentage of the total <span class="SpellE">ViewModel</span> code.<span style="mso-spacerun: yes;">&nbsp; </span>If that was the case then I&rsquo;d start questioning what is going on.<span style="mso-spacerun: yes;">&nbsp; </span>On one extreme it could indicate a bad <span class="SpellE">ViewModel</span> design.<span style="mso-spacerun: yes;">&nbsp; </span>On the other extreme it could indicate that the Views are so different that sharing code isn&rsquo;t practical.<span style="mso-spacerun: yes;">&nbsp; </span>It would really be case-by-case, although most times it is bad <span class="SpellE">ViewModel</span> design.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;"><span style="line-height: 107%; font-size: 12pt; mso-bidi-font-size: 11.0pt;">Wait There&rsquo;s <span class="GramE">More</span></span></strong></p>
<p class="MsoNormal">Just like in your favourite infomercial, don&rsquo;t order yet &ndash; more cool stuff to <span class="GramE">come.</span></p>
<p class="MsoNormal">Because Views are now so easy to code we can add more than 1 for phone.<span style="mso-spacerun: yes;">&nbsp; </span>For example we could easily support a Landscape view in addition to the Portrait one we just created.</p>
<p class="MsoNormal">To do this we first want to make the <span class="SpellE">markup</span> easy to replace at runtime.<span style="mso-spacerun: yes;">&nbsp; </span>Enter the <span class="SpellE">ContentControl</span>.<span style="mso-spacerun: yes;">&nbsp; </span>This is a control which takes some data (Content) and <span class="SpellE">markup</span> (<span class="SpellE">ContentTemplate</span>).<span style="mso-spacerun: yes;">&nbsp; </span>It then combines the 2 &ndash; making the Content the <span class="SpellE">DataContext</span> of the template.<span style="mso-spacerun: yes;">&nbsp; </span>In this case our data is fixed (the <span class="SpellE">ViewModel</span>) but we can switch the template at runtime for different renderings.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">This may sound like magic, but it is actually a key piece of XAML&rsquo;s <span class="SpellE">composability</span>.<span style="mso-spacerun: yes;">&nbsp; </span>For example Button is a subclass of <span class="SpellE">ContentControl</span>.<span style="mso-spacerun: yes;">&nbsp; </span>That is what allows you to put stuff like text, images, <span class="SpellE">etc</span> inside a button.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">To make the change we take our View <span class="SpellE">markup</span> and wrap it inside a <span class="SpellE">DataTemplate</span> element and then put it in the Resources section with a key name.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Page.Resources</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DataTemplate</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> x</span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">:</span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Key</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="PortraitViewTemplate"&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.RowDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">RowDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Height</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="*" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">RowDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Height</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="Auto" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">RowDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Height</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="Auto" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.RowDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.ColumnDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ColumnDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Width</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="*" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.ColumnDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="StackGrid"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&hellip;snip&hellip;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal">We then put the <span class="SpellE">ContentControl</span> in our page and set its properties.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ContentControl</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Binding</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">ContentTemplate</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">StaticResource</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">PortraitViewTemplate</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal">Now we are prepped to add a Landscape View.<span style="mso-spacerun: yes;">&nbsp; </span>To do that I define a new <span class="SpellE">DataTemplate</span> in the resources and call it <span class="SpellE">LandscapeViewTemplate</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Inside that template we define the <span class="SpellE">markup</span> we want for the landscape view.<span style="mso-spacerun: yes;">&nbsp; </span>Then we need to add the code to switch between the 2 templates appropriately.<span style="mso-spacerun: yes;">&nbsp; </span>For that we need to:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -0.25in; mso-list: l1 level1 lfo7;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Update the manifest to indicate we support both portrait and landscape modes</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; mso-list: l1 level1 lfo7;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Give our <span class="SpellE">ContentControl</span> a name so we can reference it in our View code (appropriate since this is a View layer thing)</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -0.25in; mso-list: l1 level1 lfo7;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Set the right template for both the initial state and when orientation changes</p>
<p class="MsoNormal">And so here is the XAML:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ContentControl</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="PageContentControl"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Binding</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And here is the page code:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">MainPage</span>()</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">displayInfo</span> = </span><span class="SpellE"><span class="GramE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DisplayInformation</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.GetForCurrentView</span></span></span><span class="GramE"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">displayInfo.OrientationChanged</span> += <span class="SpellE">OnOrientationChanged</span>;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">viewModel</span> = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span class="SpellE"><span class="GramE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">StackCalculatorViewModel</span></span></span><span class="GramE"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.DataContext</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> = _<span class="SpellE">viewModel</span>;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.InitializeComponent</span></span></span><span class="GramE"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.NavigationCacheMode</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> = </span><span class="SpellE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">NavigationCacheMode</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Required</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">UpdateViewTemplate</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">UpdateViewTemplate</span>()</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">resourceName</span>;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">_<span class="SpellE">displayInfo.CurrentOrientation</span> == </span><span class="SpellE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DisplayOrientations</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Landscape</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">resourceName</span></span> = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"<span class="SpellE">LandscapeViewTemplate</span>"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">resourceName</span></span> = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"<span class="SpellE">PortraitViewTemplate</span>"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">var</span></span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> template = (</span><span class="SpellE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DataTemplate</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">) </span><span class="SpellE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Resources</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">[<span class="SpellE">resourceName</span>];</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">this</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.PageContentControl.ContentTemplate</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> = template;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">OnOrientationChanged</span>(</span><span class="SpellE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DisplayInformation</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> sender, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">object</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">args</span>)</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">UpdateViewTemplate</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;And here is the end result:</p>
<p class="MsoNormal"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/8206.Phone%20Landscape.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/8206.Phone-Landscape.jpg"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/8206.Phone%20Landscape.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/8206.Phone-Landscape.jpg" alt="" border="0" /></a></p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;"><span style="line-height: 107%; font-size: 12pt; mso-bidi-font-size: 11.0pt;">Whats</span></strong></span><strong style="mso-bidi-font-weight: normal;"><span style="line-height: 107%; font-size: 12pt; mso-bidi-font-size: 11.0pt;"> Next</span></strong></p>
<p class="MsoNormal">Obviously <span class="SpellE">MyCalc</span> is not a finished and polished app ready to be published to the app store.<span style="mso-spacerun: yes;">&nbsp; </span>So there is a decent amount of work left to get to that point beyond the <span class="SpellE">ViewModel</span> focus of this series.</p>
<p class="MsoNormal">I&rsquo;m just going to enumerate a few things that would be at the top of my list to fix, if I were to seriously try and take this to the app store.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Too many commands</strong> &ndash; right now add, subtract, multiply and divide each have their own command.<span style="mso-spacerun: yes;">&nbsp; </span>When I think about new features like square root and trig functions this would cause an explosion of commands.<span style="mso-spacerun: yes;">&nbsp; </span>Likely this should be handled in a way similar to the command to add to the text box, using the parameter to differentiate the buttons.<span style="mso-spacerun: yes;">&nbsp; </span>I&rsquo;m not really thrilled with a huge switch statement in the code, but it seems the lesser of 2 evils compared with tens of command properties and backing methods.<span style="mso-spacerun: yes;">&nbsp; </span>Seems like having a command for all the buttons which share the same number or arguments is a decent compromise between the 2 extremes.</p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;">ListBox</strong></span><strong style="mso-bidi-font-weight: normal;"> doesn&rsquo;t scroll </strong>&ndash; if one adds enough numbers to fill the viewable area of the list box, and then adds another the <span class="SpellE">ListBox</span> doesn&rsquo;t scroll the new number into view.<span style="mso-spacerun: yes;">&nbsp; </span>This could be solved in multiple ways.<span style="mso-spacerun: yes;">&nbsp; </span>One way is an attached service that you attach to the <span class="SpellE">ListBox</span> control.<span style="mso-spacerun: yes;">&nbsp; </span>It can monitor when the Items collection gets a new item added to the end, and call <span class="SpellE">ListBox.ScrollIntoView</span> with that new item.</p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;">AppBarButton</strong></span> &ndash; I don&rsquo;t like how the <span class="SpellE">AppBar</span> takes up so much space for just the one button.<span style="mso-spacerun: yes;">&nbsp; </span>I&rsquo;d like to get rid of it.<span style="mso-spacerun: yes;">&nbsp; </span>Perhaps some &ldquo;tabs&rdquo; in the UI to allow the user to expose additional settings/commands since there isn&rsquo;t enough room to put everything on a single page.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Enter Button</strong> &ndash; it would be cool if the user didn&rsquo;t have to hit Enter all the time.<span style="mso-spacerun: yes;">&nbsp; </span>Wouldn&rsquo;t it be cool to type in that second number and be able to hit Add immediately, without hitting Enter <span class="GramE">first.</span><span style="mso-spacerun: yes;">&nbsp; </span>This functionality could either be pushed into the Model, or we could have the <span class="SpellE">ViewModel</span> push and pop the number appropriately as the user types.<span style="mso-spacerun: yes;">&nbsp; </span>Either approach has its own pros &amp; cons.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Better UI</strong> &ndash; My <span class="SpellE">dev</span> design UI works, but isn&rsquo;t anywhere close to pretty or polished.<span style="mso-spacerun: yes;">&nbsp; </span>So having UI designed by someone with skills would be a big plus.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Bring Orientation Support To Desktop</strong> &ndash; Why can&rsquo;t the desktop app work in Portrait as well as Landscape?<span style="mso-spacerun: yes;">&nbsp; </span>No good reason.<span style="mso-spacerun: yes;">&nbsp; </span>Seems like there is also an opportunity for more code sharing&hellip;</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;"><span style="line-height: 107%; font-size: 12pt; mso-bidi-font-size: 11.0pt;">Wrapping it <span class="GramE">Up</span></span></strong></p>
<p class="MsoNormal">Having reusable <span class="SpellE">ViewModel</span> enabled me to get my Windows app running on phone supporting both Landscape and Portrait views really easily.<span style="mso-spacerun: yes;">&nbsp; </span>I probably spent about 30 <span class="SpellE">mins</span> coding.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">If we were to have estimated the cost to add a phone version of the app based on the code in part 1, the estimate would have been much higher than 30 <span class="SpellE">mins</span>.<span style="mso-spacerun: yes;">&nbsp; </span>To add both Landscape and Portrait views would have caused the cost to climb even more.<span style="mso-spacerun: yes;">&nbsp; </span>As well as the end result would have left us with a lot more code, raising future costs for maintenance and additional features.</p>
<p class="MsoNormal">It is easy to image going beyond the simple landscape &amp; portrait views and develop skins &ndash; user switchable views to make it look like a physical calculator, or like all the buttons are made of wood, etc&hellip;</p>
<p class="MsoNormal">I hope this series of articles have shown you the power of having a reusable view model, and also a bit of education on how to design and develop a <span class="SpellE">ViewModel</span>.</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;">As always complete code is attached.</p>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/55/36/26/MyCalc%20-%20part%205.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-55-36-26/MyCalc-_2D00_-part-5.zip">MyCalc - part 5.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Reusing ViewModels in a Universal App – Part 4</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/reusing-viewmodels-in-a-universal-app-part-4</link>
      <pubDate>Thu, 28 Aug 2014 15:40:43 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2014/08/28/reusing-viewmodels-in-a-universal-app-part-4/</guid>
      <description><![CDATA[To restate where part 3 ended &ndash; We have migrated all the buttons and TextBox to use the...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">To restate where part 3 ended &ndash; We have migrated all the buttons and <span class="SpellE">TextBox</span> to use the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>What is left is the display of the items on the stack and the mode radio buttons.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Handling Radio Buttons</strong></p>
<p class="MsoNormal">There are a few different <span class="SpellE">ViewModel</span> patterns which work with <span class="SpellE">RadioButtons</span>.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Option #1</strong> &ndash; Since <span class="SpellE">RadioButton</span> supports Command we could make a <span class="SpellE">ChangeModeCommand</span> which takes a parameter of the new mode.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">The pros of this solution are:</p>
<p class="MsoListParagraph" style="text-indent: -.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>It works well if other supported views uses controls which support commands for changing modes (i.e. some kind of button style)</p>
<p class="MsoNormal">The cons are:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Can&rsquo;t set initial button state</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Doesn&rsquo;t update state of other buttons<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="SpellE">CanExecute</span> should always return true, otherwise radio buttons will get disabled</p>
<p class="MsoNormal">Those issues can be solved, but will require more work.<span style="mso-spacerun: yes;">&nbsp; </span>Which implies we should only go this route if we need commands for the other view implementations.<span style="mso-spacerun: yes;">&nbsp; </span>At this point we haven&rsquo;t designed the phone UI, so going with a low cost solution is probably best.<span style="mso-spacerun: yes;">&nbsp; </span>Plus I&rsquo;d like to demonstrate some other techniques.</p>
<p class="MsoNormal">The next 2 options are variations on 2 way bindings.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Options #2</strong> &ndash; Add an <span class="SpellE">IsHexMode</span> and <span class="SpellE">IsDecimalMode</span> bool properties to the <span class="SpellE">ViewModel</span> and 2 way bind the <span class="SpellE">RadioButton.IsChecked</span> to them.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">The pros of this solution are:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l4 level1 lfo4;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Very simple and easy to understand</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l4 level1 lfo4;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Allows configuring initial control state</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l4 level1 lfo4;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Updates control state of all <span class="SpellE">RadioButtons</span></p>
<p class="MsoNormal">The cons are:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l4 level1 lfo4;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Only works well for <span class="SpellE">RadioButton</span> UI</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l4 level1 lfo4;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Doesn&rsquo;t scale if the number of modes increases</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Option #3</strong> &ndash; 2 way bind to the Mode property and use a converter to convert back and forth between <span class="SpellE">CalculatorMode</span> and bool.</p>
<p class="MsoNormal">The pros of this solution are:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l5 level1 lfo5;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Just 1 property regardless of number of modes</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l5 level1 lfo5;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Allows configuring initial control state</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l5 level1 lfo5;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Updates control state of all <span class="SpellE">RadioButtons</span></p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l5 level1 lfo5;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Different (or none) converters can be used for other UIs (i.e. <span class="SpellE">enums</span> work well with <span class="SpellE">ComboBoxes</span>)</p>
<p class="MsoNormal">The cons are:</p>
<p class="MsoListParagraph" style="text-indent: -.25in; mso-list: l1 level1 lfo6;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Have to be careful that we have 1 &ldquo;winner&rdquo; when buttons change (i.e. 1 <span class="SpellE">RadioButton</span> should change the mode, the others shouldn&rsquo;t even though their <span class="SpellE">IsChecked</span> property changes)</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">In this example we are going with Option #3 because not only is it a good choice, but it allows me to demonstrate a new <span class="SpellE">ViewModel</span> technique.<span style="mso-spacerun: yes;">&nbsp; </span>To implement a converter we define a class and have it implement <span class="SpellE">IValueConverter</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Since we will be using it in 2 way bindings we implement both Convert and <span class="SpellE">ConvertBack</span>.</p>
<p class="MsoNormal">If this was WPF we&rsquo;d be able to pass the <span class="SpellE">enum</span> value in as the <span class="SpellE">ConverterParameter</span>, but Windows store apps don&rsquo;t support x<span class="GramE">:Static</span> like WPF does, so we need to pass the value in as a string.<span style="mso-spacerun: yes;">&nbsp; </span>Then inside the converter parse the string into the right mode <span class="SpellE">enum</span> value.</p>
<p class="MsoNormal">Here is the implementation:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">class</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">BoolToCalculatorModeConverter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> : </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">IValueConverter</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">BoolToCalculatorModeConverter</span>()</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;</span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// nothing</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> Convert(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value, </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Type</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">targetType</span>, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> language)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">desiredMode</span> = <span class="SpellE">StringToMode</span>((</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">acutalMode</span> = (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)value;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (<span class="SpellE">desiredMode</span> == <span class="SpellE">acutalMode</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">ConvertBack</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value, </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Type</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">targetType</span>, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> language)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> state = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)value;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">desiredMode</span> = <span class="SpellE">StringToMode</span>((</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">state)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">desiredMode</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// since we don't know the right value, just return null</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">null</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE"><span class="GramE">StringToMode</span></span><span class="GramE">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">switch</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">value)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"Decimal"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Decimal</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"Hex"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Hex</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">default</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">throw</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">NotSupportedException</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">You&rsquo;ll notice in <span class="SpellE">ConvertBack</span> we solve the issue of the unchecked <span class="SpellE">RadioButtons</span> from changing the mode by returning null, which isn&rsquo;t a valid <span class="SpellE">enum</span> value.<span style="mso-spacerun: yes;">&nbsp; </span>That causes the binding to fail and not update the property.</p>
<p class="MsoNormal">Then in the XAML we first declare an instance of the converter in the Resources section, and then update the <span class="SpellE">RadioButton</span> to bind to the value using the Converter.<span style="mso-spacerun: yes;">&nbsp; </span>Here is the code for one of the <span class="SpellE">RadioButtons</span>:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">RadioButton</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="_HexSelection"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Style</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">StaticResource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">RadioButtonStyle</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="Hex"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">GroupName</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="<span class="SpellE">NumbericType</span>"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">IsChecked</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Converter</span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">={</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">StaticResource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">BoolToCalculatorModeConverter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">},</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">ConverterParameter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=Hex,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=<span class="SpellE">TwoWay</span>}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And of course we need to go into our View and rip out all the code which interacted with the <span class="SpellE">RadioButtons</span>.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Picking a Solution for Stack Content Display</strong></p>
<p class="MsoNormal">Now we are down to the last bit of View code &ndash; that dealing with populating the <span class="SpellE">ListBox</span> with the stack contents, formatting appropriately for the mode.</p>
<p class="MsoNormal">If this was WPF we could use a <span class="SpellE">MultiBinding</span> and <span class="SpellE">IMultiValueConverter</span>.<span style="mso-spacerun: yes;">&nbsp; </span>That would allow us to bind to the number and mode, and in the converter format the value appropriately and return it.<span style="mso-spacerun: yes;">&nbsp; </span>It would be re-evaluated when the values of the bindings change.<span style="mso-spacerun: yes;">&nbsp; </span>However this isn&rsquo;t available for Windows Store apps currently.</p>
<p class="MsoNormal">We could also solve this with a custom control for the content for the <span class="SpellE">ListBoxItems</span>.<span style="mso-spacerun: yes;">&nbsp; </span>But that seems like a heavy handed solution.<span style="mso-spacerun: yes;">&nbsp; </span>It also limits reusability as other Views may not be able to re-use that control for whatever reason.</p>
<p class="MsoNormal">Another way is to expose a collection of strings from the <span class="SpellE">ViewModel</span> and then update this collection when the mode changes and when the stack changes.<span style="mso-spacerun: yes;">&nbsp; </span>This can be a reasonable solution and is pretty straightforward to implement.<span style="mso-spacerun: yes;">&nbsp; </span>Something like a <a href="https://blogs.msdn.com/b/nathannesbit/archive/2008/11/03/convertingreadonlyobservablecollection.aspx"><span class="SpellE">ConvertingReadOnlyObservableCollection</span></a> can help make that happen.<span style="mso-spacerun: yes;">&nbsp; </span>When the mode changes you could either re-build the shadow collection with newly formatted <span class="GramE">strings.</span><span style="mso-spacerun: yes;">&nbsp; </span>Or the collection could be a <span class="SpellE">ViewModel</span> class with a string property for the formatted string.<span style="mso-spacerun: yes;">&nbsp; </span>Then when the mode changes one can enumerate the collection and tell each object to re-format, which as it does will raise a <span class="SpellE">PropertyChanged</span> event to cause the bindings to update.</p>
<p class="MsoNormal">I&rsquo;m going to go with a different solution, which uses an Attached Service (some people call it an Attached <span class="SpellE">Behavior</span>).<span style="mso-spacerun: yes;">&nbsp; </span>If you haven&rsquo;t had experiences with these then it may feel weird and require some getting used to.<span style="mso-spacerun: yes;">&nbsp; </span>It will allow the <span class="SpellE">ItemsControl</span> and bindings do the work, rather than us having to explicitly code it.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Attached Services</strong></p>
<p class="MsoNormal">You may be aware that XAML controls use a thing called <span class="SpellE">DependencyProperty</span>.<span style="mso-spacerun: yes;">&nbsp; </span>This allows controls to do stuff like inherit property values from their parent controls in a seamless way for us using the controls.<span style="mso-spacerun: yes;">&nbsp; </span>A closely related thing is an <span class="SpellE">AttachedDependencyProperty</span>.<span style="mso-spacerun: yes;">&nbsp; </span>This is a property defined on one class, but you actually set/retrieve the values on instances of other classes &ndash; weird, right?</p>
<p class="MsoNormal">I&rsquo;ll not go into how this is implemented internally, just believe that it does work and allows us to do some things which are usually unconventional.</p>
<p class="MsoNormal">I bet you&rsquo;ve used these before and never realized you were using attached properties.<span style="mso-spacerun: yes;">&nbsp; </span>For example if you add <span class="SpellE">Grid.Column</span>=&rdquo;3&rdquo; onto a <span class="SpellE">TextBox</span> then you are setting the value for an attached property.<span style="mso-spacerun: yes;">&nbsp; </span>The Grid implementation then reads these values when performing layout.</p>
<p class="MsoNormal">One of the tricky things you can do with an attached property is register a <span class="SpellE">callback</span> for when the property is changed.<span style="mso-spacerun: yes;">&nbsp; </span>This <span class="SpellE">callback</span> gets as the sender the object that the property has been applied to.<span style="mso-spacerun: yes;">&nbsp; </span>At that point you can interact with that object &ndash; get/set properties, call methods, etc...<span style="mso-spacerun: yes;">&nbsp; </span>It is typically the implementation of the changed <span class="SpellE">callback</span> that turns this into a &ldquo;service&rdquo; instead of just a stored value.</p>
<p class="MsoNormal">What is great about attached services is that if written right they can apply to just about any control, even custom ones.<span style="mso-spacerun: yes;">&nbsp; </span>And you can apply different attached services to the same control instance, allowing you to combine <span class="SpellE">behaviors</span>.<span style="mso-spacerun: yes;">&nbsp; </span>This is very superior to traditional methods like sub-classing or custom controls.</p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;">CalculatorNumberFormatter</strong></span><strong style="mso-bidi-font-weight: normal;"></strong></p>
<p class="MsoNormal">The attached service we are going to implement is <span class="SpellE">CalculatorNumberFormatter</span>.<span style="mso-spacerun: yes;">&nbsp; </span>It will define 3 attached properties.<span style="mso-spacerun: yes;">&nbsp; </span>2 are input properties &ndash; Number and Mode.<span style="mso-spacerun: yes;">&nbsp; </span>Number is designed to be bound an integer value.<span style="mso-spacerun: yes;">&nbsp; </span>Mode is designed to be bound to the current Mode.<span style="mso-spacerun: yes;">&nbsp; </span>When either of those properties change it will run code which will correctly format the integer into a string based on the mode.<span style="mso-spacerun: yes;">&nbsp; </span>The resulting value will be placed in an output parameter called Text.</p>
<p class="MsoNormal">Then the control that these properties are attached to can bind <span class="GramE">it&rsquo;s</span> Content/Text property to <span class="SpellE">CalculatorNumberFormatter.Text</span>.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">Not only will this correctly format the number correctly initially, but it will respond if the mode changes.<span style="mso-spacerun: yes;">&nbsp; </span>And this will happen in a better way than the initial View oriented implementation we had, because it will not clear and rebuild the <span class="SpellE">listbox</span>.<span style="mso-spacerun: yes;">&nbsp; </span>It also works if the <span class="SpellE">ListBox</span> (or any <span class="SpellE">ItemsControl</span>) is virtualizing (but we will not dive into that in this series).</p>
<p class="MsoNormal">Before we look at the code I&rsquo;d like to point out that we could have avoided the Text property and instead casted the <span class="SpellE">DependencyObject</span> we are attached to into what control it actually is, like <span class="SpellE">TextBlock</span>.<span style="mso-spacerun: yes;">&nbsp; </span>However this limits reuse as it wouldn&rsquo;t work with other controls.</p>
<p class="MsoNormal">Here is the code for one of the attached properties.<span style="mso-spacerun: yes;">&nbsp; </span>You&rsquo;ll notice we both define the static property, and add Get/Set methods.<span style="mso-spacerun: yes;">&nbsp; </span>Those methods are helpful, but also required for the XAML to compile.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">readonly</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyProperty</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">ModeProperty</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyProperty</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.RegisterAttached</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"Mode"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">,</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">typeof</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">),</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">typeof</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorNumberFormatter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">),</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">PropertyMetadata</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">null</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, <span class="SpellE">ModeProperty_PropertyChanged</span>));</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">GetMode</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyObject</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">obj</span>) { </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)<span class="SpellE">obj.GetValue</span>(<span class="SpellE">ModeProperty</span>); }</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">SetMode</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyObject</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">obj</span>, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value) { <span class="SpellE">obj.SetValue</span>(<span class="SpellE">ModeProperty</span>, value); }</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">ModeProperty_PropertyChanged</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyObject</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> o, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyPropertyChangedEventArgs</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> e)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">FormatNumber</span></span><span class="GramE">(</span>o);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The Number and Text properties are similar &ndash; just different names and types.</p>
<p class="MsoNormal"><span class="SpellE">FormatNumber</span> gets the Number and Mode values (handling if they aren&rsquo;t set) and sets the Text property.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">FormatNumber</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DependencyObject</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> o)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> mode = (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">?) <span class="SpellE">o.GetValue</span>(<span class="SpellE">ModeProperty</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Int64</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">?)<span class="SpellE"><span class="GramE">o.GetValue</span></span><span class="GramE">(</span><span class="SpellE">NumberProperty</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (<span class="SpellE">mode.HasValue</span> &amp;&amp; <span class="SpellE">value.HasValue</span>)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> text;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (mode == </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Hex</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE">text</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">String</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Format</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CultureInfo</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.CurrentCulture</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"0x{0:X}"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, <span class="SpellE">value.Value</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE">text</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">String</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Format</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CultureInfo</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.CurrentCulture</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"{0:G}"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, <span class="SpellE">value.Value</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">SetText</span></span><span class="GramE">(</span>o, text);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Applying <span class="SpellE">CalculatorNumberFormatter</span> to the <span class="SpellE">ListBox</span></strong></p>
<p class="MsoNormal">In order to apply this attached service to the <span class="SpellE">ListBox</span> we start off by binding the <span class="SpellE">ItemsSource</span> property to the Stack collection.<span style="mso-spacerun: yes;">&nbsp; </span>Logic inside the control will create a child control for each item.<span style="mso-spacerun: yes;">&nbsp; </span>We also set the <span class="SpellE">ItemTemplate</span> property so we can control the details of what those child controls expose.</p>
<p class="MsoNormal">One thing to be aware of is that the <span class="SpellE">DataContext</span> for the <span class="SpellE">ListBoxItem</span> children is not the <span class="SpellE">ViewModel</span>, but a specific item from the <span class="SpellE">ItemsSource</span> collection &ndash; in this case an <span class="SpellE">int</span> from our Stack collection.<span style="mso-spacerun: yes;">&nbsp; </span>So binding without a Path value will give use that object.<span style="mso-spacerun: yes;">&nbsp; </span>To get a reference to our view model, which has the Mode property we need, we need to use some additional features of binding.<span style="mso-spacerun: yes;">&nbsp; </span>Our choices in a Windows Store app are more limited than in WPF, but we only need 1 way that works.<span style="mso-spacerun: yes;">&nbsp; </span>In this case using <span class="SpellE">ElementName</span> and specifying an element outside the <span class="SpellE">ListBox</span> will work nicely.</p>
<p class="MsoNormal">That handles binding the Number and Mode properties.<span style="mso-spacerun: yes;">&nbsp; </span>All that is left is to connect <span class="SpellE">TextBlock.Text</span> to <span class="SpellE">CalculatorNumberFormatter.Text</span>.<span style="mso-spacerun: yes;">&nbsp; </span>I tried doing this the typical way and was having issues getting it to work.<span style="mso-spacerun: yes;">&nbsp; </span>So I wound up reversing it and making the binding 2 way to feed the value to the right place.</p>
<p class="MsoNormal">The result is this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">ListBox</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="_StackBox"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.ColumnSpan</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="2"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Margin</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">FontSize</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="32"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">ItemContainerStyle</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">StaticResource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">ListBoxItemStyle</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">ItemsSource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">Calculator<span style="color: blue;">.Stack</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">ListBox.ItemTemplate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">DataTemplate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">TextBlock</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">local<span class="GramE"><span style="color: blue;">:</span>CalculatorNumberFormatter.Number</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">local<span class="GramE"><span style="color: blue;">:</span>CalculatorNumberFormatter.Mode</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">DataContext<span style="color: blue;">.Mode</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">ElementName</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=<span class="SpellE">StackGrid</span>}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">local<span class="GramE"><span style="color: blue;">:</span>CalculatorNumberFormatter.Text</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Text</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">RelativeSource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">={</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">RelativeSource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=Self},</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=<span class="SpellE">TwoWay</span>}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">DataTemplate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">ListBox.ItemTemplate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">ListBox</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">We can then clean out the <span class="SpellE">ListBox</span> related code out of the view.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Wrapping it <span class="GramE">Up</span></strong></p>
<p class="MsoNormal">We are now done with the first phase.<span style="mso-spacerun: yes;">&nbsp; </span>Our <span class="SpellE">MainPage</span> class no longer has any logic in it.<span style="mso-spacerun: yes;">&nbsp; </span>The only thing we have in there that isn&rsquo;t part of the VS broiler plate is the code to create the <span class="SpellE">ViewModel</span> and assign the <span class="SpellE">DataContext</span>.</p>
<p class="MsoNormal">We learned a few new techniques to decoupling logic so we can have a View independent <span class="SpellE">ViewModel</span> and maximize code reusability.</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l1 level1 lfo6;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="SpellE">ValueConverters</span> can be used to bridge the gap between how our <span class="SpellE">ViewModel</span> works and the needs of a specific control</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l1 level1 lfo6;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Attached services are a powerful technique to connect custom logic to any control, helping us customize <span class="SpellE">behaviors</span> and overcome system limitations</p>
<p class="MsoNormal">In the next edition we&rsquo;ll take this completed <span class="SpellE">ViewModel</span> and build a phone view on top of it &ndash; proving that we can re-use it for different Views.</p>
<p class="MsoNormal">&nbsp;</p>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/55/36/06/MyCalc%20-%20part%203.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-55-36-06/MyCalc-_2D00_-part-3.zip">MyCalc - part 3.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Reusing ViewModels in a Universal App – Part 3</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/reusing-viewmodels-in-a-universal-app-part-3</link>
      <pubDate>Thu, 21 Aug 2014 16:34:00 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2014/08/21/reusing-viewmodels-in-a-universal-app-part-3/</guid>
      <description><![CDATA[To restate where part 2 ended &ndash; We took the MyCalc app and created an empty ViewModel...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">To restate where part 2 ended &ndash; We took the <span class="SpellE">MyCalc</span> app and created an empty <span class="SpellE">ViewModel</span> class.<span style="mso-spacerun: yes;">&nbsp; </span>We then implemented <span class="SpellE">DelegateCommand</span> so we could expose <span class="SpellE">ICommand</span> objects from the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>We used that to create commands for the majority of the buttons and modified the XAML to use them.<span style="mso-spacerun: yes;">&nbsp; </span>This allowed us to remove some code from the View.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Binding to <span class="SpellE">EnterText</span></strong></p>
<p class="MsoNormal">This next step we want to get rid of the coupling to <span class="SpellE">EnterBox</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Fortunately this is pretty trivial. <span style="mso-spacerun: yes;">&nbsp;</span>The solution is a simple data binding.<span style="mso-spacerun: yes;">&nbsp; </span>There are just 2 details we need to see to while settings up the binding.<span style="mso-spacerun: yes;">&nbsp; </span>The first is to make the binding 2 way, so as the control changes the new value is pushed into the View Model.<span style="mso-spacerun: yes;">&nbsp; </span>The second is to set the update mode so that the binding update occurs on each keystroke.</p>
<p class="MsoNormal">What we wind up with is this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">TextBox</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="_EnterBox"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Margin</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Width</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="400"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">FontSize</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="32"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Text</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">EnterText</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> Mode</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=<span class="SpellE">TwoWay</span>,</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">UpdateSourceTrigger</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">=<span class="SpellE">PropertyChanged</span>}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Now the View can just interact w/ <span class="SpellE">EnterText</span> rather than the control.<span style="mso-spacerun: yes;">&nbsp; </span>It also doesn&rsquo;t need that code we added to copy the string back and forth between the control and view model.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Number Mode</strong></p>
<p class="MsoNormal">To unblock the next set of changes we need to make the view model aware of the number nodes &ndash; decimal vs hex.<span style="mso-spacerun: yes;">&nbsp; </span>There are multiple ways we can do this &ndash; for example we could have a bool <span class="SpellE">IsHex</span> property.<span style="mso-spacerun: yes;">&nbsp; </span>However I personally prefer an <span class="SpellE">enum</span>.</p>
<p class="MsoNormal">Not only does the <span class="SpellE">enum</span> make the code more readable, but it also sets us up to support other number modes in the future.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">enum</span></span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Decimal,</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Hex</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> Mode</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">get</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _mode;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">set</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">_mode != </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">value</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_mode = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">value</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE"><span class="GramE">addDigitCommand.RaiseCanExecuteChanged</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">RaisePropertyChanged</span></span><span class="GramE">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"Mode"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">We also update the View code so that when the radio buttons are changed we update this <span class="SpellE">ViewModel</span> property appropriately.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Supporting Hex Entry</strong></p>
<p class="MsoNormal">With that property in the view model we now have the information we need to correctly enable/disable the A-F buttons.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">We update the can execute to correctly compute if the hex buttons are enabled.<span style="mso-spacerun: yes;">&nbsp; </span>In addition in the Mode setter we raise the <span class="SpellE">CanExecuteChanged</span> event for the <span class="SpellE">AddDigitCommand</span>.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnAddDigitCanExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">stringParameter</span> = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)parameter;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">switch</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">stringParameter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"0"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"1"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"2"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"3"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"4"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"5"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"6"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"7"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"8"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"9"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"A"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"B"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"C"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"D"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"E"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"F"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (Mode == </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Hex</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">default</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">false</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">We can then update the XAML and View code just like we did for the 0-9 buttons.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Enter Button</strong></p>
<p class="MsoNormal">The last thing we are going to do in this part is convert the Enter button to use the View Model.<span style="mso-spacerun: yes;">&nbsp; </span>Just like the other buttons we add a new command, wire it up in the XAML and remove the obsolete code from the View.</p>
<p class="MsoNormal">I called it <span class="SpellE">PushCommand</span>, since that is the logical operation of the Enter button and here is its implementation:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnPushExecuted</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">NumberStyles</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> styles;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (Mode == </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CalculatorViewMode</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Hex</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE">styles</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">NumberStyles</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.HexNumber</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">else</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="GramE">styles</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">NumberStyles</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Integer</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Int64</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (</span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Int64</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.TryParse(<span class="SpellE">EnterText</span>, styles, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CultureInfo</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.CurrentCulture</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">out</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> value))</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE"><span class="GramE">calc.Push</span></span><span class="GramE">(</span>value);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">EnterText</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">String</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Empty</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnPushCanExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (<span class="SpellE">EnterText.Length</span> &gt; 0);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Wrapping it up</strong></p>
<p class="MsoNormal">This part introduced 2 way bindings as a way to link a <span class="SpellE">TextBox</span> control with a <span class="SpellE">ViewModel</span> property.<span style="mso-spacerun: yes;">&nbsp; </span>By adding the Mode property to the <span class="SpellE">ViewModel</span> we were able to move the remaining Button&rsquo;s to use <span class="SpellE">ICommand</span>.</p>
<p class="MsoNormal">This also shows how easy enhancing the view model is once you have some <span class="SpellE">ViewModel</span> infrastructure coded, like <span class="SpellE">DelegateCommand</span>.</p>
<p class="MsoNormal">Now all that remains in the View is code to handle displaying the stack and responding to the <span class="SpellE">RadioButtons</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Getting rid of that code will be the subject of the next instalment.</p>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/55/19/76/MyCalc%20-%20part%203.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-55-19-76/MyCalc-_2D00_-part-3.zip">MyCalc - part 3.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Reusing ViewModels in a Universal App – Part 2</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/reusing-viewmodels-in-a-universal-app-part-2</link>
      <pubDate>Thu, 21 Aug 2014 16:24:00 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2014/08/21/reusing-viewmodels-in-a-universal-app-part-2/</guid>
      <description><![CDATA[To restate where part 1 ended - We have our Windows App, MyCalc, which currently has no ViewModel...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">To restate where part 1 ended - We have our Windows App, <span class="SpellE">MyCalc</span>, which currently has no <span class="SpellE">ViewModel</span> and instead has all the logic in the View layer.<span style="mso-spacerun: yes;">&nbsp; </span>Which means it can&rsquo;t be reused for different views, such as one for phone.<span style="mso-spacerun: yes;">&nbsp; </span>Our goal is to move that logic into a shared <span class="SpellE">ViewModel</span> and add a Phone view.</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Adding a <span class="SpellE">ViewModel</span></strong></p>
<p class="MsoNormal">The first step is to add an empty <span class="SpellE">ViewModel</span> class.<span style="mso-spacerun: yes;">&nbsp; </span>So in the shared code project we create a new class called <span class="SpellE">StackCalculatorViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">One thing just about every <span class="SpellE">ViewModel</span> needs is a way to communicate data changes to the View layer.<span style="mso-spacerun: yes;">&nbsp; </span>In XAML there is already a standard way to do this that works with data binding (more about that later).<span style="mso-spacerun: yes;">&nbsp; </span>That way is the <a href="https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.inotifypropertychanged.aspx"><span class="SpellE">INotifyPropertyChanged</span></a> interface.<span style="mso-spacerun: yes;">&nbsp; </span>It allows us to raise an event with the name of the property that has changed.<span style="mso-spacerun: yes;">&nbsp; </span>So <span class="SpellE"><span class="GramE">lets</span></span> implement that interface, and a helper method to raise the event.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">RaisePropertyChanged</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">propertyName</span>)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> eh = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.PropertyChanged</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">eh != </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">null</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="GramE">eh(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">PropertyChangedEventArgs</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">propertyName</span>));</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">event</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">PropertyChangedEventHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">PropertyChanged</span>;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The next thing our <span class="SpellE">ViewModel</span> needs is a reference to the Model, which is our <span class="SpellE">StackCalculator</span> class.<span style="mso-spacerun: yes;">&nbsp; </span>So we add a private field of type <span class="SpellE">StackCalculator</span> and in the constructor initialize it to a new instance of that class.</p>
<p class="MsoNormal">Lastly we need to give the View access to our <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>So in our <span class="SpellE">MainPage</span> class we add a private field of type <span class="SpellE">StackCalculatorViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>In the constructor we assign it to a new instance of that class.</p>
<p class="MsoNormal">And here is the &ldquo;tricky&rdquo; part &ndash; we set the <span class="SpellE">MainPage.DataContext</span> to point to the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>The <span class="SpellE">DataContent</span> property is inherited by all the child controls, so now all the controls can access the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Not only <span class="GramE">that but <span class="SpellE">DataContext</span> is</span> the default data binding source, which will come in handy later.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">sealed</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">partial</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">class</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">MainPage</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> : </span><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">Page</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">StackCalculatorViewModel</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">viewModel</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">MainPage</span>()</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">viewModel</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">StackCalculatorViewModel</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">viewModel.PropertyChanged</span> += _<span class="SpellE">viewModel_PropertyChanged</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">collectionChanged</span> = (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">INotifyCollectionChanged</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)_<span class="SpellE">viewModel.Calculator.Stack</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">collectionChanged.CollectionChanged</span> += <span class="SpellE">collectionChanged_CollectionChanged</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.DataContext</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> = _<span class="SpellE">viewModel</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.InitializeComponent</span></span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// initial to defaults</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">DecimalSelection.IsChecked</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">UpdateEnterBoxButtons</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">UpdateStackBox</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">You&rsquo;ll likely notice we removed the <span class="SpellE">StackCalculator</span> field from <span class="SpellE">MainPage</span>, and instead reference the <span class="SpellE">StackCalculator</span> object held in the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>This is temporary code that will go away once the <span class="SpellE">ViewModel</span> is fully fleshed out.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Moving Logic to the <span class="SpellE">ViewModel</span></strong></p>
<p class="MsoNormal">Now we have an empty view model and we need to pick some logic to move into it.<span style="mso-spacerun: yes;">&nbsp;&nbsp; </span>Buttons tend to be pretty easy to support in a <span class="SpellE">ViewModel</span>, so we&rsquo;ll start with those.</p>
<p class="MsoNormal">To bite off a reasonable chunk of work we are going to limit this to the buttons 0-9, add, subtract, multiply, divide and drop.</p>
<p class="MsoNormal">Buttons are easy to support with a <span class="SpellE">ViewModel</span> because they support having a command &ndash; meaning we can provide an instance of <a href="https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.input.icommand.aspx"><span class="SpellE">ICommand</span></a> which allows us to execute code when the button is clicked.<span style="mso-spacerun: yes;">&nbsp; </span>It also allows us to enable/disable the button when conditions change.</p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;">ICommand</strong></span><strong style="mso-bidi-font-weight: normal;"> and <span class="SpellE">DelegateCommand</span></strong></p>
<p class="MsoNormal"><span class="SpellE">ICommand</span>, as an interface, is perfect for our use in the <span class="SpellE">ViewModel</span>.<span style="mso-spacerun: yes;">&nbsp; </span>However there isn&rsquo;t an implementation of that interface that is <span class="SpellE">ViewModel</span> friendly.<span style="mso-spacerun: yes;">&nbsp; </span>So we need to code our own implementation.<span style="mso-spacerun: yes;">&nbsp; </span>The most common implementation is along the lines of what I call <span class="SpellE">DelegateCommand</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Simply put when one constructs a <span class="SpellE">DelegateCommand</span> one provides delegates to the implementations you want of Execute and <span class="SpellE">CanExecute</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Thus the implementation is a simple Adapter to the required interface.<span style="mso-spacerun: yes;">&nbsp; </span>Add a method to raise the <span class="SpellE">CanExecuteChanged</span> event when needed and you are done.</p>
<p class="MsoNormal">My particular implementation has 2 constructors:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l1 level1 lfo1;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>One that takes an <span class="SpellE">ExecuteHandler</span></p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l1 level1 lfo1;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Another that takes an <span class="SpellE">ExecuteHandler</span> and <span class="SpellE">CanExecuteHandler</span></p>
<p class="MsoNormal">The version that just takes an <span class="SpellE">ExecuteHandler</span> supplies an implementation of <span class="SpellE">CanExecute</span> which always returns true.<span style="mso-spacerun: yes;">&nbsp; </span>This is makes it just a little easier for supporting buttons that are always enabled.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">delegate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">delegate</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CanExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">class</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> : </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ICommand</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">executeHandler</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CanExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">canExecuteHandler</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">DelegateCommand</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">executeHandler</span>):</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">executeHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, <span class="SpellE">CanAlwaysExecute</span>)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: green; background: white; mso-highlight: white;">// nothing</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">DelegateCommand</span>(</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">executeHandler</span>, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">CanExecuteHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">canExecuteHandler</span>)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">executeHandler</span> = <span class="SpellE">executeHandler</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">canExecuteHandler</span> = <span class="SpellE">canExecuteHandler</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">RaiseCanExecuteChanged</span>()</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> eh = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.CanExecuteChanged</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(eh != </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">null</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>eh(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">EventArgs</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Empty</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">CanExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">canExecuteHandler</span>(parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">event</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">EventHandler</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">CanExecuteChanged</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> Execute(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">executeHandler</span>(parameter);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">static</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">CanAlwaysExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Using <span class="SpellE">DelegateCommand</span> in our <span class="SpellE">ViewModel</span></strong></p>
<p class="MsoNormal">Ok, now that we have an implementation of <span class="SpellE">ICommand</span> we can use, lets wire it up in our <span class="SpellE">ViewModel</span>.</p>
<p class="MsoNormal">First we add private methods for Execute/<span class="SpellE">CanExecute</span> that we need for the commands we will expose.<span style="mso-spacerun: yes;">&nbsp; </span>As an example here is are the implementation for the add command:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnAddExecuted</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">calc.Add</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnAddCanExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">calc.CanAdd</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">We also expose the command as a property so the Button can bind to it, like this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">ICommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">AddCommand</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">get</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">addCommand</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And in the constructor we create the <span class="SpellE">DelegateCommand</span> instance providing the right methods, like this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">StackCalculatorViewModel</span>()</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">enterText</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">String</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.Empty</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">calc</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">StackCalculator</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">collectionChanged</span> = (</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">INotifyCollectionChanged</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)_<span class="SpellE">calc.Stack</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">collectionChanged.CollectionChanged</span> += <span class="SpellE">OnStackChanged</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">addCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnAddExecuted</span>, <span class="SpellE">OnAddCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">subtractCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnSubtractExecuted</span>, <span class="SpellE">OnSubtractCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">divideCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnDivideExecuted</span>, <span class="SpellE">OnDivideCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">multiplyCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnMultiplyExecuted</span>, <span class="SpellE">OnMultiplyCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">popCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnPopExecuted</span>, <span class="SpellE">OnPopCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">addDigitCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnAddDigitExecuted</span>, <span class="SpellE">OnAddDigitCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">backspaceCommand</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">new</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">DelegateCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">OnBackspaceExecuted</span>, <span class="SpellE">OnBackspaceCanExecute</span>);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Just one last thing and the <span class="SpellE">AddButton</span> can be complete &ndash; that is raising the <span class="SpellE">OnCanExecuteChanged</span> appropriately.<span style="mso-spacerun: yes;">&nbsp; </span>Our Model doesn&rsquo;t expose an event directly for this, but does let us know when the stack contents have changed.<span style="mso-spacerun: yes;">&nbsp; </span>Since we know that impacts the value returned from <span class="SpellE">CanAdd</span>, we subscribe to that event and in the handler raise <span class="SpellE">OnCanExecuteChanged</span> for the appropriate commands, like this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnStackChanged</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> sender, </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #2b91af; background: white; mso-highlight: white;">NotifyCollectionChangedEventArgs</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> e)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">addCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">subtractCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">divideCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">multiplyCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">popCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Then we need to update the XAML to bind to the command, which is straightforward and works like this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Button</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>x</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">:</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">Name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="_AddButton"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Style</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">StaticResource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">ButtonStyle</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="+"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Command</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">AddCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The event handler for Clicked event was removed, and we bind to the <span class="SpellE">AddCommand</span> from the View Model.<span style="mso-spacerun: yes;">&nbsp; </span>You&rsquo;ll also notice that I added a _ in front of the name of the control.<span style="mso-spacerun: yes;">&nbsp; </span>I did this to ensure I caught all references to the control in the View, since the name change will cause a compiler break.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">In the view code we remove the event handler and all references to the button &ndash; ensuring as we got that we captured all the right functionality in the <span class="SpellE">ViewModel</span> for that button.</p>
<p class="MsoNormal">As you look at the code you&rsquo;ll notice that Add, Subtract, Divide, Multiply and Pop commands are all very similar.<span style="mso-spacerun: yes;">&nbsp; </span>But <span class="SpellE">AddDigitCommand</span> is a bit different.</p>
<p class="MsoNormal"><span class="SpellE"><strong style="mso-bidi-font-weight: normal;">AddDigitCommand</strong></span><strong style="mso-bidi-font-weight: normal;"></strong></p>
<p class="MsoNormal">Now for buttons 0-9 we could create 10 commands &ndash; one per button.<span style="mso-spacerun: yes;">&nbsp; </span>That would work, but would be a bit monotonous to code, as well as just require a bunch of code (although it would be simple code).</p>
<p class="MsoNormal">Instead we can take advantage of the parameter which <span class="SpellE">ICommand&rsquo;s</span> Execute &amp; <span class="SpellE">CanExecute</span> methods.<span style="mso-spacerun: yes;">&nbsp; </span>That means we can have a single <span class="SpellE">ICommand</span> which supports all 10 buttons (0-9) and eventually will even support A-F (we&rsquo;ll first need to move the Hex/Decimal support into the <span class="SpellE">ViewModel</span>, which will happen in a future article).</p>
<p class="MsoNormal">The implementation of that command looks like this:</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">void</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnAddDigitExecuted</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">stringParameter</span> = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)parameter;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">newText</span> = </span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.EnterText</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> + <span class="SpellE">stringParameter</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">this</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">.EnterText</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> = <span class="SpellE">newText</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">private</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">bool</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">OnAddDigitCanExecute</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">object</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> parameter)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">var</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">stringParameter</span> = (</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)parameter;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">switch</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">(<span class="SpellE">stringParameter</span>)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"0"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"1"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"2"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"3"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"4"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"5"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"6"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"7"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"8"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">case</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"9"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">true</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">default</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">:</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">false</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">As you can see the parameter is the character to add to the <span class="SpellE">EnterText</span>.<span style="mso-spacerun: yes;">&nbsp; </span>Right now <span class="SpellE">CanExecute</span> just verifies it is an expected value between 0 and 9.<span style="mso-spacerun: yes;">&nbsp; </span>This method will get more interesting when Hex support is added.</p>
<p class="MsoNormal">You&rsquo;ll also notice that I&rsquo;ve added another property, <span class="SpellE">EnterText</span>, which contains the string the user is composing to add a number to the stack.<span style="mso-spacerun: yes;">&nbsp; </span>This is required because the <span class="SpellE">AddDigitCommand</span> needs to modify it.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">public</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">string</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> <span class="SpellE">EnterText</span></span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">get</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">return</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> _<span class="SpellE">enterText</span>;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">set</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">if</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"> (_<span class="SpellE">enterText</span> != </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">value</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">)</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">enterText</span> = </span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">value</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>_<span class="SpellE">backspaceCommand.RaiseCanExecuteChanged</span>();</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">RaisePropertyChanged</span>(</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">"<span class="SpellE">EnterText</span>"</span><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;">);</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The property is pretty simple.<span style="mso-spacerun: yes;">&nbsp; </span>You&rsquo;ll notice the setter raises the <span class="SpellE">PropertyChanged</span> event and also tells the backspace command to re-evaluate if it can execute &ndash; this is because the <span class="SpellE">BackspaceCommand</span> being able to execute depends on the length of <span class="SpellE">EnterText</span>.</p>
<p class="MsoNormal">Because this is just one step in a multi-step refactoring we do need some temporary code to keep everything working.<span style="mso-spacerun: yes;">&nbsp; </span>Because we can&rsquo;t fully move dealing with that text to the view model until the Hex support is done.<span style="mso-spacerun: yes;">&nbsp; </span>So you&rsquo;ll notice in the View there is now code which updates <span class="SpellE">EnterText</span> on the <span class="SpellE">ViewModel</span> when the <span class="SpellE">TextBox&rsquo;s</span> text is changed.<span style="mso-spacerun: yes;">&nbsp; </span>There is also code that does the reverse and updates the <span class="SpellE">TextBox</span> when <span class="SpellE">EnterText</span> changes.</p>
<p class="MsoNormal">Just like the other commands we need to update the XAML and remove code from the View.<span style="mso-spacerun: yes;">&nbsp; </span>The only real difference is we also set the <span class="SpellE">CommandParameter</span> property to define what should be passed into the Command&rsquo;s methods.</p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">&lt;</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Button</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>x</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">:</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;">Name</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="_FourButton"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="2"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="2"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Style</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span class="SpellE"><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">StaticResource</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">ButtonStyle</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Command</span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="{</span><span style="font-size: 9.5pt; font-family: Consolas; color: #a31515; background: white; mso-highlight: white;">Binding</span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"> <span class="SpellE">AddDigitCommand</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: red; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">CommandParameter</span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="margin-bottom: .0001pt; line-height: normal; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 9.5pt; font-family: Consolas; color: black; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9.5pt; font-family: Consolas; color: blue; background: white; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal;">Wrapping It Up</strong></p>
<p class="MsoNormal"><span class="SpellE">Lets</span> review what we&rsquo;ve done:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Created a <span class="SpellE">ViewModel</span> and made it available to the XAML <span class="SpellE">markup</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Created <span class="SpellE">DelegateCommand</span> to expose <span class="SpellE">ICommand</span> objects from the <span class="SpellE">ViewModel</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Exposed commands from the <span class="SpellE">ViewModel</span> for most of the buttons</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Migrated the XAML to use the commands</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Removed code from the View</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -.25in; mso-list: l0 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7.0pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Added some temporary code in the View for <span class="SpellE">EnterText</span> to keep things working until the next step.</p>
<p class="MsoNormal">Hopefully you can see that the net effect is logic that was previous in the View and coupled with the XAML <span class="SpellE">markup</span> is now in the reusable <span class="SpellE">ViewModel</span> and is completely decoupled from the <span class="SpellE">markup</span>.</p>
<p class="MsoNormal">One thing that might have escaped notice is that there are a variety of controls which accept an <span class="SpellE">ICommand</span>, beyond the simple button.<span style="mso-spacerun: yes;">&nbsp; </span>For example the <span class="SpellE">HyperlinkButton</span> takes an <span class="SpellE">ICommand</span>, so we could change the <span class="SpellE">markup</span> to use hyperlinks instead of traditional buttons.<span style="mso-spacerun: yes;">&nbsp; </span>Which gives us a glimpse of the flexibility a good <span class="SpellE">ViewModel</span> can give us when it comes to defining different <span class="SpellE">markup</span> over the same logic.</p>
<p class="MsoNormal">Complete code attached.</p>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/55/19/72/MyCalc%20-%20part%202.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-55-19-72/MyCalc-_2D00_-part-2.zip">MyCalc - part 2.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Reusing ViewModels in a Universal App – Part 1</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/reusing-viewmodels-in-a-universal-app-part-1</link>
      <pubDate>Wed, 20 Aug 2014 16:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2014/08/20/reusing-viewmodels-in-a-universal-app-part-1/</guid>
      <description><![CDATA[There is an increasing trend for Microsoft platforms to build what is called a &ldquo;Universal...]]></description>
      <content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">There is an increasing trend for Microsoft platforms to build what is called a &ldquo;Universal App&rdquo;.<span style="mso-spacerun: yes;">&nbsp; </span>In its ideal it is an application that you code once, and then it runs on desktop, tablet and phone platforms.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">This opportunity introduces new challenges &ndash; with a main one being that the different platforms have differences.<span style="mso-spacerun: yes;">&nbsp; </span>Differences in APIs, differences in capabilities and differences in display size and orientation.</p>
<p class="MsoNormal">All of these threaten the ideal of reusing most, if not all, of the code between the platforms.<span style="mso-spacerun: yes;">&nbsp; </span>The less code you can share between the platforms, means:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -0.25in; mso-list: l0 level1 lfo1;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>A higher cost for development &ndash; meaning either more resources needed, or more time required</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; mso-list: l0 level1 lfo1;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>More code that needs to be maintained</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -0.25in; mso-list: l0 level1 lfo1;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Higher probability of bugs, especially that impact one platform but another</p>
<p class="MsoNormal">The Model-View-<span class="SpellE">ViewModel</span> pattern has the potential to increase code reuse across the platforms.<span style="mso-spacerun: yes;">&nbsp; </span>This can be done by minimizing logic in the View and designing a View Model that is agnostic of the details of the View.<span style="mso-spacerun: yes;">&nbsp; </span>This will allow us to develop a different View for the different platforms, while sharing the code in the <span class="SpellE">ViewModel</span> and Model.<span style="mso-spacerun: yes;">&nbsp; </span></p>
<p class="MsoNormal">To illustrate this I&rsquo;ve created a Windows Store application, called My <span class="SpellE">Calc</span>, which is purposeful coded in a naive View centric way.<span style="mso-spacerun: yes;">&nbsp; </span>As is if one were to make the application available phone would be a large incremental cost.</p>
<p class="MsoNormal"><span class="SpellE">MyCalc</span> is a simple stack calculator application.<span style="mso-spacerun: yes;">&nbsp; </span>One can push numbers on the stack.<span style="mso-spacerun: yes;">&nbsp; </span>Operations, such as Add, take their operands from the stack and replace it with the resulting value.<span style="mso-spacerun: yes;">&nbsp; </span>Purposefully the set of operations implemented as basic, as the object is to focus on <span class="SpellE">ViewModel</span> development, and not develop a fully featured calculator app.</p>
<p class="MsoNormal"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/3264.MyCalc%20-%20part%201%20screenshot.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/3264.MyCalc-_2D00_-part-1-screenshot.png"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/13/65/3264.MyCalc%20-%20part%201%20screenshot.png" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-65/3264.MyCalc-_2D00_-part-1-screenshot.png" alt="" border="0" /></a></p>
<p class="MsoNormal">In the various parts of this blog we&rsquo;ll first refactor the application to have a <span class="SpellE">ViewModel</span>, and end with porting the application to phone by coding up a phone specific View.</p>
<p class="MsoNormal">Attached is our starting solution.<span style="mso-spacerun: yes;">&nbsp; </span>It consists of 3 projects:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -0.25in; mso-list: l1 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="SpellE">MyCalc.Shared</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; margin-left: 1in; mso-list: l1 level2 lfo2; mso-add-space: auto;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Courier New';"><span style="mso-list: Ignore;">o<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp; </span></span></span>Contains code shared across the platforms</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; mso-list: l1 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="SpellE">MyCalc.Windows</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; margin-left: 1in; mso-list: l1 level2 lfo2; mso-add-space: auto;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Courier New';"><span style="mso-list: Ignore;">o<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp; </span></span></span>Contains code specific to Windows</p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent: -0.25in; mso-list: l1 level1 lfo2;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span class="SpellE">MyCalc.WindowsPhone</span></p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -0.25in; margin-left: 1in; mso-list: l1 level2 lfo2; mso-add-space: auto;"><span style="font-family: 'Courier New'; mso-fareast-font-family: 'Courier New';"><span style="mso-list: Ignore;">o<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp; </span></span></span>Contains code specific to phone</p>
<p class="MsoNormal">The shared project contains a <span class="SpellE">StackCalculator</span> class.<span style="mso-spacerun: yes;">&nbsp; </span>This is our Model, or business logic.<span style="mso-spacerun: yes;">&nbsp; </span>It doesn&rsquo;t know anything about the UI.</p>
<p class="MsoNormal">You can ignore the <span class="SpellE">WindowsPhone</span> project for now, as all it has in it is the broiler plate code the Visual Studio put in when the project was created.</p>
<p class="MsoNormal">The Windows project has the <span class="SpellE">MainWindow</span>.<span style="mso-spacerun: yes;">&nbsp; </span>If you&rsquo;ve done UI development in XAML, WPF or Silverlight then likely you&rsquo;ve seen (and written) code similar to this.</p>
<p class="MsoNormal">Here is a snippet of the <span class="SpellE">xaml</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Button</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="SixButton"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="2"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Style</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">StaticResource</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">ButtonStyle</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="6"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Click</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="<span class="SpellE">SixButton_Click</span>"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Button</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="ThreeeButton"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="3"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Style</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">StaticResource</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">ButtonStyle</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Content</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="3"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Click</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="<span class="SpellE">ThreeeButton_Click</span>"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;/</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="StackGrid"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.ColumnDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ColumnDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Width</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="Auto" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ColumnDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Width</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="Auto" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.ColumnDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.RowDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">RowDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Height</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="*" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">RowDefinition</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Height</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="Auto" /&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;/</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Grid.RowDefinitions</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ListBox</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="StackBox"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.ColumnSpan</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="2"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Margin</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">FontSize</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="32"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">ItemContainerStyle</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="{</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">StaticResource</span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">ListBoxItemStyle</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">}"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&lt;</span><span class="SpellE"><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">TextBox</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="GramE">x<span style="color: blue;">:</span></span>Name</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="EnterBox"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Column</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="0"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">Grid.Row</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="1"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Margin</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="4"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>Width</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="400"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">FontSize</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="32"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span><span class="SpellE">TextChanged</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">="<span class="SpellE">EnterBox_TextChanged</span>"</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;</span>/&gt;</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">There are a few things I&rsquo;d like to point out about this code:</p>
<p class="MsoListParagraphCxSpFirst" style="text-indent: -0.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>The elements (buttons, <span class="SpellE">ListBox</span>, <span class="SpellE">TextBox</span>, etc&hellip;) have names so we can reference them in the code behind</p>
<p class="MsoListParagraphCxSpLast" style="text-indent: -0.25in; mso-list: l2 level1 lfo3;"><span style="font-family: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol;"><span style="mso-list: Ignore;">&middot;<span style="font: 7pt/normal 'Times New Roman'; font-size-adjust: none; font-stretch: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span>Events are wired up to methods in the code behind, such as the Clicked and <span class="SpellE">TextChanged</span> events.</p>
<p class="MsoNormal">These are indicators that this UI was coded up in a traditional way without a <span class="SpellE">ViewModel</span> (or at least not a <span class="SpellE">well designed</span> one).<span style="mso-spacerun: yes;">&nbsp; </span>When we look at the code we get that confirmed with code like this:</p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">EnterBox_TextChanged</span>(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">object</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> sender, </span><span class="SpellE"><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">TextChangedEventArgs</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> e)</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE"><span class="GramE">UpdateEnterBoxButtons</span></span><span class="GramE">(</span>);</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">&nbsp;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> <span class="SpellE">UpdateEnterBoxButtons</span>()</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span></span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> (<span class="SpellE">EnterBox.Text.Length</span> == 0)</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">EnterButton.IsEnabled</span> = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">false</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">BackspaceButton.IsEnabled</span> = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">false</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span class="GramE"><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">else</span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">EnterButton.IsEnabled</span> = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="SpellE">BackspaceButton.IsEnabled</span> = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">true</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">;</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp; </span><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>}</span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none;"><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Here we see that the logic in the View is very tightly coupled with the XAML <span class="SpellE">markup</span>, since the View not only responds to events from the controls, but directly updates the state of various controls.</p>
<p class="MsoNormal">While one could theoretically compile this code against 2 sets of <span class="SpellE">markup</span> (and there are some challenges to just making that happen) we are quite limited in the changes that could be made &ndash; other than just layout and style changes.<span style="mso-spacerun: yes;">&nbsp; </span>For example the View code expects both an Enter and Backspace button &ndash; but what if, due to space constraints, on phone we would rather just use the ones on the soft keyboard rather than have such buttons in our main UI.</p>
</div>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/55/16/71/MyCalc%20-%20part%201.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-55-16-71/MyCalc-_2D00_-part-1.zip">MyCalc - part 1.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Detecting UI Thread Misuse</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/detecting-ui-thread-misuse</link>
      <pubDate>Tue, 28 Dec 2010 10:13:00 GMT</pubDate>
      <dc:creator><![CDATA[Natenez]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2010/12/28/detecting-ui-thread-misuse/</guid>
      <description><![CDATA[One of the challenges in building a UI application is correctly using the UI thread.&nbsp; This is a...]]></description>
      <content:encoded><![CDATA[<p>One of the challenges in building a UI application is correctly using the UI thread.&nbsp; This is a shared resource, and so any misuse has the potential to have global application effect.</p>
<p>The most common form of misuse is using the UI thread for a long period of time.&nbsp; In severe cases this causes the application to hang and for Windows to detect it is not responsive.&nbsp; In minor cases this can interfere with animations or missing transitional visual updates.</p>
<p>Unfortunately most UI technologies make it really easy to make a mistake.&nbsp; UI programs are very event driven.&nbsp; And these technologies call back into the application on the UI thread to handle these events, which naturally causes people to write code to handle those events on the UI thread, unless they do additional work to move the logic to a background thread.</p>
<p>Components like ThreadPool and BackgroundWorker can help with this task, but it is still easy to make a mistake and occasionally block the UI thread.</p>
<p>Operations that are always slow are easy for developers and testers to detect during development so it can be fixed.&nbsp; But real applications don't always run at the same speed - network connections, responsiveness of remote computers, and local thread timing can all make UI thread violation issues hard to reproduce or even detect in the first place - especially if the symptom is a subtle paint issue.</p>
<p>This makes detecting the issues almost random which can cause a steady stream of bugs coming in.&nbsp; Worse it is hard to get confidence that the state of the application is improving.</p>
<p>Wouldn't it be nice if there was a way to detect these issues deterministically?&nbsp; That would help one identifying all existing issues and help prevent new issues from being added.</p>
<p>The answer is something I call the UI Thread Watchdog.&nbsp; Its responsibility is to detect misuse of the UI thread and complain.&nbsp; It has 2 approaches to achieve that.</p>
<p>The first way is to provide methods which components can call to validate if they are being executed on the UI thread or not.&nbsp; These methods are easy to implement.&nbsp; They just need to compare the current Thread with the Thread being used for the UI.&nbsp; The Dispatcher object has this information.</p>
<p>Using these methods is also easy, just call the watchdog right method from your code.&nbsp; Now one could do a brute force approach and call the watchdog from just about everywhere.&nbsp; But one often gets more "bang for the buck" if one picks strategic locations to "instrument".&nbsp; Key places are the ones which may take a while, such as:</p>
<ul>
<li>Code which interacts with networking</li>
<li>Code which interacts with other processes</li>
<li>Code which interacts with the file system</li>
<li>Code which acquires locks</li>
<li>Code which makes blocking calls, like waiting on handles</li>
<li>Code which interacts with the hardware </li>
</ul>
<p>The second way the UI Thread Watchdog works is that is occasionally polls the UI thread to see if it is responsive.&nbsp; Implementing this isn't as easy, but isn't hard.&nbsp; </p>
<p>On a background thread one occasionally does an Invoke to the UI thread.&nbsp; Then checks to see if the invoked method gets called within a certain amount of time.&nbsp; If it doesn't execute then something time consuming is using the UI thread, preventing processing of other messages.</p>
<p>The hard part of this is deciding how often to poll and how long to wait before declaring the UI thread as blocked.&nbsp; The balance is to make the times small enough to detect issues, but long enough to avoid false positives.&nbsp; I suggest starting with small values and then increment the values up if you hit a large number of false positives.</p>
<p>This isn't deterministic like the validation methods, but can improve detection of these issues significantly - and works on code not "instrumented."</p>
<p>Attached is code which implements a simple thread watchdog using these techniques to demonstrate the ideas.</p>
<p>Some ways this can be improved are:</p>
<ul>
<li>Freeze the UI thread on a not responsive validation so the culprit will be on the stack when the issue is debugged</li>
<li>Disable the checks on the shipping build so users don't see the issue</li>
<li>Log the violations to the application's log</li>
</ul>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/10/96/16/WatchDoggie.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-10-96-16/WatchDoggie.zip">WatchDoggie.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Launching Dialog Windows in Managed MMC Snapins</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/launching-dialog-windows-in-managed-mmc-snapins</link>
      <pubDate>Tue, 22 Dec 2009 16:07:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2009/12/22/launching-dialog-windows-in-managed-mmc-snapins/</guid>
      <description><![CDATA[Launching a child window inside MMC 3.0 from managed code isn’t easy.&nbsp; Different APIs are...]]></description>
      <content:encoded><![CDATA[<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>Launching a child window inside MMC 3.0 from managed code isn’t easy.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Different APIs are required for different circumstances.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>I don’t know exactly why this is, but I assume it has something to do with all the tricks MMC does to give each managed snapin its own UI thread, and needing to marshal a request back to the main UI thread owned by the console.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>There are 3 APIs you need to know about when launching a child window (modal or modeless) inside a managed snapin.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraph><SPAN style="COLOR: #1f497d; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>1)</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>If you want a child window of the console then use Console.Show/ShowDialog<o:p></o:p></FONT></FONT></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraph><SPAN style="COLOR: #1f497d; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>2)</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>If you want a child window of a property sheet then use PropertySheet.Show/ShowDialog<o:p></o:p></FONT></FONT></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1" class=MsoListParagraph><SPAN style="COLOR: #1f497d; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>3)</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>If you want a child window of a winforms window (previously launched w/ Console/PropertySheet APIs) then use Form.Show/ShowDialog<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>The same rules apply for simple MessageBoxes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The APIs to use are Console.ShowDialog(MessageBoxPrameters), PropertySheet.ShowDialog(MessageBoxParameters) and MessageBox.Show().<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Follow the same rules as above.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>In one project I worked on we had some common code which needed to launch dialogs &amp; show messages – but that code could be called from anywhere and so it didn’t know what API to call.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>What we did was make an interface called INotifyUser which had Show/ShowMessageBox/ShowDialog methods.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Then we had 3 concrete implementations, one for each case – ConsoleNotifyUser, PropertyPageNotifyUser and WinformsNotifyUser.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The caller of the common code would pass in the correct implementation, allowing these API differences to be abstracted away from the common code.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><FONT size=3><FONT face=Calibri><SPAN style="COLOR: #1f497d; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape style="WIDTH: 444pt; HEIGHT: 300.75pt; VISIBILITY: visible; mso-wrap-style: square" id=Picture_x0020_1 o:spid="_x0000_i1026" type="#_x0000_t75"><v:imagedata mce_src="file:///C:\Users\nathann\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" src="file:///C:\Users\nathann\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" o:title=""></v:imagedata></v:shape></SPAN><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri></FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri></FONT></FONT></SPAN>&nbsp;</P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><A href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com//nathannesbits_images/9940313.aspx" original-url="https://blogs.msdn.com/photos/nathannesbits_images/picture9940313.aspx" target=_blank><IMG border=0 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/94/03/13/INotifyUser.png" original-url="https://blogs.msdn.com/photos/nathannesbits_images/images/9940313/596x405.aspx"></A>&nbsp;</SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri></FONT></FONT></SPAN>&nbsp;</P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>As time went on this interface changed into an abstract class, NotifyUser.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This class had abstract methods for Show/ShowMessageBox/ShowDialog, for the concrete sub-classes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>It also added higher level methods like ShowYesNoDialog, ShowConfirmDialog, etc… which internally called ShowMessageBox with the right parameters.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>This approach turned out very well.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Everything dealing with the details of child windows was encapsulated in one place, which provided a nice consistent API.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes"></SPAN></FONT></FONT></SPAN>&nbsp;</P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri><SPAN style="mso-spacerun: yes"></SPAN><o:p><IMG style="WIDTH: 464px; HEIGHT: 375px" title="NotifyUser UML" alt="NotifyUser UML" align=middle src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/94/03/14/NotifyUser.png" original-url="https://blogs.msdn.com/photos/nathannesbits_images/images/9940314/464x375.aspx" width=464 height=375 mce_src="https://blogs.msdn.com/photos/nathannesbits_images/images/9940314/464x375.aspx">&nbsp;</o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><FONT size=3><FONT face=Calibri><SPAN style="COLOR: #1f497d; mso-no-proof: yes; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><v:shape style="WIDTH: 460.5pt; HEIGHT: 372pt; VISIBILITY: visible; mso-wrap-style: square" id=Picture_x0020_4 o:spid="_x0000_i1025" type="#_x0000_t75"><v:imagedata mce_src="file:///C:\Users\nathann\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png" src="file:///C:\Users\nathann\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png" o:title=""></v:imagedata></v:shape></SPAN><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3 face=Calibri>Late in the project we had a spec change to use the </FONT><A href="https://msdn.microsoft.com/en-us/library/bb787471(VS.85).aspx" mce_href="https://msdn.microsoft.com/en-us/library/bb787471(VS.85).aspx"><FONT size=3 face=Calibri>Task Dialog</FONT></A><FONT size=3><FONT face=Calibri> in Windows for things like the YesNo and Confirm dialogs.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>After implementing a TaskDialog class which P/Invoked the APIs, we were able to wire it into the NotifyUser.ShowYesNoDialog/ShowConfirmDialog/etc implementations – and update all our dialogs without having to hunt down all the code which dealt with these dialogs.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>After that it was straight forward to update the methods, and callers, to take advantage of the additional functionality of the Task Dialog (extra text sections, icons, etc…).<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>I thought it actually worked out rather nice, and is a great example of the sort of power you get when you encapsulate logical functionality behind an interface.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>And those unit testers out there will appreciate how this helped make the logic more testable by allowing test code to use its own test double implementation of NotifyUser.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="COLOR: #1f497d; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-themecolor: dark2"><FONT size=3><FONT face=Calibri>Enjoy<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P>]]></content:encoded>
    </item>
    <item>
      <title>WPF ICommandSource Implementations Leak Memory!</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/wpf-icommandsource-implementations-leak-memory</link>
      <pubDate>Fri, 29 May 2009 17:30:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2009/05/29/wpf-icommandsource-implementations-leak-memory/</guid>
      <description><![CDATA[Actually the title of this article should be entitled “How to use WeakEventManager with ICommand...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Actually the title of this article should be entitled “How to use WeakEventManager with ICommand implementations”, but the memory leak title is more exciting (and true)</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"><FONT face=Calibri>Overview<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Some WPF controls, such as Buttons, are <I style="mso-bidi-font-style: normal">command sources</I>.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>A command source has 3 properties, Command, CommandParameter and CommandTarget.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>When the control does it’s action (like Click for Button)it Executes the command, using the CommandParameter and CommandTarget as inputs.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>WPF has provided an interface called ICommandSource to encapsulate this concept.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(Although there seems to be nothing significant which consumes the interface – just classes which implement it.)</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Most (all?) command sources implementations care about the CanExecute state, and usually disable themselves if CanExecute is false.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The implementations listen to the CanExecuteChanged event for the command, and when it is raised query the new CanExecute status.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>And that is where the issue begins – the implementations don’t unsubscribe from the event (except when the Command property changes).<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>So if the command object has a lifetime longer than any control(s) which list to it’s CanExecuteChanged event it will cause a memory leak of the control.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This is because when you register a handler for an event the delegate isn’t just a function pointer, but also include a reference to your instance.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"><FONT face=Calibri>The RoutedCommand Winkle<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>At this point you are probably saying to yourself “The logic seems sound, but I use commands and my app doesn’t appear to leak”.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The answer is because most apps use RoutedCommand, rather than their own implementation of ICommand.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The RoutedCommand.CanExecuteChanged implementation is to be a proxy for CommandManager.RequerySuggested, which if you read the docs or look at the IL keeps a weak reference to the delegate.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>That allows the delegate to be GCed, and along with it the strong reference to the command source which subscribed to the event.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>So to hit this you need to have a custom ICommand implementation which implements the CanExecute event the way most .NET events are implementd.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 14pt; LINE-HEIGHT: 115%"><FONT face=Calibri>Why I Think WPF’s CanExecuteChanged Implementation Is Wrong<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>No doubt someone on the WPF team thought they were being rather smart –after since most RoutedCommands are declared as static fields their lifetime is roughly the application’s lifetime.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>But I don’t think they were clever enough – because they redefined the standard contract for a .NET event.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Now anyone subscribing with RoutedCommand.CanExecuteChanged needs to be aware of this non-standard behavior – since if they don’t hold a strong reference to their delegate then it will be GCed and at some point the event seems to stop working.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(A really fun problem to debug BTW)</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Since they also have this nice ICommand interface which RoutedCommand is one implementation it also means that if you implement an ICommand you have to be aware of this behavior and also implement your event in a similar way holding weak references to the delegate, because the built-in consumers of the event (Button, etc…) rely on this behavior.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Furthermore since RoutedCommand.CanExecuteChanged event is really CommandManager.RequerySuggested each RoutedCommand instance shares the same event.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This means when the event is raised the sender field is null.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Another inconsistency from regular .NET event behavior.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>To me this is a big mess, and unfortunately one we probably will have to live with for the whole lifetime of WPF.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Why I consider it a mess is not the technical specifics of the solution, but because they redefined the standard .NET event contract.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT size=3><FONT face=Calibri>While we could argue that the .NET event contract has flaws, the fact of the matter is it is now a standard.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>And this means that both producers and consumers of that contact know how to correctly implement their end.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>What RoutedCommand.CanExecuteChanged has done is made a different contract where it holds weak references to the delegate.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This means that producers and consumers need to be coded differently that a standard .NET event.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>That is the issue from my point of view.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>If the WPF team has decided to do their own custom Observer pattern implementation that didn’t overload “event” I would be happy, since it would clearly be a different contract.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>By this I mean imagine if on ICommand instead of defining the CanExecuteChanged event they did something like this:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">interface</SPAN> <SPAN style="COLOR: #2b91af">ICommand<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">void</SPAN> RegisterCanExecuteListener(<SPAN style="COLOR: #2b91af">EventHandler</SPAN> listener);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">void</SPAN> UnregisterCanExecuteListener(<SPAN style="COLOR: #2b91af">EventHandler</SPAN> listener);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">bool</SPAN> CanExecute(<SPAN style="COLOR: blue">object</SPAN> parameter);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">void</SPAN> Execute(<SPAN style="COLOR: blue">object</SPAN> parameter);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Then they could clearly document the contract for the CanExecuteListener and no one would get confused with .NET event semantics.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><FONT size=3><FONT face=Calibri>How To Properly Implement A Command Source<o:p></o:p></FONT></FONT></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Now that I’m done with my rant I’ll give you some advice on how to deal with this.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Specifically in how to correctly implement your own command source implementation which will not leak regardless of the implementation of ICommand.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The short answer is to use WeakEventManager.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Unfortunately we need the long answer because since RoutedCommand.CanExecuteChanged doesn’t follow the standard contract it doesn’t work with WeakEventManager.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Specifically because:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>WeakEventManager doesn’t hold a strong reference to the delegate, so it will be GCed </FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Since the sender parameter is null, WeakEventManager can’t which listener to deliver the event to</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Notice the&nbsp;impact of not following the standard pattern&nbsp;means you can't use components designed to work with the standard pattern.&nbsp;</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The key is in the WeakEventManager implementation to use a proxy class for the ICommand implementation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This proxy class does 2 things:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Holds a strong reference to the delegate registered with ICommand.CanExecuteChanged</FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Since there is a 1:1 relationship between the proxy and the ICommand, and the handler delegate has a reference to the proxy we can get the reference to the associated ICommand</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The complete implementation of a WeakEventManager which works with all ICommand implementations is in the attached file.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The class is called CommandCanExecuteChangedWeakEventManager.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Then all the command source implementation needs to do is to use that and all will be well.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><FONT size=3><FONT face=Calibri>The Attached Project<o:p></o:p></FONT></FONT></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>If you build and run the attached project you will see a couple of buttons.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>One demonstrates the memory leak using Button. Simply open task manager, click the button and watch memory usage go up.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Click the GC button to ensure yourself that the objects really have a strong reference back to them.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The other parts of the UI are to demonstrate that CommandCanExecuteChangedWeakEventManager really works, both to deliver events and to avoid the memory leak.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Use at your own risk and other standard disclaimers apply.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Enjoy</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/65/85/90/CommandWeakEventManager.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-65-85-90/CommandWeakEventManager.zip">CommandWeakEventManager.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>WeakEventManager and IWeakEventListener</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/weakeventmanager-and-iweakeventlistener</link>
      <pubDate>Thu, 28 May 2009 13:54:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2009/05/28/weakeventmanager-and-iweakeventlistener/</guid>
      <description><![CDATA[When my team first starting hitting “memory leaks” due to event handlers we investigated a variety...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>When my team first starting hitting “memory leaks” due to event handlers we investigated a variety of solutions.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>There are many examples on the web for implementing some kind of weak listener – but interestingly almost all of them have some flaw.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In the end we settled on using WeakEventManager and its associated IWeakEventListener interface.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>However this isn’t perfect.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We have 3 major complaints with this setup:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>The tedium of implementing a new WeakEventManager</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Implementing the IWeakEventListener interface</FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>The scalability of WeakEventManager</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 16pt; LINE-HEIGHT: 115%"><FONT face=Calibri>Implementing a WeakEventManager<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Implementing a WeakEventManager isn’t hard.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In fact it is fairly mechanical.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>But that doesn’t make it good – especially if you need to implement a dozen of them.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Personally I hate implementing this sort of code because:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>It is boring</FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>It is more places where bugs can be introduced</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Currently my team copy-and-pastes an existing WeakEventManager and then modifies it.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>I’ve thought about how to simplify this by using generics or helper classes or something.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Unfortunately none of those ideas have significantly reduced the work enough to justify implementing it.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>So I’ve come to the conclusion that code generation is the answer.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In order to handle 90+% of the cases all one needs for the metadata is:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>The event source type</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>The event name</FONT></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3"><SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore"><FONT size=3>·</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>The event args type</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>I hope to explore this in the future and blog about it.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>As an aside I’d love to see Visual Studio come with this kind of built in support.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Imagine being able to right click on an event declaration and select “Generate Weak Event Manager” from a menu and have it generate the code and add it to your project?</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 16pt; LINE-HEIGHT: 115%"><FONT face=Calibri>Implementing the IWeakEventListener<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>I believe encapsulation is a good thing, especially for implementation details.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Whether a given class uses a WeakEventManager to subscribe to events listens to is an implementation detail.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Users of that class shouldn’t care, just so long as the implementation meets the contact and is well behaved (i.e. doesn’t cause memory leaks).</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Unfortunately the pattern for using WeakEventManager is to implement IWeakEventListener on the class which needs to do the listening.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Look at CollectionViewSource, CompositeCollection and BindingExpressionBase.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Due to this choice we know something about their implementation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>That doesn’t seem very encapsulated to me.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>What happens if the class no longer needs to use a WeakEventManager?<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>What happens if I directly call the RecieveWeakEvent method – which is public on those classes?</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>And don’t get me started on what the implementation of RevieveWeakEvent needs to look like if you subscribe to more than 1 event.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Thankfully we don’t have to rely on this pattern.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We can implement a small class which implements IWeakEventListener.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This class can either raise its own event, or call a delegate or whatever.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We can then contain an instance of this class in whatever class needs to do the listening.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>But we don’t want to implement tons of these little listener classes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>So generics to the rescue.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Using generics are can implement a single listener class that works for all types of events.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>And here it is:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> A common weak event listener which can be used for different kinds of events.<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;/summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;typeparam name="TEventArgs"&gt;</SPAN><SPAN style="COLOR: green">The EventArgs type for the event.</SPAN><SPAN style="COLOR: gray">&lt;/typeparam&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">class</SPAN> <SPAN style="COLOR: #2b91af">WeakEventListener</SPAN>&lt;TEventArgs&gt; : <SPAN style="COLOR: #2b91af">IWeakEventListener</SPAN> <SPAN style="COLOR: blue">where</SPAN> TEventArgs : <SPAN style="COLOR: #2b91af">EventArgs<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2b91af">EventHandler</SPAN>&lt;TEventArgs&gt; realHander;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> Initializes a new instance of the WeakEventListener class. <o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;/summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;param name="handler"&gt;</SPAN><SPAN style="COLOR: green">The handler for the event.</SPAN><SPAN style="COLOR: gray">&lt;/param&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">public</SPAN> WeakEventListener(<SPAN style="COLOR: #2b91af">EventHandler</SPAN>&lt;TEventArgs&gt; handler)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> (handler == <SPAN style="COLOR: blue">null</SPAN>)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">throw</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">ArgumentNullException</SPAN>(<SPAN style="COLOR: #a31515">"handler"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.realHander = handler;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> Receives events from the centralized event manager.<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;/summary&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;param name="managerType"&gt;</SPAN><SPAN style="COLOR: green">The type of the WeakEventManager calling this method.</SPAN><SPAN style="COLOR: gray">&lt;/param&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;param name="sender"&gt;</SPAN><SPAN style="COLOR: green">Object that originated the event.</SPAN><SPAN style="COLOR: gray">&lt;/param&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;param name="e"&gt;</SPAN><SPAN style="COLOR: green">Event data.</SPAN><SPAN style="COLOR: gray">&lt;/param&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;returns&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> true if the listener handled the event. It is considered an error by the WeakEventManager handling in WPF to register a listener for an event that the listener does not handle. Regardless, the method should return false if it receives an event that it does not recognize or handle.<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: gray">///</SPAN><SPAN style="COLOR: green"> </SPAN><SPAN style="COLOR: gray">&lt;/returns&gt;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">bool</SPAN> IWeakEventListener.ReceiveWeakEvent(<SPAN style="COLOR: #2b91af">Type</SPAN> managerType, <SPAN style="COLOR: #2b91af">Object</SPAN> sender, <SPAN style="COLOR: #2b91af">EventArgs</SPAN> e)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>TEventArgs realArgs = (TEventArgs)e;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">this</SPAN>.realHander(sender, realArgs);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>It is easy to use.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For each event you want to listen to you:</FONT></P>
<P class=MsoListParagraphCxSpFirst style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l3 level1 lfo4"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Add a field of type WeakEventListener&lt;T&gt; to your class</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l3 level1 lfo4"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Construct the WeakEventListener instance with a line like:</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">this</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.collectionChangedListener = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">WeakEventListener</SPAN>&lt;<SPAN style="COLOR: #2b91af">NotifyCollectionChangedEventArgs</SPAN>&gt;(<SPAN style="COLOR: blue">this</SPAN>.HandleCollectionChanged);<o:p></o:p></SPAN></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l3 level1 lfo4"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>3.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>Register the WeakEventListener with a line like:</FONT></P>
<P class=MsoListParagraphCxSpMiddle style="MARGIN: 0in 0in 0pt 0.5in"><SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">CollectionChangedEventManager</SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.AddListener(<SPAN style="COLOR: blue">this</SPAN>.sourceCollection, <SPAN style="COLOR: blue">this</SPAN>.collectionChangedListener);</SPAN></P>
<P class=MsoListParagraphCxSpLast style="MARGIN: 0in 0in 10pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l3 level1 lfo4"><SPAN style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>4.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT face=Calibri size=3>And of course implement your handler (HandleCollectionChanged in this example)</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT size=3><FONT face=Calibri>We now have a way to use a WeakEventManager in an encapsulated way (and have separate handlers without having to write a switch statement).<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 16pt; LINE-HEIGHT: 115%"><FONT face=Calibri>Scalability of WeakEventManager<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT size=3><FONT face=Calibri>One unfortunate thing we discovered is that as you get thousands of weak event listeners registering/unregistering for events the internal maintenance (called “purge” inside the WeakEventManager code) gets slow. <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>I mean really slow – like the user thinks your app has hung slow.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>BTW if you wonder why this is happening on the UI thread <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>it is because WeakEventManager is a subclass of DispatcherObject and therefore has a thread affinity.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>No doubt the creators thought they were being clever in avoiding synchronization issues.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Currently there is no fix for this other than reverse engineering WeakEventManager and coding a new one with a more performant purge (or at least enable purge to happen on a background thread in a non-blocking way).</FONT></P>]]></content:encoded>
    </item>
    <item>
      <title>Creating Attached Properties And Services</title>
      <link>https://docs.microsoft.com/archive/blogs/nathannesbit/creating-attached-properties-and-services</link>
      <pubDate>Wed, 27 May 2009 18:49:00 GMT</pubDate>
      <dc:creator><![CDATA[MSDN Archive]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nathannesbit/2009/05/27/creating-attached-properties-and-services/</guid>
      <description><![CDATA[One cool thing about DependencyProperties is the ability to have AttachedDependencyProperties (or...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>One cool thing about DependencyProperties is the ability to have AttachedDependencyProperties (or Attached Property for short).<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Regular properties are defined on a type and exist for instances of that type and sub-classes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>An Attached Property is defined in one type, but can be applied to any instance of DependencyObject.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>So how does this work?<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Well the simplistic model is that DependencyObject defines a dictionary to hold property values.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The key in the dictionary is the DependencyProperty instance and the value is the current value for that property.<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Adding an attached property simply adds a new entry into the dictionary for a given object.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>To define an attached property one typically writes code that looks like this:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">public</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">readonly</SPAN> <SPAN style="COLOR: #2b91af">DependencyProperty</SPAN> IsTextTrimmedMonitoringEnabledProperty = <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">DependencyProperty</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">.RegisterAttached( <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">"IsTextTrimmedMonitoringEnabled"</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">, <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">typeof</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(<SPAN style="COLOR: blue">bool</SPAN>), <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">typeof</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(<SPAN style="COLOR: #2b91af">TextBlockService</SPAN>), <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">new</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: #2b91af">PropertyMetadata</SPAN>( <SPAN style="COLOR: #2b91af">false</SPAN>, IsTextTrimmedMonitoringEnabledProperty_PropertyChanged) );<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">static</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> GetIsTextTrimmedMonitoringEnabled(<SPAN style="COLOR: #2b91af">DependencyObject</SPAN> element)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">return</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> (<SPAN style="COLOR: blue">bool</SPAN>) element.GetValue(IsTextTrimmedMonitoringEnabledProperty);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">static</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">void</SPAN> SetIsTextTrimmedMonitoringEnabled(<SPAN style="COLOR: #2b91af">DependencyObject</SPAN> element, <SPAN style="COLOR: blue">bool</SPAN> value)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">element.SetValue(IsTextTrimmedMonitoringEnabledProperty, value);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">static</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">void</SPAN> IsTextTrimmedMonitoringEnabledProperty_PropertyChanged(<SPAN style="COLOR: #2b91af">DependencyObject</SPAN> o, <SPAN style="COLOR: #2b91af">DependencyPropertyChangedEventArgs</SPAN> e)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">// handle the property being changed for "o"<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The DependencyProperty field holds the DP instance, which as we discussed is essentially the key to the value.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>The Get/Set methods are a best practice to encapsulate the details of how to get/set the value.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Like any dependency property there are scenarios where the value can be changed/retrieved directly from the dictionary – so don’t put any logic in there, beyond calling GetValue/SetValue.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>Attached properties are common used to implement something called Attached Services (or Attached Behavior), which is logic which you want to associate with a given object.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The PropertyChanged method is the entry point to get that logic executing.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>John Gossman has a good example for an </FONT><A href="https://blogs.msdn.com/johngossman/archive/2008/05/16/attachedbehavior-pattern-sample.aspx"><FONT face=Calibri size=3>attached service</FONT></A><FONT face=Calibri size=3>.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT size=3><FONT face=Calibri>When writing an attached service often you’ll find that you need to keep some additional state for each object.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>One way to do that is to define a ReadOnlyAttachedService.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>When doing this if the read only attached property should be read outside the class implementing the property (say to be used in XAML to drive a trigger) then the DependencyProperty field and Get method are public, but the Set method is private.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>If the value contains state which is internal to the workings of the attached service, then make everything private.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><FONT face=Calibri size=3>If you find your state extends beyond a few simple fields then you might find it handy to define a single read only attached property which holds a value which is an instance of a data class you defined to hold all your state.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>]]></content:encoded>
    </item>
  </channel>
</rss>