<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Damon Payne: Hand waving software architect</title>
    <link>http://www.damonpayne.com/</link>
    <description>Solution Architect, software developer, geek</description>
    <language>en-us</language>
    <copyright>Damon Payne</copyright>
    <lastBuildDate>Fri, 06 Nov 2009 04:18:41 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>damon@damonpayne.com</managingEditor>
    <webMaster>damon@damonpayne.com</webMaster>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/DamonPayne" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=61882413-36ba-4a8d-88b9-16d3eaf08e47</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,61882413-36ba-4a8d-88b9-16d3eaf08e47.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,61882413-36ba-4a8d-88b9-16d3eaf08e47.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=61882413-36ba-4a8d-88b9-16d3eaf08e47</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In just over a week, on November 14th, I will be speaking at the Chippewa Valley Code
Camp taking place way up in Menomonie, WI on the UW-Stout campus.  The topic
is <u>Advanced Silverlight Development</u> however the content will be somewhat different
than my talk by the same name I gave at MadCamp a little while ago.  Doug and
company run a healthy community so I’m looking forward to good conversations.
</p>
        <p>
Details on the code camp can be found at: <a title="http://www.chippewavalleycodecamp.com/" href="http://www.chippewavalleycodecamp.com/">http://www.chippewavalleycodecamp.com/</a> 
</p>
        <p>
The schedule of topics can be found here: <a title="http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx" href="http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx">http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx</a></p>
        <p>
The elusive <strong><u>Christopher J. Barwick</u></strong> will be accompanying me
and making his return to Public Nerd Life.  I am looking forward to seeing Jason
Bock and Chris speak, and I’ll probably check out some of the non-Microsoft or Building
Better Software topics in between.  We’ll be up the night before and open to
grabbing a drink with any local geeks or speakers who may be available.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=61882413-36ba-4a8d-88b9-16d3eaf08e47" />
      </body>
      <title>Speaking at Chippewa Valley Code Camp 2009</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,61882413-36ba-4a8d-88b9-16d3eaf08e47.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/7cwJKJmukio/SpeakingAtChippewaValleyCodeCamp2009.aspx</link>
      <pubDate>Fri, 06 Nov 2009 04:18:41 GMT</pubDate>
      <description>&lt;p&gt;
In just over a week, on November 14th, I will be speaking at the Chippewa Valley Code
Camp taking place way up in Menomonie, WI on the UW-Stout campus.&amp;#160; The topic
is &lt;u&gt;Advanced Silverlight Development&lt;/u&gt; however the content will be somewhat different
than my talk by the same name I gave at MadCamp a little while ago.&amp;#160; Doug and
company run a healthy community so I’m looking forward to good conversations.
&lt;/p&gt;
&lt;p&gt;
Details on the code camp can be found at: &lt;a title="http://www.chippewavalleycodecamp.com/" href="http://www.chippewavalleycodecamp.com/"&gt;http://www.chippewavalleycodecamp.com/&lt;/a&gt;&amp;#160;
&lt;/p&gt;
&lt;p&gt;
The schedule of topics can be found here: &lt;a title="http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx" href="http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx"&gt;http://www.chippewavalleycodecamp.com/Schedule/2009Schedule/tabid/74/Default.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The elusive &lt;strong&gt;&lt;u&gt;Christopher J. Barwick&lt;/u&gt;&lt;/strong&gt; will be accompanying me
and making his return to Public Nerd Life.&amp;#160; I am looking forward to seeing Jason
Bock and Chris speak, and I’ll probably check out some of the non-Microsoft or Building
Better Software topics in between.&amp;#160; We’ll be up the night before and open to
grabbing a drink with any local geeks or speakers who may be available.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=61882413-36ba-4a8d-88b9-16d3eaf08e47" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,61882413-36ba-4a8d-88b9-16d3eaf08e47.aspx</comments>
      <category>.NET</category>
      <category>Milwaukee .NET Community</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/11/06/SpeakingAtChippewaValleyCodeCamp2009.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=1e5cc2a5-4bc7-47fc-a53e-29f08321e149</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,1e5cc2a5-4bc7-47fc-a53e-29f08321e149.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,1e5cc2a5-4bc7-47fc-a53e-29f08321e149.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=1e5cc2a5-4bc7-47fc-a53e-29f08321e149</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In order to increase the prestige of the Microsoft MVP program and the practice of
.Net user group presentations, I have purchased a new automobile.
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/NewCar_D9F2/DamonS4_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="DamonS4" border="0" alt="DamonS4" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/NewCar_D9F2/DamonS4_thumb.jpg" width="644" height="432" />
          </a>
        </p>
        <p>
This is my 2010 Audi S4 – there are many like it but this one is mine.  After
driving the WRX for almost 9 years I thought I could splurge a little bit.  
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=1e5cc2a5-4bc7-47fc-a53e-29f08321e149" />
      </body>
      <title>New Car</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,1e5cc2a5-4bc7-47fc-a53e-29f08321e149.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/m2QlSLAIlxY/NewCar.aspx</link>
      <pubDate>Fri, 23 Oct 2009 20:30:23 GMT</pubDate>
      <description>&lt;p&gt;
In order to increase the prestige of the Microsoft MVP program and the practice of
.Net user group presentations, I have purchased a new automobile.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/NewCar_D9F2/DamonS4_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="DamonS4" border="0" alt="DamonS4" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/NewCar_D9F2/DamonS4_thumb.jpg" width="644" height="432" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
This is my 2010 Audi S4 – there are many like it but this one is mine.&amp;#160; After
driving the WRX for almost 9 years I thought I could splurge a little bit.&amp;#160; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=1e5cc2a5-4bc7-47fc-a53e-29f08321e149" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,1e5cc2a5-4bc7-47fc-a53e-29f08321e149.aspx</comments>
      <category>Personal</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/10/23/NewCar.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=5313f8fb-38ea-471a-aac7-81621f40fe8e</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,5313f8fb-38ea-471a-aac7-81621f40fe8e.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,5313f8fb-38ea-471a-aac7-81621f40fe8e.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=5313f8fb-38ea-471a-aac7-81621f40fe8e</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This may be obvious to some, but by calling out to JavaScript within the document
hosting your Silverlight application, you can do some powerful things.  Back
in the day, we used to sometimes use an href with “mailto” as a poor man’s contact
form – hosting solutions with databases and backups weren’t always as cheap and plentiful
as they are today.  If you lack a Service Layer to email solution in your environment,
you can always try the following:
</p>
        <pre class="code">
          <span style="color: blue">private void </span>Mailto_Click(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: blue">var </span>svc = <span style="color: blue">new </span>Modules.<span style="color: #2b91af">BrowserHostService</span>();
svc.CallHostFunction(<span style="color: #a31515">"MailTo"</span>, <span style="color: #a31515">"damon@damonpayne.com"</span>, <span style="color: #a31515">"Damon"</span>, <span style="color: #a31515">"Try
this Wine!"</span>, <span style="color: #a31515">"Roger Sabon Chateauneuf
du Pape 2005"</span>, <span style="color: #a31515">"11F16974-8EE1-4c0c-8AB4-21EF0B7BF087"</span>);
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Where BrowserHostService is part of a demo that I’m working on.  The implementation
is very simple:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">BrowserHostService </span>: <span style="color: #2b91af">IHostService </span>{ <span style="color: blue">public
void </span>CallHostFunction(<span style="color: blue">string </span>func, <span style="color: blue">params
string</span>[] args) { <span style="color: #2b91af">HtmlPage</span>.Window.CreateInstance(func,
args); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This method does everything needed to invoke a JavaScript method on the document containing
the Silverlight application.  For invoking the user’s default mail program, you
could write a function like what’s shown below.  The only item of note is that
the parameter orders must match up.  If you’ve done much Reflection coding you
should be used to this.
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">script </span>
          <span style="color: red">type</span>
          <span style="color: blue">="text/javascript"&gt;
function </span>MailTo(toEmail, fromUser, subject, suggestTopic, suggestCode) { <span style="color: blue">var </span>body
= fromUser + <span style="color: #a31515">' thought you would really enjoy ' </span>+
suggestTopic + <span style="color: #a31515">' and there is some info about that on
TastingProject.com. ' </span>+ <span style="color: #a31515">'Why not head over to
http://localhost:5785/TastingProject.UITestPage.aspx?code=' </span>+ suggestCode + <span style="color: #a31515">'
and check it out?'</span>; window.open(<span style="color: #a31515">'mailto:'</span>+
toEmail +<span style="color: #a31515">'?subject=' </span>+ subject+<span style="color: #a31515">'&amp;body='</span>+body);
} <span style="color: blue">&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The preceding C# and JavaScript produces the following result on my machine in Outlook:
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/SilverlightandMailto_14726/AGmailto_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AGmailto" border="0" alt="AGmailto" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/SilverlightandMailto_14726/AGmailto_thumb.png" width="644" height="286" />
          </a>
        </p>
        <p>
Of course you would have to rely on your end user to hit the Send button but this
may be a quick and dirty way to get email functionality if you don’t have complete
control over your environment.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=5313f8fb-38ea-471a-aac7-81621f40fe8e" />
      </body>
      <title>Silverlight and Mailto:</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,5313f8fb-38ea-471a-aac7-81621f40fe8e.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/Uj3qsQS5XzY/SilverlightAndMailto.aspx</link>
      <pubDate>Thu, 22 Oct 2009 04:24:18 GMT</pubDate>
      <description>&lt;p&gt;
This may be obvious to some, but by calling out to JavaScript within the document
hosting your Silverlight application, you can do some powerful things.&amp;#160; Back
in the day, we used to sometimes use an href with “mailto” as a poor man’s contact
form – hosting solutions with databases and backups weren’t always as cheap and plentiful
as they are today.&amp;#160; If you lack a Service Layer to email solution in your environment,
you can always try the following:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Mailto_Click(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;svc = &lt;span style="color: blue"&gt;new &lt;/span&gt;Modules.&lt;span style="color: #2b91af"&gt;BrowserHostService&lt;/span&gt;();
svc.CallHostFunction(&lt;span style="color: #a31515"&gt;&amp;quot;MailTo&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;damon@damonpayne.com&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Damon&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Try
this Wine!&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Roger Sabon Chateauneuf
du Pape 2005&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;11F16974-8EE1-4c0c-8AB4-21EF0B7BF087&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Where BrowserHostService is part of a demo that I’m working on.&amp;#160; The implementation
is very simple:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BrowserHostService &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IHostService &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
void &lt;/span&gt;CallHostFunction(&lt;span style="color: blue"&gt;string &lt;/span&gt;func, &lt;span style="color: blue"&gt;params
string&lt;/span&gt;[] args) { &lt;span style="color: #2b91af"&gt;HtmlPage&lt;/span&gt;.Window.CreateInstance(func,
args); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This method does everything needed to invoke a JavaScript method on the document containing
the Silverlight application.&amp;#160; For invoking the user’s default mail program, you
could write a function like what’s shown below.&amp;#160; The only item of note is that
the parameter orders must match up.&amp;#160; If you’ve done much Reflection coding you
should be used to this.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
function &lt;/span&gt;MailTo(toEmail, fromUser, subject, suggestTopic, suggestCode) { &lt;span style="color: blue"&gt;var &lt;/span&gt;body
= fromUser + &lt;span style="color: #a31515"&gt;' thought you would really enjoy ' &lt;/span&gt;+
suggestTopic + &lt;span style="color: #a31515"&gt;' and there is some info about that on
TastingProject.com. ' &lt;/span&gt;+ &lt;span style="color: #a31515"&gt;'Why not head over to
http://localhost:5785/TastingProject.UITestPage.aspx?code=' &lt;/span&gt;+ suggestCode + &lt;span style="color: #a31515"&gt;'
and check it out?'&lt;/span&gt;; window.open(&lt;span style="color: #a31515"&gt;'mailto:'&lt;/span&gt;+
toEmail +&lt;span style="color: #a31515"&gt;'?subject=' &lt;/span&gt;+ subject+&lt;span style="color: #a31515"&gt;'&amp;amp;body='&lt;/span&gt;+body);
} &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The preceding C# and JavaScript produces the following result on my machine in Outlook:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/SilverlightandMailto_14726/AGmailto_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AGmailto" border="0" alt="AGmailto" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/SilverlightandMailto_14726/AGmailto_thumb.png" width="644" height="286" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Of course you would have to rely on your end user to hit the Send button but this
may be a quick and dirty way to get email functionality if you don’t have complete
control over your environment.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=5313f8fb-38ea-471a-aac7-81621f40fe8e" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,5313f8fb-38ea-471a-aac7-81621f40fe8e.aspx</comments>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/10/22/SilverlightAndMailto.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=7b63d6ea-a4dd-417d-a556-a8af54cb3bac</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,7b63d6ea-a4dd-417d-a556-a8af54cb3bac.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,7b63d6ea-a4dd-417d-a556-a8af54cb3bac.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=7b63d6ea-a4dd-417d-a556-a8af54cb3bac</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On Saturday, October 24th I will be speaking at the Madison .NET User’s Group’s annual
Code Camp: MadCamp.  You can find general information about MadCamp <a href="http://maddotnet.com/Events/MADcamp/tabid/187/Default.aspx">here</a>.
My presentation will be on <a href="http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/211/Developer-MADcamp-Advanced-Silverlight-Topics.aspx">Advanced
Silverlight Topics</a>.  They are going to be giving away a lot of cool stuff,
particularly around Windows 7.  The group has combined the code camp event with
a Microsoft sponsored Windows 7 launch party so it should be pretty sweet.  I
will be tweeting on the subject using #madcamp09 and I hope to see you there
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=7b63d6ea-a4dd-417d-a556-a8af54cb3bac" />
      </body>
      <title>Speaking at MadCamp and 7 Fest</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,7b63d6ea-a4dd-417d-a556-a8af54cb3bac.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/cPfC2OgduXE/SpeakingAtMadCampAnd7Fest.aspx</link>
      <pubDate>Thu, 15 Oct 2009 01:36:51 GMT</pubDate>
      <description>&lt;p&gt;
On Saturday, October 24th I will be speaking at the Madison .NET User’s Group’s annual
Code Camp: MadCamp.&amp;#160; You can find general information about MadCamp &lt;a href="http://maddotnet.com/Events/MADcamp/tabid/187/Default.aspx"&gt;here&lt;/a&gt;.
My presentation will be on &lt;a href="http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/211/Developer-MADcamp-Advanced-Silverlight-Topics.aspx"&gt;Advanced
Silverlight Topics&lt;/a&gt;.&amp;#160; They are going to be giving away a lot of cool stuff,
particularly around Windows 7.&amp;#160; The group has combined the code camp event with
a Microsoft sponsored Windows 7 launch party so it should be pretty sweet.&amp;#160; I
will be tweeting on the subject using #madcamp09 and I hope to see you there
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=7b63d6ea-a4dd-417d-a556-a8af54cb3bac" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,7b63d6ea-a4dd-417d-a556-a8af54cb3bac.aspx</comments>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/10/15/SpeakingAtMadCampAnd7Fest.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <i>The AGT (Argentum Tela) series of articles is an effort to do two things. Usually
an idea is presented only in its finished form. The first goal is to do some Reality
Blogging, to show an idea evolve over time without pulling any punches. The second
goal, and the example vehicle for the evolution aspect, is an extensible Design Surface
for Silverlight similar to what we have in Visual Studio 2008. This type of application
has all sorts of interesting uses. My example is a Home Theater layout tool. Read
the entire saga: </i>
          <a href="http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx">
            <i>http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx</i>
          </a>
        </p>
        <h1>What’s in a name?
</h1>
        <p>
If you have worked in the Windows Forms designer, ASP.Net ViewState, ASP.Net Control
Templates, or ever used a design surface of any kind you are likely familiar with
the concepts of a Naming Container and Name Providers.  The first Label I drag
onto a form might get a name of “Label0”, then “Label1” and so on.  Many visual
technologies will throw an exception if two items inside the same container have the
same name.  We will need this idea for AGT very soon, so now is the time to implement
it.
</p>
        <h2>Design Goals
</h2>
        <p>
The design goals for this next step are fairly simple:
</p>
        <ol>
          <li>
            <font face="Verdana">We must have a notion of A Container.  This container has
children, this container will use some sort of Name provider to provide a default
name for each new Child added to it.</font>
          </li>
          <li>
            <font face="Verdana">The Type of the children should be inspect able, such that we
can supply names like “Foo0”, etc.</font>
          </li>
          <li>
            <font face="Verdana">We do not need to support a parent-child relationship between
Containers (like asp.net does).</font>
          </li>
          <li>
            <font face="Verdana">We should let the Caller set the name on its child</font>
          </li>
        </ol>
        <p>
The only remaining decisions are small but important.  
</p>
        <ol>
          <li>
Is there any value to having an INamingContainer interface or can we use an existing
mechanism to expose children? 
</li>
          <li>
Is there any value to having an INameable interface for items to be named? 
</li>
        </ol>
        <p>
          <font face="Arial">I’m going with “yes”, and “no”, respectively.</font>
        </p>
        <h2>
          <font face="Arial">INamingContainer</font>
        </h2>
        <p>
I’m going to create a simple INamingContainer interface which can be implemented by
containers (like a design surface) that want their users to have a unique name. 
I could make this part of the IDesigner interface but I’d like this to be optional. 
I am still in my IDesignableControl-centric world though.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>DamonPayne.AGT.Design
{ <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Represents
something that has children needing unique names </span><span style="color: gray">///
&lt;/summary&gt; </span><span style="color: blue">public interface </span><span style="color: #2b91af">INamingContainer </span>{ <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">IDesignableControl</span>&gt;
Children{<span style="color: blue">get</span>;} } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This is all we need, we can now enumerate the children and generate a unique name.
</p>
        <h2>INameProvider
</h2>
        <p>
The interface for INameProvider is equally simple.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>DamonPayne.AGT.Design.Contracts
{ <span style="color: blue">public interface </span><span style="color: #2b91af">INameProvider </span>{ <span style="color: blue">string </span>GetUniqueName(<span style="color: #2b91af">INamingContainer </span>container, <span style="color: #2b91af">IDesignableControl </span>newChild);
} }</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>As I mentioned before, I’d like to
follow the familiar convention of ControlType[number], so I’m ready to build a default
implementation for INameProvider.  This is pretty easy using LINQ.
</p>
        <pre style="width: 680px; height: 265px" class="code">
          <span style="color: blue">namespace </span>DamonPayne.AGT.Design.Services
{ <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">A
name provider that uses the last part of type name plus </span><span style="color: gray">///
&lt;/summary&gt; </span><span style="color: blue">public class </span><span style="color: #2b91af">TypeScopedNameProvider </span>: <span style="color: #2b91af">INameProvider </span>{ <span style="color: blue">public
string </span>GetUniqueName(<span style="color: #2b91af">INamingContainer </span>container, <span style="color: #2b91af">IDesignableControl </span>newChild)
{ <span style="color: blue">var </span>type = newChild.GetType(); <span style="color: blue">string </span>namePart
= type.Name; <span style="color: blue">int </span>existingCount = (<span style="color: blue">from </span>c <span style="color: blue">in </span>container.Children <span style="color: blue">where </span>c.GetType()
== type <span style="color: blue">select </span>c).Count(); <span style="color: blue">return </span>namePart
+ existingCount; } } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <h2>Hooking up DesignSurface
</h2>
        <p>
In order to finish up, I only need to hook my implementation up using Unity and call
the name provider whenever a new item is added to the DesignSurface.
</p>
        <pre style="width: 680px; height: 55px" class="code">[<span style="color: #2b91af">Dependency</span>] <span style="color: blue">public </span><span style="color: #2b91af">INameProvider </span>NameProvider
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
        <p>
 
</p>
        <pre class="code">
          <span style="color: blue">private void </span>EnsureName(<span style="color: #2b91af">IDesignableControl </span>dc)
{ <span style="color: blue">if </span>(<span style="color: blue">string</span>.IsNullOrEmpty(dc.DesignTimeName))
{ <span style="color: blue">string </span>name = NameProvider.GetUniqueName(<span style="color: blue">this</span>,
((<span style="color: #2b91af">DesignSite</span>)dc).HostedContent); dc.DesignTimeName
= name; } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I can drag things out without a name now, and everything looks the way it should with
the generated naming.  My glorious purple couches and poorly drawn speakers have
names!  Now that I’ve been learning Expression Design I should really take another
crack at drawing those speakers.
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ArgentumTelaDesignSurface20NameProvider_12877/21ss0_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="21ss0" border="0" alt="21ss0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ArgentumTelaDesignSurface20NameProvider_12877/21ss0_thumb.jpg" width="644" height="449" />
          </a>
        </p>
        <p>
I’ve really got to fix the way that PropertyGrid looks too.
</p>
        <h2>Conclusion
</h2>
        <p>
The Service/Module based approach continues to prove itself out as the correct approach
for making it easy to add new functionality to the system.  Adding a name provider
was a small feature, but the next step won’t be so easy.
</p>
        <p>
You can get the source code <a href="http://agt.codeplex.com/SourceControl/ListDownloadableCommits.aspx">from
codeplex</a>.  This article represents change set 28479.
</p>
        <p>
The live demo has been updated and can be viewed <a href="http://www.damonpayne.com/agt">here</a>.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07" />
      </body>
      <title>Argentum Tela Design Surface [21] &amp;ndash; Name Provider</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/dFxZy0EyoUQ/ArgentumTelaDesignSurface21NdashNameProvider.aspx</link>
      <pubDate>Mon, 05 Oct 2009 02:08:07 GMT</pubDate>
      <description>&lt;p&gt;
&lt;i&gt;The AGT (Argentum Tela) series of articles is an effort to do two things. Usually
an idea is presented only in its finished form. The first goal is to do some Reality
Blogging, to show an idea evolve over time without pulling any punches. The second
goal, and the example vehicle for the evolution aspect, is an extensible Design Surface
for Silverlight similar to what we have in Visual Studio 2008. This type of application
has all sorts of interesting uses. My example is a Home Theater layout tool. Read
the entire saga: &lt;/i&gt;&lt;a href="http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx"&gt;&lt;i&gt;http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx&lt;/i&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h1&gt;What’s in a name?
&lt;/h1&gt;
&lt;p&gt;
If you have worked in the Windows Forms designer, ASP.Net ViewState, ASP.Net Control
Templates, or ever used a design surface of any kind you are likely familiar with
the concepts of a Naming Container and Name Providers.&amp;#160; The first Label I drag
onto a form might get a name of “Label0”, then “Label1” and so on.&amp;#160; Many visual
technologies will throw an exception if two items inside the same container have the
same name.&amp;#160; We will need this idea for AGT very soon, so now is the time to implement
it.
&lt;/p&gt;
&lt;h2&gt;Design Goals
&lt;/h2&gt;
&lt;p&gt;
The design goals for this next step are fairly simple:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;We must have a notion of A Container.&amp;#160; This container has
children, this container will use some sort of Name provider to provide a default
name for each new Child added to it.&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;The Type of the children should be inspect able, such that we
can supply names like “Foo0”, etc.&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;We do not need to support a parent-child relationship between
Containers (like asp.net does).&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;We should let the Caller set the name on its child&lt;/font&gt; 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The only remaining decisions are small but important.&amp;#160; 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Is there any value to having an INamingContainer interface or can we use an existing
mechanism to expose children? 
&lt;/li&gt;
&lt;li&gt;
Is there any value to having an INameable interface for items to be named? 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;font face="Arial"&gt;I’m going with “yes”, and “no”, respectively.&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;&lt;font face="Arial"&gt;INamingContainer&lt;/font&gt;
&lt;/h2&gt;
&lt;p&gt;
I’m going to create a simple INamingContainer interface which can be implemented by
containers (like a design surface) that want their users to have a unique name.&amp;#160;
I could make this part of the IDesigner interface but I’d like this to be optional.&amp;#160;
I am still in my IDesignableControl-centric world though.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;DamonPayne.AGT.Design
{ &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Represents
something that has children needing unique names &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;INamingContainer &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IDesignableControl&lt;/span&gt;&amp;gt;
Children{&lt;span style="color: blue"&gt;get&lt;/span&gt;;} } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This is all we need, we can now enumerate the children and generate a unique name.
&lt;/p&gt;
&lt;h2&gt;INameProvider
&lt;/h2&gt;
&lt;p&gt;
The interface for INameProvider is equally simple.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;DamonPayne.AGT.Design.Contracts
{ &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;INameProvider &lt;/span&gt;{ &lt;span style="color: blue"&gt;string &lt;/span&gt;GetUniqueName(&lt;span style="color: #2b91af"&gt;INamingContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;IDesignableControl &lt;/span&gt;newChild);
} }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As I mentioned before, I’d like to
follow the familiar convention of ControlType[number], so I’m ready to build a default
implementation for INameProvider.&amp;#160; This is pretty easy using LINQ.
&lt;/p&gt;
&lt;pre style="width: 680px; height: 265px" class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;DamonPayne.AGT.Design.Services
{ &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;A
name provider that uses the last part of type name plus &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeScopedNameProvider &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INameProvider &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
string &lt;/span&gt;GetUniqueName(&lt;span style="color: #2b91af"&gt;INamingContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;IDesignableControl &lt;/span&gt;newChild)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;type = newChild.GetType(); &lt;span style="color: blue"&gt;string &lt;/span&gt;namePart
= type.Name; &lt;span style="color: blue"&gt;int &lt;/span&gt;existingCount = (&lt;span style="color: blue"&gt;from &lt;/span&gt;c &lt;span style="color: blue"&gt;in &lt;/span&gt;container.Children &lt;span style="color: blue"&gt;where &lt;/span&gt;c.GetType()
== type &lt;span style="color: blue"&gt;select &lt;/span&gt;c).Count(); &lt;span style="color: blue"&gt;return &lt;/span&gt;namePart
+ existingCount; } } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;Hooking up DesignSurface
&lt;/h2&gt;
&lt;p&gt;
In order to finish up, I only need to hook my implementation up using Unity and call
the name provider whenever a new item is added to the DesignSurface.
&lt;/p&gt;
&lt;pre style="width: 680px; height: 55px" class="code"&gt;[&lt;span style="color: #2b91af"&gt;Dependency&lt;/span&gt;] &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;INameProvider &lt;/span&gt;NameProvider
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;EnsureName(&lt;span style="color: #2b91af"&gt;IDesignableControl &lt;/span&gt;dc)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(dc.DesignTimeName))
{ &lt;span style="color: blue"&gt;string &lt;/span&gt;name = NameProvider.GetUniqueName(&lt;span style="color: blue"&gt;this&lt;/span&gt;,
((&lt;span style="color: #2b91af"&gt;DesignSite&lt;/span&gt;)dc).HostedContent); dc.DesignTimeName
= name; } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I can drag things out without a name now, and everything looks the way it should with
the generated naming.&amp;#160; My glorious purple couches and poorly drawn speakers have
names!&amp;#160; Now that I’ve been learning Expression Design I should really take another
crack at drawing those speakers.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ArgentumTelaDesignSurface20NameProvider_12877/21ss0_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="21ss0" border="0" alt="21ss0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ArgentumTelaDesignSurface20NameProvider_12877/21ss0_thumb.jpg" width="644" height="449" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
I’ve really got to fix the way that PropertyGrid looks too.
&lt;/p&gt;
&lt;h2&gt;Conclusion
&lt;/h2&gt;
&lt;p&gt;
The Service/Module based approach continues to prove itself out as the correct approach
for making it easy to add new functionality to the system.&amp;#160; Adding a name provider
was a small feature, but the next step won’t be so easy.
&lt;/p&gt;
&lt;p&gt;
You can get the source code &lt;a href="http://agt.codeplex.com/SourceControl/ListDownloadableCommits.aspx"&gt;from
codeplex&lt;/a&gt;.&amp;#160; This article represents change set 28479.
&lt;/p&gt;
&lt;p&gt;
The live demo has been updated and can be viewed &lt;a href="http://www.damonpayne.com/agt"&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,c44cf438-bc07-45c3-bfc1-e6d8bd0b6a07.aspx</comments>
      <category>Architecture and Design</category>
      <category>Designer Support</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/10/05/ArgentumTelaDesignSurface21NdashNameProvider.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=8aadfbff-bb97-4f79-85e5-7f60763e5a9e</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,8aadfbff-bb97-4f79-85e5-7f60763e5a9e.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,8aadfbff-bb97-4f79-85e5-7f60763e5a9e.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8aadfbff-bb97-4f79-85e5-7f60763e5a9e</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <i>The AGT (Argentum Tela) series of articles is an effort to do two things. Usually
an idea is presented only in its finished form. The first goal is to do some Reality
Blogging, to show an idea evolve over time without pulling any punches. The second
goal, and the example vehicle for the evolution aspect, is an extensible Design Surface
for Silverlight similar to what we have in Visual Studio 2008. This type of application
has all sorts of interesting uses. My example is a Home Theater layout tool. Read
the entire saga: </i>
          <a href="http://www.damonpayne.com/ct.ashx?id=63976c45-3b0f-422e-8a49-df2e73b26b2c&amp;url=http%3a%2f%2fwww.damonpayne.com%2f2008%2f09%2f14%2fRunTimeIsDesignTimeForAGT0.aspx">
            <i>http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx</i>
          </a>
        </p>
        <h2>Atrophy
</h2>
        <p>
As of <a href="http://www.damonpayne.com/2009/01/12/ArgentumTelaDesignSurface19.aspx">AGT[19]</a> I
was getting ready to extend Commanding and introduce some other concepts in order
to get Undo/Redo support.  Of course, that was many months ago and I’ve let this
sit for a while haven’t I?  Since that time a lot has happened.  Silverlight
3 has shipped, there are many more class libraries available, and Silverlight in general
is being taken more seriously.  It’s time to continue working on the design surface
project.
</p>
        <p>
Ok, fine, I also need AGT for some other projects.  I need to take some time
to bring the project up to the state of the art and change direction slightly to move
the road map around to be more in line with my own short term needs.  Let’s get
started.
</p>
        <p>
If you’re new to this project, you can also check out the <a href="http://agt.codeplex.com/Wiki/View.aspx?title=Home">Codeplex
site</a>.
</p>
        <h2>Silverlight 3 Upgrade
</h2>
        <p>
Since I’ve been through a machine repave or two since my last work I had to get Team
System set back up for Codeplex.  I didn’t realize this until after I’d opened
the solution and done the conversion wizard so I was concerned.  Sure enough,
the project upgrade hozed the source control bindings but I was able to get it fixed
using the Change Source Control command.  With this done I built and ran with
no problems, however trying to change anything revealed that source control was most
likely irrevocably ruined so I got latest and went back to work.
</p>
        <p>
Once I got through all this, the Silverlight 3 upgrade went off without a hitch. 
I found exactly zero breaking changes in the application so far; this is much nicer
than some of the previous Silverlight 2 cycles I had gone through.
</p>
        <p>
While no changes were strictly necessary, I had to think about taking advantage of
new Silverlight 3 features.  I quickly cycled through my mental list of new Silverlight
3 features.  I may use things like Behaviors in the future.  One thing I
wasn’t thrilled with from Silverlight 2 was the copy/scale/glass approach I had to
take for creating my drag &amp; drop “thumbnails” of controls.  I sat down to
see how much work it would take to create an IDragTypeCreator implementation using
WritableBitmap.  WriteableBitmap still has some bizarre behavior in some situations
so unfortunately I had to stick with the old implementation.
</p>
        <h2>Refactoring
</h2>
        <p>
Pretty much any time I look at old code, I’m eager to apply whatever I’ve since learned
to it.  AGT is no exception.  In order to get my head back into this codebase
I needed to come up with a refactoring goal that would cause me to review everything…
</p>
        <h2>Moving to Unity
</h2>
        <p>
Now that Unity for Silverlight is in good shape, there’s no reason for me not to use
it.  I have now taken a dependency on Unity 1.2.  It was hard to say goodbye
to my own IoC container but it needed to be done.  Were there any issues in this
upgrade?  Oh, you might say so…
</p>
        <ol>
          <li>
            <font face="Verdana">
              <p>
                <font face="Verdana">I removed my IService interface and replaced with Unity [Dependency]
attributes.  This of course involved touching nearly every file in the project. 
Find an IService interface, find the implementing classes and the importing classes;
lather, rinse repeat.  This was super-painful.</font>
              </p>
            </font>
          </li>
          <li>
            <p>
              <font face="Verdana">I decided to keep my own EventAggregator for now.</font>
            </p>
          </li>
          <li>
            <p>
I am also keeping my lame Region Manager for now
</p>
          </li>
          <li>
            <p>
              <font face="Verdana">The old Creative Commons declarations were removed since AGT
is really MS-PL now.</font>
            </p>
          </li>
        </ol>
        <p>
While this resulted in a massive change set, it felt good.  This was a very eye-opening
experience: 
</p>
        <p>
          <font color="#ff0000" size="4">Changing your IoC container might mean touching most
of the files in your solution!</font>
        </p>
        <h2>Design Surface
</h2>
        <p>
Once I got the solution back up and running with Unity and Silverlight 3, I started
looking at areas where I could clean up the design.  The DesignSurface implementation
is only 300 lines right now, but is also likely to grow in the future.  I decided
to use a <a href="http://www.silverlightshow.net/items/Build-an-Image-HotSpot-Designer-in-Silverlight.aspx">concept
from another article</a> to encapsulate drawing on the design surface.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>DamonPayne.AGT.Design.Behaviors.Drawing
{ <span style="color: blue">public abstract class </span><span style="color: #2b91af">DrawingBehavior </span>{ <span style="color: blue">public </span>DrawingBehavior(<span style="color: #2b91af">Canvas </span>surface)
{ Surface = surface; } <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">The
surface we're drawing on </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: blue">public </span><span style="color: #2b91af">Canvas </span>Surface
{ <span style="color: blue">get</span>; <span style="color: blue">private set</span>;
} <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Mouse
click point relative to Surface </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: blue">public </span><span style="color: #2b91af">Point </span>StartPoint
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: blue">public
abstract void </span>StartDrawing(<span style="color: #2b91af">Point </span>s); <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Give derrived classes a chance
to determine if they can or should return a valid </span><span style="color: gray">/// </span><span style="color: green">Shape
in their current state </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;param
name="g"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; </span><span style="color: blue">public
virtual bool </span>ShouldStopDrawing(<span style="color: #2b91af">DrawingGestures </span>g)
{ <span style="color: blue">return </span>(g == <span style="color: #2b91af">DrawingGestures</span>.MouseLeftButtonUp);
} <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Stop
drawing and return the final Shape </span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;returns&gt;&lt;/returns&gt; </span><span style="color: blue">public abstract </span><span style="color: #2b91af">Shape </span>StopDrawing(); <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Pass Mouse events through to
the DrawingBehavior </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;param
name="e"&gt;&lt;/param&gt; </span><span style="color: blue">public abstract
void </span>MouseMove(<span style="color: #2b91af">MouseEventArgs </span>e); } }</pre>
        <p>
If you are a long-time follower of this series or you’ve read the road map on Codeplex,
you’ll know that I intend to have an extensible behavior mechanism for making things
happen on the design surface.  DrawingBehvior is the first step in that direction,
and also allows me to take a lot of code out of the design surface and move it to
a dedicated construct.
</p>
        <pre style="width: 680px; height: 1017px" class="code">
          <span style="color: blue">namespace </span>DamonPayne.AGT.Design.Behaviors.Drawing
{ <span style="color: blue">public class </span><span style="color: #2b91af">RectangleDrawingBehavior </span>: <span style="color: #2b91af">DrawingBehavior </span>{ <span style="color: blue">public
static readonly double </span>SELECT_RECT_STROKE_WIDTH = 3.0; <span style="color: blue">public
static readonly double </span>SELECT_RECT_RADIUS_X = 0.0; <span style="color: blue">public
static readonly double </span>SELECT_RECT_RADIUS_Y = 0.0; <span style="color: blue">public </span>RectangleDrawingBehavior(<span style="color: #2b91af">Canvas </span>surface)
: <span style="color: blue">base</span>(surface) { } <span style="color: blue">public </span><span style="color: #2b91af">Rectangle </span>DrawingRect
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: blue">public
override </span><span style="color: #2b91af">Shape </span>StartDrawing(<span style="color: #2b91af">Point </span>s)
{ StartPoint = s; DrawingRect = <span style="color: blue">new </span><span style="color: #2b91af">Rectangle </span>{
Width = 5.0, Height = 5.0, }; ApplyShapeStyle(DrawingRect); DrawingRect.SetValue(<span style="color: #2b91af">Canvas</span>.LeftProperty,
s.X); DrawingRect.SetValue(<span style="color: #2b91af">Canvas</span>.TopProperty,
s.Y); Surface.Children.Add(DrawingRect); <span style="color: blue">return </span>DrawingRect;
} <span style="color: blue">public override </span><span style="color: #2b91af">Shape </span>StopDrawing()
{ <span style="color: blue">var </span>rVal = DrawingRect; DrawingRect = <span style="color: blue">null</span>; <span style="color: blue">return </span>rVal;
} <span style="color: blue">public override void </span>MouseMove(<span style="color: #2b91af">MouseEventArgs </span>e)
{ <span style="color: #2b91af">Point </span>localMousePos = e.GetPosition(Surface); <span style="color: blue">if </span>(StartPoint.X
&lt; localMousePos.X) { <span style="color: blue">double </span>width = localMousePos.X
- StartPoint.X; <span style="color: blue">double </span>height = localMousePos.Y -
StartPoint.Y; <span style="color: blue">if </span>(width &gt; 0 &amp;&amp; height
&gt; 0) { DrawingRect.Width = width; DrawingRect.Height = height; } } <span style="color: blue">else </span><span style="color: green">//
northwest drag </span>{ <span style="color: blue">double </span>width = StartPoint.X
- localMousePos.X; <span style="color: blue">double </span>height = StartPoint.Y -
localMousePos.Y; <span style="color: blue">if </span>(width &gt; 0 &amp;&amp; height
&gt; 0)<span style="color: green">//need this safety here in case a resize rectangle
"crosses" itself. </span>{ DrawingRect.Width = width; DrawingRect.Height
= height; DrawingRect.SetValue(<span style="color: #2b91af">Canvas</span>.LeftProperty,
localMousePos.X); DrawingRect.SetValue(<span style="color: #2b91af">Canvas</span>.TopProperty,
localMousePos.Y); } } } <span style="color: blue">public virtual void </span>ApplyShapeStyle(<span style="color: #2b91af">Shape </span>s)
{ DrawingRect.StrokeThickness = SELECT_RECT_STROKE_WIDTH; DrawingRect.RadiusX = SELECT_RECT_RADIUS_X;
DrawingRect.RadiusY = SELECT_RECT_RADIUS_Y; DrawingRect.Stroke = <span style="color: blue">new </span><span style="color: #2b91af">SolidColorBrush</span>(<span style="color: #2b91af">Color</span>.FromArgb(0xFF,
0x15, 0x05, 0xFF)); DrawingRect.Fill = <span style="color: blue">new </span><span style="color: #2b91af">SolidColorBrush</span>(<span style="color: #2b91af">Color</span>.FromArgb(0xFF,
0x49, 0x59, 0xFF)); DrawingRect.Opacity = .50; DrawingRect.SetValue(<span style="color: #2b91af">Canvas</span>.ZIndexProperty,
1000); } } }</pre>
        <p>
Now the design surface code is a bit smaller.  The design surface is still assuming
the selecting shape is a rectangle but I can fix that in the future.  I will
probably also make the selection strategy pluggable at some point.  
</p>
        <h1>Conclusion
</h1>
        <p>
Since there are no functional changes I have not updated the live demo.  You
can get the most recent code <a href="http://agt.codeplex.com/SourceControl/ListDownloadableCommits.aspx">from
codeplex</a>.  The final change set as of the end of this article is 28467. 
The next AGT few articles will bring more interesting features, and yes I will get
back to the Undo framework eventually.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=8aadfbff-bb97-4f79-85e5-7f60763e5a9e" />
      </body>
      <title>Argentum Tela Design Surface [20] &amp;ndash; Getting up to speed</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,8aadfbff-bb97-4f79-85e5-7f60763e5a9e.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/yuUsCJO--Ro/ArgentumTelaDesignSurface20NdashGettingUpToSpeed.aspx</link>
      <pubDate>Fri, 25 Sep 2009 02:00:22 GMT</pubDate>
      <description>&lt;p&gt;
&lt;i&gt;The AGT (Argentum Tela) series of articles is an effort to do two things. Usually
an idea is presented only in its finished form. The first goal is to do some Reality
Blogging, to show an idea evolve over time without pulling any punches. The second
goal, and the example vehicle for the evolution aspect, is an extensible Design Surface
for Silverlight similar to what we have in Visual Studio 2008. This type of application
has all sorts of interesting uses. My example is a Home Theater layout tool. Read
the entire saga: &lt;/i&gt;&lt;a href="http://www.damonpayne.com/ct.ashx?id=63976c45-3b0f-422e-8a49-df2e73b26b2c&amp;amp;url=http%3a%2f%2fwww.damonpayne.com%2f2008%2f09%2f14%2fRunTimeIsDesignTimeForAGT0.aspx"&gt;&lt;i&gt;http://www.damonpayne.com/2008/09/14/RunTimeIsDesignTimeForAGT0.aspx&lt;/i&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h2&gt;Atrophy
&lt;/h2&gt;
&lt;p&gt;
As of &lt;a href="http://www.damonpayne.com/2009/01/12/ArgentumTelaDesignSurface19.aspx"&gt;AGT[19]&lt;/a&gt; I
was getting ready to extend Commanding and introduce some other concepts in order
to get Undo/Redo support.&amp;#160; Of course, that was many months ago and I’ve let this
sit for a while haven’t I?&amp;#160; Since that time a lot has happened.&amp;#160; Silverlight
3 has shipped, there are many more class libraries available, and Silverlight in general
is being taken more seriously.&amp;#160; It’s time to continue working on the design surface
project.
&lt;/p&gt;
&lt;p&gt;
Ok, fine, I also need AGT for some other projects.&amp;#160; I need to take some time
to bring the project up to the state of the art and change direction slightly to move
the road map around to be more in line with my own short term needs.&amp;#160; Let’s get
started.
&lt;/p&gt;
&lt;p&gt;
If you’re new to this project, you can also check out the &lt;a href="http://agt.codeplex.com/Wiki/View.aspx?title=Home"&gt;Codeplex
site&lt;/a&gt;.
&lt;/p&gt;
&lt;h2&gt;Silverlight 3 Upgrade
&lt;/h2&gt;
&lt;p&gt;
Since I’ve been through a machine repave or two since my last work I had to get Team
System set back up for Codeplex.&amp;#160; I didn’t realize this until after I’d opened
the solution and done the conversion wizard so I was concerned.&amp;#160; Sure enough,
the project upgrade hozed the source control bindings but I was able to get it fixed
using the Change Source Control command.&amp;#160; With this done I built and ran with
no problems, however trying to change anything revealed that source control was most
likely irrevocably ruined so I got latest and went back to work.
&lt;/p&gt;
&lt;p&gt;
Once I got through all this, the Silverlight 3 upgrade went off without a hitch.&amp;#160;
I found exactly zero breaking changes in the application so far; this is much nicer
than some of the previous Silverlight 2 cycles I had gone through.
&lt;/p&gt;
&lt;p&gt;
While no changes were strictly necessary, I had to think about taking advantage of
new Silverlight 3 features.&amp;#160; I quickly cycled through my mental list of new Silverlight
3 features.&amp;#160; I may use things like Behaviors in the future.&amp;#160; One thing I
wasn’t thrilled with from Silverlight 2 was the copy/scale/glass approach I had to
take for creating my drag &amp;amp; drop “thumbnails” of controls.&amp;#160; I sat down to
see how much work it would take to create an IDragTypeCreator implementation using
WritableBitmap.&amp;#160; WriteableBitmap still has some bizarre behavior in some situations
so unfortunately I had to stick with the old implementation.
&lt;/p&gt;
&lt;h2&gt;Refactoring
&lt;/h2&gt;
&lt;p&gt;
Pretty much any time I look at old code, I’m eager to apply whatever I’ve since learned
to it.&amp;#160; AGT is no exception.&amp;#160; In order to get my head back into this codebase
I needed to come up with a refactoring goal that would cause me to review everything…
&lt;/p&gt;
&lt;h2&gt;Moving to Unity
&lt;/h2&gt;
&lt;p&gt;
Now that Unity for Silverlight is in good shape, there’s no reason for me not to use
it.&amp;#160; I have now taken a dependency on Unity 1.2.&amp;#160; It was hard to say goodbye
to my own IoC container but it needed to be done.&amp;#160; Were there any issues in this
upgrade?&amp;#160; Oh, you might say so…
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt; 
&lt;p&gt;
&lt;font face="Verdana"&gt;I removed my IService interface and replaced with Unity [Dependency]
attributes.&amp;#160; This of course involved touching nearly every file in the project.&amp;#160;
Find an IService interface, find the implementing classes and the importing classes;
lather, rinse repeat.&amp;#160; This was super-painful.&lt;/font&gt; 
&lt;/p&gt;
&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
&lt;font face="Verdana"&gt;I decided to keep my own EventAggregator for now.&lt;/font&gt; 
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
I am also keeping my lame Region Manager for now
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;
&lt;font face="Verdana"&gt;The old Creative Commons declarations were removed since AGT
is really MS-PL now.&lt;/font&gt; 
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
While this resulted in a massive change set, it felt good.&amp;#160; This was a very eye-opening
experience: 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#ff0000" size="4"&gt;Changing your IoC container might mean touching most
of the files in your solution!&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;Design Surface
&lt;/h2&gt;
&lt;p&gt;
Once I got the solution back up and running with Unity and Silverlight 3, I started
looking at areas where I could clean up the design.&amp;#160; The DesignSurface implementation
is only 300 lines right now, but is also likely to grow in the future.&amp;#160; I decided
to use a &lt;a href="http://www.silverlightshow.net/items/Build-an-Image-HotSpot-Designer-in-Silverlight.aspx"&gt;concept
from another article&lt;/a&gt; to encapsulate drawing on the design surface.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;DamonPayne.AGT.Design.Behaviors.Drawing
{ &lt;span style="color: blue"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DrawingBehavior &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;DrawingBehavior(&lt;span style="color: #2b91af"&gt;Canvas &lt;/span&gt;surface)
{ Surface = surface; } &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;The
surface we're drawing on &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Canvas &lt;/span&gt;Surface
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;;
} &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Mouse
click point relative to Surface &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point &lt;/span&gt;StartPoint
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: blue"&gt;public
abstract void &lt;/span&gt;StartDrawing(&lt;span style="color: #2b91af"&gt;Point &lt;/span&gt;s); &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Give derrived classes a chance
to determine if they can or should return a valid &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Shape
in their current state &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;param
name=&amp;quot;g&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public
virtual bool &lt;/span&gt;ShouldStopDrawing(&lt;span style="color: #2b91af"&gt;DrawingGestures &lt;/span&gt;g)
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(g == &lt;span style="color: #2b91af"&gt;DrawingGestures&lt;/span&gt;.MouseLeftButtonUp);
} &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Stop
drawing and return the final Shape &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public abstract &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Shape &lt;/span&gt;StopDrawing(); &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Pass Mouse events through to
the DrawingBehavior &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;param
name=&amp;quot;e&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public abstract
void &lt;/span&gt;MouseMove(&lt;span style="color: #2b91af"&gt;MouseEventArgs &lt;/span&gt;e); } }&lt;/pre&gt;
&lt;p&gt;
If you are a long-time follower of this series or you’ve read the road map on Codeplex,
you’ll know that I intend to have an extensible behavior mechanism for making things
happen on the design surface.&amp;#160; DrawingBehvior is the first step in that direction,
and also allows me to take a lot of code out of the design surface and move it to
a dedicated construct.
&lt;/p&gt;
&lt;pre style="width: 680px; height: 1017px" class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;DamonPayne.AGT.Design.Behaviors.Drawing
{ &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RectangleDrawingBehavior &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DrawingBehavior &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
static readonly double &lt;/span&gt;SELECT_RECT_STROKE_WIDTH = 3.0; &lt;span style="color: blue"&gt;public
static readonly double &lt;/span&gt;SELECT_RECT_RADIUS_X = 0.0; &lt;span style="color: blue"&gt;public
static readonly double &lt;/span&gt;SELECT_RECT_RADIUS_Y = 0.0; &lt;span style="color: blue"&gt;public &lt;/span&gt;RectangleDrawingBehavior(&lt;span style="color: #2b91af"&gt;Canvas &lt;/span&gt;surface)
: &lt;span style="color: blue"&gt;base&lt;/span&gt;(surface) { } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Rectangle &lt;/span&gt;DrawingRect
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: blue"&gt;public
override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Shape &lt;/span&gt;StartDrawing(&lt;span style="color: #2b91af"&gt;Point &lt;/span&gt;s)
{ StartPoint = s; DrawingRect = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Rectangle &lt;/span&gt;{
Width = 5.0, Height = 5.0, }; ApplyShapeStyle(DrawingRect); DrawingRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty,
s.X); DrawingRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty,
s.Y); Surface.Children.Add(DrawingRect); &lt;span style="color: blue"&gt;return &lt;/span&gt;DrawingRect;
} &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Shape &lt;/span&gt;StopDrawing()
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;rVal = DrawingRect; DrawingRect = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;return &lt;/span&gt;rVal;
} &lt;span style="color: blue"&gt;public override void &lt;/span&gt;MouseMove(&lt;span style="color: #2b91af"&gt;MouseEventArgs &lt;/span&gt;e)
{ &lt;span style="color: #2b91af"&gt;Point &lt;/span&gt;localMousePos = e.GetPosition(Surface); &lt;span style="color: blue"&gt;if &lt;/span&gt;(StartPoint.X
&amp;lt; localMousePos.X) { &lt;span style="color: blue"&gt;double &lt;/span&gt;width = localMousePos.X
- StartPoint.X; &lt;span style="color: blue"&gt;double &lt;/span&gt;height = localMousePos.Y -
StartPoint.Y; &lt;span style="color: blue"&gt;if &lt;/span&gt;(width &amp;gt; 0 &amp;amp;&amp;amp; height
&amp;gt; 0) { DrawingRect.Width = width; DrawingRect.Height = height; } } &lt;span style="color: blue"&gt;else &lt;/span&gt;&lt;span style="color: green"&gt;//
northwest drag &lt;/span&gt;{ &lt;span style="color: blue"&gt;double &lt;/span&gt;width = StartPoint.X
- localMousePos.X; &lt;span style="color: blue"&gt;double &lt;/span&gt;height = StartPoint.Y -
localMousePos.Y; &lt;span style="color: blue"&gt;if &lt;/span&gt;(width &amp;gt; 0 &amp;amp;&amp;amp; height
&amp;gt; 0)&lt;span style="color: green"&gt;//need this safety here in case a resize rectangle
&amp;quot;crosses&amp;quot; itself. &lt;/span&gt;{ DrawingRect.Width = width; DrawingRect.Height
= height; DrawingRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.LeftProperty,
localMousePos.X); DrawingRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.TopProperty,
localMousePos.Y); } } } &lt;span style="color: blue"&gt;public virtual void &lt;/span&gt;ApplyShapeStyle(&lt;span style="color: #2b91af"&gt;Shape &lt;/span&gt;s)
{ DrawingRect.StrokeThickness = SELECT_RECT_STROKE_WIDTH; DrawingRect.RadiusX = SELECT_RECT_RADIUS_X;
DrawingRect.RadiusY = SELECT_RECT_RADIUS_Y; DrawingRect.Stroke = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SolidColorBrush&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Color&lt;/span&gt;.FromArgb(0xFF,
0x15, 0x05, 0xFF)); DrawingRect.Fill = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SolidColorBrush&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Color&lt;/span&gt;.FromArgb(0xFF,
0x49, 0x59, 0xFF)); DrawingRect.Opacity = .50; DrawingRect.SetValue(&lt;span style="color: #2b91af"&gt;Canvas&lt;/span&gt;.ZIndexProperty,
1000); } } }&lt;/pre&gt;
&lt;p&gt;
Now the design surface code is a bit smaller.&amp;#160; The design surface is still assuming
the selecting shape is a rectangle but I can fix that in the future.&amp;#160; I will
probably also make the selection strategy pluggable at some point.&amp;#160; 
&lt;/p&gt;
&lt;h1&gt;Conclusion
&lt;/h1&gt;
&lt;p&gt;
Since there are no functional changes I have not updated the live demo.&amp;#160; You
can get the most recent code &lt;a href="http://agt.codeplex.com/SourceControl/ListDownloadableCommits.aspx"&gt;from
codeplex&lt;/a&gt;.&amp;#160; The final change set as of the end of this article is 28467.&amp;#160;
The next AGT few articles will bring more interesting features, and yes I will get
back to the Undo framework eventually.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=8aadfbff-bb97-4f79-85e5-7f60763e5a9e" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,8aadfbff-bb97-4f79-85e5-7f60763e5a9e.aspx</comments>
      <category>Architecture and Design</category>
      <category>Designer Support</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/09/25/ArgentumTelaDesignSurface20NdashGettingUpToSpeed.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=9e18c424-9630-4185-bd5f-ff5340fd4882</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,9e18c424-9630-4185-bd5f-ff5340fd4882.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,9e18c424-9630-4185-bd5f-ff5340fd4882.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9e18c424-9630-4185-bd5f-ff5340fd4882</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://www.damonpayne.com/2009/09/17/ViewBasedNavigationInSilverlight3.aspx">last
article on View-based navigation</a>, I mentioned that a useful Navigation History
module could be built to work in concert.  This article discusses the design
and implementation of that module. In this case my design goals are more straightforward:
</p>
        <ol>
          <li>
Allow the size of the navigation history to be set so as to control memory usage 
</li>
          <li>
Allow going forwards and backwards in the history 
</li>
          <li>
Allow inspection of the navigation history 
</li>
          <li>
Allow aspects of navigation history to be bindable 
</li>
        </ol>
        <h1>Navigation History
</h1>
        <p>
As with the core navigation concepts I went over previously, there is some basic Navigation
History built into the Frame class in Silverlight 3.  Since I’ve extended the
core navigation concepts I can also add additional hooks for navigation.  As
usual, I will first think of my design goals and craft an interface.
</p>
        <pre class="code">
          <span style="color: blue">public interface </span>
          <span style="color: #2b91af">INavigationHistory </span>: <span style="color: #2b91af">INotifyPropertyChanged </span>{ <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">How many Views do we want to
keep around? </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: blue">int </span>HistorySize
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Go back one View, return the
next active view </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: #2b91af">IView </span>GoBack(); <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Go forward one view, return
the next active view </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; </span><span style="color: #2b91af">IView </span>GoForward(); <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Can we go back based on current
position? </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: blue">bool </span>CanGoBack{<span style="color: blue">get</span>;} <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Can we go forward based on current
position </span><span style="color: gray">/// &lt;/summary&gt; </span><span style="color: blue">bool </span>CanGoForward{<span style="color: blue">get</span>;} <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Called by clients to add a view
to history </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;param name="v"&gt;&lt;/param&gt; </span><span style="color: blue">void </span>Navigated(<span style="color: #2b91af">IView </span>v); <span style="color: blue">event </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IView</span>&gt;
NavigatedToView; <span style="color: blue">event </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IView</span>&gt;
HistoryItemRemoved; <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Get
the items in the current history, allow a jump to a specific point </span><span style="color: gray">///
&lt;/summary&gt; </span><span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">IView</span>&gt;
CurrentHistory { <span style="color: blue">get</span>; } <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Go to a specific View in the
history </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;param name="historyIndex"&gt;&lt;/param&gt; </span><span style="color: blue">void </span>GoTo(<span style="color: #2b91af">IView </span>v); <span style="color: gray">///
&lt;summary&gt; /// </span><span style="color: green">Removes the most recent item </span><span style="color: gray">///
&lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; </span><span style="color: #2b91af">IView </span>Pop();
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Including INotifyPropertyChanged here means aspects of the navigation history will
be bindable: CanGoBack, CanGoForward, and CurrentHistory.  The other parts of
the interface should be self explanatory.
</p>
        <h2>Binding to History
</h2>
        <p>
Using the previous Audi Fans demo, I can now make the navigation buttons in the lower
right hand corner work.
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/HistoryforViewBasedNavigationinSilverlig_1149F/navhist0_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navhist0" border="0" alt="navhist0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/HistoryforViewBasedNavigationinSilverlig_1149F/navhist0_thumb.jpg" width="483" height="146" />
          </a>
        </p>
        <p>
The XAML for creating these buttons is fairly simple, note the {Binding} aspects:
</p>
        <pre style="width: 680px; height: 233px" class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">StackPanel </span>
          <span style="color: red">Orientation</span>
          <span style="color: blue">="Horizontal" </span>
          <span style="color: red">Grid.Row</span>
          <span style="color: blue">="2" </span>
          <span style="color: red">Margin</span>
          <span style="color: blue">="5" </span>
          <span style="color: red">HorizontalAlignment</span>
          <span style="color: blue">="Right"&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Style</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">StaticResource </span>
          <span style="color: red">TextStyle</span>
          <span style="color: blue">}"
&gt;</span>
          <span style="color: #a31515">Navigation: </span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="GoBack" </span>
          <span style="color: red">IsEnabled</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Path</span>
          <span style="color: blue">=History.CanGoBack}" </span>
          <span style="color: red">Click</span>
          <span style="color: blue">="GoBack_Click"&gt;
&lt;</span>
          <span style="color: #a31515">Button.Content</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Style</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">StaticResource </span>
          <span style="color: red">TextStyle</span>
          <span style="color: blue">}" </span>
          <span style="color: red">Foreground</span>
          <span style="color: blue">="Black"&gt;</span>
          <span style="color: #a31515">&amp;lt;
Back</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Button.Content</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Button</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">ComboBox </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="JumpCmb" </span>
          <span style="color: red">DataContext</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding</span>
          <span style="color: blue">}" </span>
          <span style="color: red">ItemsSource</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Path</span>
          <span style="color: blue">=HistoryItems}" </span>
          <span style="color: red">SelectedItem</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Path</span>
          <span style="color: blue">=CurrentViewName, </span>
          <span style="color: red">Mode</span>
          <span style="color: blue">=TwoWay}" </span>
          <span style="color: red">Margin</span>
          <span style="color: blue">="10,0,10,0" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="250"
&gt; &lt;/</span>
          <span style="color: #a31515">ComboBox</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="GoForward" </span>
          <span style="color: red">IsEnabled</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Path</span>
          <span style="color: blue">=History.CanGoForward}" </span>
          <span style="color: red">Click</span>
          <span style="color: blue">="GoForward_Click"&gt;
&lt;</span>
          <span style="color: #a31515">Button.Content</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Style</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">StaticResource </span>
          <span style="color: red">TextStyle</span>
          <span style="color: blue">}" </span>
          <span style="color: red">Foreground</span>
          <span style="color: blue">="Black"&gt;</span>
          <span style="color: #a31515">Forward
&amp;gt;</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Button.Content</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Button</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
In order to satisfy these bindings, the Navigation History implementation has been
added to the MainViewModel for the front page.  You will note that I’m not binding
directly to the History.CurrentHistory but that some additional fields have been added
to MainViewModel:
</p>
        <pre class="code">
          <span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: blue">string</span>&gt;
_histItems; <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Silverlight
crashes if you use a Page (IView) as a binding source. Lame. </span><span style="color: gray">///
&lt;/summary&gt; </span><span style="color: blue">public </span><span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: blue">string</span>&gt;
HistoryItems { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_histItems;
} } <span style="color: blue">string </span>_currentViewName; <span style="color: blue">public
string </span>CurrentViewName { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_currentViewName;
} <span style="color: blue">set </span>{ <span style="color: blue">if </span>(<span style="color: blue">value </span>!=
_currentViewName) { _currentViewName = <span style="color: blue">value</span>; OnPropertyChanged(<span style="color: #a31515">"CurrentViewName"</span>); <span style="color: blue">if </span>(_viewMap.ContainsKey(<span style="color: blue">value</span>))
{ Navigation.GoTo(_viewMap[<span style="color: blue">value</span>]); } } } }</pre>
        <p>
          <font color="#ff0000">Pit of failure</font>: Silverlight does not seem to like having
a Page object as a binding source except when doing Element binding.  This may
be a low level implementation detail that’s just not supported, or it may be because
the Pages in question are not visible and this causes some fundamental failure.
</p>
        <p>
        </p>
        <p>
So there is a layer that maps IView names back to the INavigationHistory because Silverlight
binding doesn’t like binding to a visual element that is not currently visible.
</p>
        <h2>Conclusion
</h2>
        <p>
That’s pretty much it for Navigation History, be sure to read the <a href="http://www.damonpayne.com/2009/09/17/ViewBasedNavigationInSilverlight3.aspx">previous
article</a> if you’d like to see how it fits in with everything else.  There
are other directions you could take this, obviously.  If you are using some kind
of Region manager you could scope the navigation to each region.  If you are
concerned about memory usage, you could create a scheme to use WeakReferences and
provide hooks to save <em>just</em> the ViewModel of an IView so that the IView itself
could be reconstructed later.  
</p>
        <p>
The updated source code for the HandWaver.AG guidance projects can be found <a href="http://fwww.damonpayne.com/HandWaver.AG/HandWaverAG.zip">here</a>.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=9e18c424-9630-4185-bd5f-ff5340fd4882" />
      </body>
      <title>History for View Based Navigation in Silverlight 3</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,9e18c424-9630-4185-bd5f-ff5340fd4882.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/v6_8kUd0EPk/HistoryForViewBasedNavigationInSilverlight3.aspx</link>
      <pubDate>Tue, 22 Sep 2009 00:17:04 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://www.damonpayne.com/2009/09/17/ViewBasedNavigationInSilverlight3.aspx"&gt;last
article on View-based navigation&lt;/a&gt;, I mentioned that a useful Navigation History
module could be built to work in concert.&amp;#160; This article discusses the design
and implementation of that module. In this case my design goals are more straightforward:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Allow the size of the navigation history to be set so as to control memory usage 
&lt;/li&gt;
&lt;li&gt;
Allow going forwards and backwards in the history 
&lt;/li&gt;
&lt;li&gt;
Allow inspection of the navigation history 
&lt;/li&gt;
&lt;li&gt;
Allow aspects of navigation history to be bindable 
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Navigation History
&lt;/h1&gt;
&lt;p&gt;
As with the core navigation concepts I went over previously, there is some basic Navigation
History built into the Frame class in Silverlight 3.&amp;#160; Since I’ve extended the
core navigation concepts I can also add additional hooks for navigation.&amp;#160; As
usual, I will first think of my design goals and craft an interface.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;INavigationHistory &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged &lt;/span&gt;{ &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;How many Views do we want to
keep around? &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;HistorySize
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Go back one View, return the
next active view &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;GoBack(); &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Go forward one view, return
the next active view &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;GoForward(); &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Can we go back based on current
position? &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;CanGoBack{&lt;span style="color: blue"&gt;get&lt;/span&gt;;} &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Can we go forward based on current
position &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;CanGoForward{&lt;span style="color: blue"&gt;get&lt;/span&gt;;} &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Called by clients to add a view
to history &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;param name=&amp;quot;v&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;Navigated(&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;v); &lt;span style="color: blue"&gt;event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;&amp;gt;
NavigatedToView; &lt;span style="color: blue"&gt;event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;&amp;gt;
HistoryItemRemoved; &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Get
the items in the current history, allow a jump to a specific point &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;&amp;gt;
CurrentHistory { &lt;span style="color: blue"&gt;get&lt;/span&gt;; } &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Go to a specific View in the
history &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;param name=&amp;quot;historyIndex&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;GoTo(&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;v); &lt;span style="color: gray"&gt;///
&amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Removes the most recent item &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;Pop();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Including INotifyPropertyChanged here means aspects of the navigation history will
be bindable: CanGoBack, CanGoForward, and CurrentHistory.&amp;#160; The other parts of
the interface should be self explanatory.
&lt;/p&gt;
&lt;h2&gt;Binding to History
&lt;/h2&gt;
&lt;p&gt;
Using the previous Audi Fans demo, I can now make the navigation buttons in the lower
right hand corner work.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/HistoryforViewBasedNavigationinSilverlig_1149F/navhist0_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navhist0" border="0" alt="navhist0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/HistoryforViewBasedNavigationinSilverlig_1149F/navhist0_thumb.jpg" width="483" height="146" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The XAML for creating these buttons is fairly simple, note the {Binding} aspects:
&lt;/p&gt;
&lt;pre style="width: 680px; height: 233px" class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Horizontal&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;2&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;5&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;HorizontalAlignment&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Right&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;TextStyle&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;
&amp;gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Navigation: &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;GoBack&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;IsEnabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=History.CanGoBack}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Click&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;GoBack_Click&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button.Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;TextStyle&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Foreground&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Black&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;amp;lt;
Back&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button.Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ComboBox &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;JumpCmb&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;DataContext&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;ItemsSource&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=HistoryItems}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;SelectedItem&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=CurrentViewName, &lt;/span&gt;&lt;span style="color: red"&gt;Mode&lt;/span&gt;&lt;span style="color: blue"&gt;=TwoWay}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Margin&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;10,0,10,0&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;250&amp;quot;
&amp;gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ComboBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;GoForward&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;IsEnabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Path&lt;/span&gt;&lt;span style="color: blue"&gt;=History.CanGoForward}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Click&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;GoForward_Click&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button.Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;TextStyle&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Foreground&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Black&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Forward
&amp;amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button.Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
In order to satisfy these bindings, the Navigation History implementation has been
added to the MainViewModel for the front page.&amp;#160; You will note that I’m not binding
directly to the History.CurrentHistory but that some additional fields have been added
to MainViewModel:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
_histItems; &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Silverlight
crashes if you use a Page (IView) as a binding source. Lame. &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;
HistoryItems { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_histItems;
} } &lt;span style="color: blue"&gt;string &lt;/span&gt;_currentViewName; &lt;span style="color: blue"&gt;public
string &lt;/span&gt;CurrentViewName { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_currentViewName;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;value &lt;/span&gt;!=
_currentViewName) { _currentViewName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; OnPropertyChanged(&lt;span style="color: #a31515"&gt;&amp;quot;CurrentViewName&amp;quot;&lt;/span&gt;); &lt;span style="color: blue"&gt;if &lt;/span&gt;(_viewMap.ContainsKey(&lt;span style="color: blue"&gt;value&lt;/span&gt;))
{ Navigation.GoTo(_viewMap[&lt;span style="color: blue"&gt;value&lt;/span&gt;]); } } } }&lt;/pre&gt;
&lt;p&gt;
&lt;font color="#ff0000"&gt;Pit of failure&lt;/font&gt;: Silverlight does not seem to like having
a Page object as a binding source except when doing Element binding.&amp;#160; This may
be a low level implementation detail that’s just not supported, or it may be because
the Pages in question are not visible and this causes some fundamental failure.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So there is a layer that maps IView names back to the INavigationHistory because Silverlight
binding doesn’t like binding to a visual element that is not currently visible.
&lt;/p&gt;
&lt;h2&gt;Conclusion
&lt;/h2&gt;
&lt;p&gt;
That’s pretty much it for Navigation History, be sure to read the &lt;a href="http://www.damonpayne.com/2009/09/17/ViewBasedNavigationInSilverlight3.aspx"&gt;previous
article&lt;/a&gt; if you’d like to see how it fits in with everything else.&amp;#160; There
are other directions you could take this, obviously.&amp;#160; If you are using some kind
of Region manager you could scope the navigation to each region.&amp;#160; If you are
concerned about memory usage, you could create a scheme to use WeakReferences and
provide hooks to save &lt;em&gt;just&lt;/em&gt; the ViewModel of an IView so that the IView itself
could be reconstructed later.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
The updated source code for the HandWaver.AG guidance projects can be found &lt;a href="http://fwww.damonpayne.com/HandWaver.AG/HandWaverAG.zip"&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=9e18c424-9630-4185-bd5f-ff5340fd4882" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,9e18c424-9630-4185-bd5f-ff5340fd4882.aspx</comments>
      <category>Architecture and Design</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/09/22/HistoryForViewBasedNavigationInSilverlight3.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=9ab5db77-6976-404a-b06e-87449a6ba99f</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,9ab5db77-6976-404a-b06e-87449a6ba99f.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,9ab5db77-6976-404a-b06e-87449a6ba99f.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9ab5db77-6976-404a-b06e-87449a6ba99f</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
 
</p>
        <p>
Silverlight 3 ships with a Navigation mechanism.  A control that extends Page
can be stored inside the Frame control as content, and Navigation from Page to Page
is done by specifying the URI of the target Page.  This mechanism includes navigation
history and the ability to go forwards and backwards through said history.  I
would, however, like a slightly different programming model around application navigation.
</p>
        <p>
Design goals:
</p>
        <ol>
          <li>
            <font face="Verdana">I would like to be able to navigate to a new “screen” from within
various places such as Presenters or Commands</font>
          </li>
          <li>
            <font face="Verdana">I do not want to have to configure each new View I add to the
system</font>
          </li>
          <li>
            <font face="Verdana">I don’t want to have to map Views to URIs to use the Frame</font>
          </li>
          <li>
            <font face="Verdana">I need to be able to pass parameters to Views as they are navigated
to</font>
          </li>
          <li>
            <font face="Verdana">I need to support having more than one View implementation in
the system</font> for a given View interface type 
</li>
          <li>
            <font face="Verdana">I need an inspect-able navigation history that I can also configure
in terms of how much history to keep and so forth</font>
          </li>
          <li>
            <font face="Verdana">I would like to support animated transitions from View to View,
this is Silverlight after all</font>
          </li>
        </ol>
        <p>
My sample application will be an “Audi Fan Site” in Silverlight.
</p>
        <h1>Views &amp; ViewModels
</h1>
        <p>
I have a Silverlight “guidance” Solution called HandWaver.AG; I will use some concepts
from this solution to build the navigation framework and post the entire solution’s
source code.  
</p>
        <p>
          <font color="#80ff80">Pattern: For separation of concerns, I’ll use the Model-View-ViewModel
pattern.</font>
        </p>
        <p>
I’ll start with a humble IView base interface:
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.PresenationModel
{ <span style="color: blue">public interface </span><span style="color: #2b91af">IView </span>{ <span style="color: blue">string </span>Title
{ <span style="color: blue">get</span>; } <span style="color: blue">void </span>Activate(<span style="color: #2b91af">Action </span>complete); <span style="color: blue">void </span>DeActivate(<span style="color: #2b91af">Action </span>complete);
} }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
        </p>
        <p>
Nothing complicated here.  The Activate and Deactivate methods can be called
to inform a IView that it is about to be “started” or shut down.  The Action
callbacks will facilitate animation later.
</p>
        <p>
Now I need a ViewModel base class that does a little bit of change notification work
for me.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.PresenationModel
{ <span style="color: blue">public abstract class </span><span style="color: #2b91af">ViewModel </span>: <span style="color: #2b91af">INotifyPropertyChanged </span>{ <span style="color: blue">public
event </span><span style="color: #2b91af">PropertyChangedEventHandler </span>PropertyChanged; <span style="color: blue">protected
void </span>OnPropertyChanged(<span style="color: blue">string </span>propName) { <span style="color: blue">if </span>(<span style="color: blue">null </span>!=
PropertyChanged) { PropertyChanged(<span style="color: blue">this</span>, <span style="color: blue">new </span><span style="color: #2b91af">PropertyChangedEventArgs</span>(propName));
} } } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I will expand on these concepts in a little bit, for now assume that I will have Controls
of some kind that implement IView and bind to ViewModels.
</p>
        <h1>Navigation
</h1>
        <p>
For the most simple case, I want to be able to just go to an IVIew.  I’ve created
an INavigationService interface.  This is the calling convention that I’ll use.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.PresenationModel.Navigation
{ <span style="color: blue">public interface </span><span style="color: #2b91af">INavigationService </span>{ <span style="color: gray">/// 
<summary>
/// 
</summary></span><span style="color: green">Display the first IView implementation
we find </span><span style="color: gray">/// 
/// 
<typeparam name="T" /></span><span style="color: blue">void </span>NavigateTo&lt; T &gt;() <span style="color: blue">where </span>T:<span style="color: #2b91af">IView</span>;</pre>
        <p>
At this point I can go ahead and create my sample application and my “Home” screen. 
I’m going to re-use the Frame and Page concepts from Silverlight.  I have some
navigation buttons that are disabled, a ComboBox that will contain my navigation history,
and some HyperlinkButtons up at the top to link to the various IViews I will create.
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp0_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp0" border="0" alt="navapp0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp0_thumb.jpg" width="644" height="437" />
          </a>
        </p>
        <p>
The grey area is my navigation Frame.  What I want to do now is Navigate to the
Home screen when the app is loaded.  As you can see from the INavigationService
code above, I’m just going to do this:
</p>
        <pre style="width: 680px; height: 53px" class="code">Navigation.NavigateTo<span style="color: #2b91af">IHomeView</span>&gt;();</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Adding this now displays my Home screen in the navigation frame…
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp1_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp1" border="0" alt="navapp1" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp1_thumb.jpg" width="644" height="436" />
          </a>
        </p>
        <p>
… but how did we get there?  The first item is the discovery of IViews.
</p>
        <h2>Discovering Views
</h2>
        <p>
In order to meet my design goals, I need to figure out how to find IView implementations
automatically without having to create some kind of View Registry.  In my implementation
of the INavigationService interface, reflection is the key.  It’s worth going
over this code as everything else depends on it:
</p>
        <pre class="code">
          <span style="color: blue">private void </span>MapViewTypes() { _viewMap
= <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Type</span>&gt;&gt;(); <span style="color: green">//Note:
we're making a giant assumption here that all Views are in this assembly. MEF could
help? </span><span style="color: blue">var </span>types = _viewAssembly.GetTypes(); <span style="color: blue">var </span>viewType
= <span style="color: blue">typeof</span>(<span style="color: #2b91af">IView</span>); <span style="color: blue">var </span>pageType
= <span style="color: blue">typeof</span>(<span style="color: #2b91af">Page</span>); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>t <span style="color: blue">in </span>types)
{ <span style="color: blue">if </span>(t.ImplmentsInterface(viewType) &amp;&amp; t.IsSubclassOf(pageType))
{ <span style="color: blue">var </span>viewImplType = t; <span style="color: green">//if
we have a sub-class of IView, use that instead </span><span style="color: blue">var </span>subViewType
= (<span style="color: blue">from </span>v <span style="color: blue">in </span>viewImplType.GetInterfaces() <span style="color: blue">where </span>v.InterfaceExtends(viewType) <span style="color: blue">select </span>v).FirstOrDefault(); <span style="color: blue">if </span>(<span style="color: blue">null </span>!=
subViewType) { EnsureList(subViewType); _viewMap[subViewType].Add(t); } <span style="color: blue">else </span>{ <span style="color: blue">continue</span>;
} } } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>ImplementsInterface</strong> and <strong>InterfaceExtends</strong> are extension
methods I wrote for System.Type.  When this class is created, I’m reflecting
the implementing assembly and finding all classes that implement a descended of IView
and storing them in a List.  The implementation for the most basic NavigateTo<t>
(), then, just looks like this:
</t></p>
        <pre style="width: 680px; height: 122px" class="code">
          <span style="color: blue">public
void </span>NavigateTo<t>
() <span style="color: blue">where </span>T : <span style="color: #2b91af">IView </span>{ <span style="color: blue">var </span>viewType
= _viewMap[<span style="color: blue">typeof</span>(T)][0]; <span style="color: blue">var </span>view
= (<span style="color: #2b91af">IView</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(viewType);
InitializeView(view); }
</t></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
In the most basic case, I am just using the first IView type that I find.  As
for how the navigation actually happens, I will have to expose a few more implementation
details now.
</p>
        <pre style="width: 680px; height: 234px" class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.PresenationModel.Navigation.Modules
{ [<span style="color: #2b91af">Export</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">INavigationService</span>))] <span style="color: blue">public
class </span><span style="color: #2b91af">URIFrameNavigationService </span>: <span style="color: #2b91af">INavigationService </span>{ <span style="color: blue">public </span>URIFrameNavigationService(<span style="color: blue">string </span>rootNamespace, <span style="color: #2b91af">Assembly </span>va)
{ _viewAssembly = va; MapViewTypes(); } <span style="color: blue">static </span><span style="color: #2b91af">Action </span>nullCallback
= () =&gt; { }; [<span style="color: #2b91af">Import</span>(AllowRecomposition=<span style="color: blue">true</span>)] <span style="color: blue">public </span><span style="color: #2b91af">Frame </span>TargetFrame
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Note that, for now, I am using MEF as an IoC container.  URIFrameNavigationService
uses the Export attribute to tell MEF that it implments the INavigationService contract. 
When I construct the instance I pass in some basic information and the class uses
MEF to import an instance of the <strong>Frame</strong> class from Silverlight navigation. 
So for the most basic kind of navigation, I’m instantiating a new instance of a Page
implementing an IView interface and directly setting the Content of the Frame rather
than giving it a URI.
</p>
        <h2>Transitions
</h2>
        <p>
Next, I’m going to build the “Models” screen of my Audi fan application; in this case
we’re talking about Audi cars and not the usual “Model”.  We can go ahead and
look at the implementation for InitializeView() now:
</p>
        <pre class="code">
          <span style="color: blue">void </span>InitializeView(<span style="color: #2b91af">IView </span>v)
{ <span style="color: #2b91af">Action </span>go = () =&gt; { TargetFrame.Content =
v; v.Activate(<span style="color: #2b91af">URIFrameNavigationService</span>.nullCallback);
History.Navigated(v); }; <span style="color: blue">if </span>(<span style="color: blue">null </span>!=
TargetFrame.Content &amp;&amp; TargetFrame.Content <span style="color: blue">is </span><span style="color: #2b91af">IView</span>)
{ ((<span style="color: #2b91af">IView</span>)TargetFrame.Content).DeActivate(go);
} <span style="color: blue">else </span>{ go(); } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
If the Frame is currently displaying an IView, call it’s deactivate method. 
When that method is done, the go action is called which activates the new IView. 
We also do something with a History object here which I’ll get to in a little bit. 
By using callbacks with Activate and Deactivate we’re able to incorporate animations
into the navigation mix.  Here is the Activate implementation for Models.xaml:
</p>
        <pre style="width: 680px; height: 215px" class="code">
          <span style="color: blue">public
void </span>Activate(<span style="color: #2b91af">Action </span>complete) { <span style="color: blue">var </span>trans
= (<span style="color: #2b91af">Storyboard</span>)<span style="color: #2b91af">App</span>.Current.Resources[<span style="color: #a31515">"ExpandIn"</span>];
trans.Stop(); <span style="color: #2b91af">Storyboard</span>.SetTarget(trans, LayoutRoot);
trans.Begin(); <span style="color: #2b91af">EventHandler </span>sbDone = <span style="color: blue">null</span>;
sbDone = (o, e) =&gt; { complete(); trans.Completed -= sbDone; }; trans.Completed
+= sbDone; }</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>I pull a common animation out of resources
and call the giving Action when the animation is complete, being sure to unhook event
handlers.  In my case, I’d like to use the same Enter and Exit transitions everywhere
in the application and I don’t want to keep copy &amp; pasting this code.  I
can move this code to a helper class and invoke it this way everywhere else:
</p>
        <pre style="width: 680px; height: 143px" class="code">
          <span style="color: blue">public
void </span>Activate(<span style="color: #2b91af">Action </span>complete) { <span style="color: #2b91af">Transition</span>.Animate(<span style="color: #a31515">"ExpandIn"</span>,
LayoutRoot, complete); } <span style="color: blue">public void </span>DeActivate(<span style="color: #2b91af">Action </span>complete)
{ <span style="color: #2b91af">Transition</span>.Animate(<span style="color: #a31515">"ShrinkOut"</span>,
LayoutRoot, complete); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
We could, in future refactoring, create some kind of IViewTransitionManager module
that automatically does transitions everywhere, and save the Activate and Deactivate
methods for other application specific uses.
</p>
        <p>
You can now run the sample application (link at the end of the article) to see the
transitions working from the Home screen to the Models screen.
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp2_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp2" border="0" alt="navapp2" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp2_thumb.jpg" width="644" height="452" />
          </a>
        </p>
        <p>
 
</p>
        <h2>Passing Parameters
</h2>
        <p>
The <u>View Details</u> Hyperlink buttons, above, should navigate to a special screen
showing even more  information about the chosen vehicle model.  When this
screen loads, then, the chosen Model will need to be available.  We could set
some global User State variable specifying what model of Audi was chosen but it would
be nicer to navigate to the chosen IView and directly pass parameters to it. 
What’s more, it would be ideal if this parameter passing could be strongly typed such
that we can determine the types of parameters using Intellisense.
</p>
        <p>
We have already seen how the INavigationService implementation locates and instantiates
an IView implementation.  Adding another overload the the interface implementation
meets all stated design goals.
</p>
        <pre class="code">
          <span style="color: gray">/// &lt;summary&gt; /// </span>
          <span style="color: green">Navigate
to a View, passing parameters to said view using the supplied Action </span>
          <span style="color: gray">///
&lt;/summary&gt; /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt; /// &lt;param
name="paramSetter"&gt;&lt;/param&gt; </span>
          <span style="color: blue">void </span>NavigateTo&lt;T&gt;(<span style="color: #2b91af">Action</span>&lt;T&gt;
paramSetter) <span style="color: blue">where </span>T : <span style="color: #2b91af">IView</span>;</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
It may not be apparent at first look what this allows us to do, but consider the current
example of choosing a vehicle model from a list.  The ViewModel of the screen
we are coming from knows what the selected VehicleModel is.  Using the excellent
Generics implementation in C# we can navigate to an IView and easily see what parameters
we might be able to pass to it via Intellisense.
</p>
        <pre style="width: 680px; height: 117px" class="code">
          <span style="color: blue">protected
void </span>ViewDetailsClick(<span style="color: blue">object </span>s, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: #2b91af">VehicleModel </span>mdl = <span style="color: blue">null</span>; <span style="color: blue">var </span>lnk
= (<span style="color: #2b91af">HyperlinkButton</span>)s; mdl = (<span style="color: #2b91af">VehicleModel</span>)lnk.Tag;
Navigation.NavigateTo&lt; <span style="color: #2b91af">IModelDetailView</span>&gt;((v)
=&gt; v.TargetModel = mdl); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I am cheating a little bit here, at least it feels like cheating.  My list of
vehicle models is implemented as a listbox and I am using (abusing?) the Tag property
that Controls expose in Silverlight:
</p>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre style="width: 680px; height: 50px" class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">HyperlinkButton </span>
          <span style="color: red">Grid.Column</span>
          <span style="color: blue">="5" </span>
          <span style="color: red">Style</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">StaticResource </span>
          <span style="color: red">MenuLinkStyle</span>
          <span style="color: blue">}" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="View
Details" </span>
          <span style="color: red">VerticalAlignment</span>
          <span style="color: blue">="Center" </span>
          <span style="color: red">Click</span>
          <span style="color: blue">="ViewDetailsClick" </span>
          <span style="color: red">Tag</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding</span>
          <span style="color: blue">}"&gt;
&lt;/&lt; span&gt;<span style="color: #a31515">HyperlinkButton</span><span style="color: blue">&gt;</span></span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <font color="#808080">Since we can use Bindings for Tag, the actual data type for 
each VehicleModel is stored with the Hyperlinkbutton and we can pull it in the Click
event handler.  The real power of what we are now able to do is demonstrated
when we see the intellisense available when navigating to IModelDetailView and exploring
the overloads available:</font>
        </p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp4_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp4" border="0" alt="navapp4" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp4_thumb.jpg" width="670" height="148" />
          </a>
        </p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp3_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp3" border="0" alt="navapp3" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp3_thumb.jpg" width="500" height="293" />
          </a>
        </p>
        <p>
          <font color="#c0c0c0">Since we have a strongly-typed mechanism, we can create a lambda
expression that takes the IView instance and we can inspect the list of public properties
available on IModelDetailView.  In my opinion this makes for a perfectly discoverable
mechanism for passing parameters directly to an IView.  If you need further help,
you could create a class something like RequiredViewParameter<t>
;  having such a type in the Intellisene dropdown might help clients determine
which properties <em>must</em> be set in order for a View to function.  Obviously,
we an set as many properties as we want using the lambda expression.
</t></font>
        </p>
        <p>
 
</p>
        <h2>
          <font color="#c0c0c0">Choosing Between Multiple IView Implementations </font>
        </h2>
        <p>
          <font color="#c0c0c0">Often, there may be only one implementation for a given IView
interface at run time and compile time.  For example, there may likely be only
a RealFooView (the real production visual) and a MockFooView (used only for unit testing
related logic) for a given IFooView interface.  In some cases, though, multiple
implementations for a given IView interface may be present in the real run-time system. 
For example, there may be “basic” and “expert” views of the same data and these views
my be different enough that it doesn’t make sense to use DataTemplates and configuration
properties to set up the View.  We’ve already seen that we are keeping a list
of IView in the INavigationService, we can now add another overload to this interface
to elegantly handle situations where more than one IView implementation is available.</font>
        </p>
        <p>
          <font color="#c0c0c0">You may have noticed in the various screenshots that there is
a CheckBox on the UI with accompanying text “I am a Fanboy”.  The goal here is
that if users check this item, we will show them a more rich set of data when they
choose <u>View Details</u>.  By passing a sort of Predicate to the INavigationService,
we can easily accomplish this goal:</font>
        </p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp5_2.jpg">
            <font color="#c0c0c0">
              <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp5" border="0" alt="navapp5" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp5_thumb.jpg" width="644" height="94" />
            </font>
          </a>
        </p>
        <p>
          <font color="#c0c0c0">For now, the Fanboy checkbox sets a global variable we can access
from within Models.xaml.cs.  I’ll use this value from within this Navigate code
to create a <strong>selector function</strong>.</font>
        </p>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre style="width: 680px; height: 158px" class="code">
          <span style="color: blue">protected
void </span>ViewDetailsClick(<span style="color: blue">object </span>s, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: blue">bool </span>isUserFanboy = <span style="color: #2b91af">App</span>.UserIsFanboy; <span style="color: #2b91af">VehicleModel </span>mdl
= <span style="color: blue">null</span>; <span style="color: blue">var </span>lnk
= (<span style="color: #2b91af">HyperlinkButton</span>)s; mdl = (<span style="color: #2b91af">VehicleModel</span>)lnk.Tag;
Navigation.NavigateTo&lt;<span style="color: #2b91af">IModelDetailView</span>&gt;((v)
=&gt; v.TargetModel = mdl, (v) =&gt; v.IsFanboyView == isUserFanboy); }</pre>
        <p>
          <font color="#c0c0c0">I can now create two implementations of IModelDetailView, a
basic page and the “fanboy” page displaying more detailed information.  Based
on this CheckBox option, Navigation will send the user to a completely different IView.</font>
        </p>
        <p>
          <font color="#c0c0c0">Regular View:</font>
        </p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp6_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp6" border="0" alt="navapp6" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp6_thumb.jpg" width="644" height="451" />
          </a>
        </p>
        <p>
          <font color="#c0c0c0">Fanboy View, with extra data:</font>
        </p>
        <p>
 <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp7_2.jpg"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp7" border="0" alt="navapp7" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp7_thumb.jpg" width="644" height="453" /></a></p>
        <p>
          <font color="#c0c0c0">In this case, the Fanboy view could probably have been done
without an extra view implementation, but you can likely see how this would be extremely
useful.</font>
        </p>
        <h2>
          <font color="#c0c0c0">What Explodes?</font>
        </h2>
        <p>
          <font color="#c0c0c0">As more than one friend has pointed out to me, when building
classes to fit into a framework mechanism there can often be an Explosion of classes,
enums, interfaces, delegates etc.  What explodes under this approach, or under
MVVM in general?</font>
        </p>
        <p>
          <font color="#c0c0c0">For every Screen in your application you could potentially have
a new IView interface, a new ViewModel, and possibly a new Presenter or Commands.</font>
        </p>
        <h2>
          <font color="#c0c0c0">Preventing View Explosion</font>
        </h2>
        <p>
          <font color="#c0c0c0">If a particular View does not need extra methods, you can prevent
View explosion by using Generics.  Generics are the answer to everything. 
If your View needs a ViewModel and nothing else special, use something like this:</font>
        </p>
        <pre style="width: 680px; height: 82px" class="code">
          <span style="color: blue">public
interface </span>
          <span style="color: #2b91af">IView</span>
          <t>
: <span style="color: #2b91af">IView </span><span style="color: blue">where </span>T
: <span style="color: #2b91af">ViewModel </span>{ T ViewModel { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} }
</t>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <font color="#c0c0c0">This allows you to implement Views, gain the navigation benefits,
but not create a new IView interface.</font>
        </p>
        <pre style="width: 680px; height: 82px" class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.NavDemo.Screens
{ <span style="color: blue">public partial class </span><span style="color: #2b91af">SampleView </span>: <span style="color: #2b91af">Page</span>, <span style="color: #2b91af">IView</span>&lt;<span style="color: #2b91af">MainViewModel</span>&gt;
{</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <h2>
          <font color="#c0c0c0">Preventing ViewModel Explosion</font>
        </h2>
        <p>
          <font color="#c0c0c0">Given the previous example, you may be worried about your ViewModel
classes exploding, one per IView interface.  I may have mentioned before that
generics are the answer to everything, sometimes lambda expressions are part of the
answer but the answer definitely always involves generics.  You might, for example,
need a ViewModel that does nothing but encapsulate an already existing Data Type from
your domain.  There’s a generic type for that:</font>
        </p>
        <pre style="width: 680px; height: 353px" class="code">
          <span style="color: gray">/// 
<summary>
/// 
</summary></span>
          <span style="color: green">If the ViewModel can be satisfied by a
pre-existing data type, </span>
          <span style="color: gray">/// </span>
          <span style="color: green">just
use that </span>
          <span style="color: gray">/// 
/// 
<typeparam name="T" /></span>
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">ViewModel</span>
          <t>
: <span style="color: #2b91af">ViewModel </span><span style="color: blue">where </span>T
: <span style="color: #2b91af">INotifyPropertyChanged </span>{ <span style="color: blue">public </span>ViewModel(T
source) { Payload = source; } <span style="color: blue">private </span>T _Payload; <span style="color: blue">public </span>T
Payload { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_Payload;
} <span style="color: blue">set </span>{ _Payload = <span style="color: blue">value</span>;
OnPropertyChanged(<span style="color: #a31515">"Payload"</span>); } } }
</t>
        </pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>
          <font color="#c0c0c0">Combined with
the previous example, you could get away without another IView or ViewModel implementation:</font>
        </p>
        <pre style="width: 680px; height: 59px" class="code">
          <span style="color: blue">public
partial class </span>
          <span style="color: #2b91af">SampleView </span>: <span style="color: #2b91af">Page</span>, <span style="color: #2b91af">IView</span>&lt; <span style="color: #2b91af">ViewModel</span>&lt;<span style="color: #2b91af">VehicleModel</span>&gt;
&gt;</pre>
        <p>
          <font color="#c0c0c0">If VehicleModel satisfies all of your needs for a View, you
don’t need to create any new classes or interfaces.</font>
        </p>
        <p>
          <font color="#c0c0c0">I’ll talk about Presenters and Commands in a future article.</font>
        </p>
        <h1>
          <font color="#c0c0c0">Conclusion</font>
        </h1>
        <p>
          <font color="#c0c0c0">While the current trends in MVVM may downplay the role of a
View interface, IView can be a powerful part of your programming toolbox.  Since
the View is an actor in this pattern, it makes sense to be able to treat Views a certain
way.  In this article you’ve seen how you can build a flexible Navigation framework
around Views and even support handy features like strongly typed parameter passing
and selecting from multiple implementations when present.  You can do all this
without manually creating any kind of mapping.</font>
        </p>
        <p>
          <font color="#c0c0c0">
            <u>
              <strong>HandWaver.AG</strong>
            </u>: I mentioned elsewhere
I would be publishing the source for all of my Silverlight Guidance demos.  You
can find it </font>
          <a href="http://www.damonpayne.com/HandWaver.AG/HandWaverAG.zip">
            <font color="#c0c0c0">here</font>
          </a>
          <font color="#c0c0c0">,
though it may be updated by future articles and look slightly differently than what
you read here.</font>
        </p>
        <p>
          <font color="#c0c0c0">You can also run the </font>
          <a href="http://www.damonpayne.com/HandWaver.AG/Demos/HandWaver.AG.NavDemoTestPage.html">
            <font color="#c0c0c0">Demo
Application</font>
          </a>
          <font color="#c0c0c0">.</font>
        </p>
        <h4>
          <font color="#c0c0c0">Future Tasks</font>
        </h4>
        <p>
          <font color="#c0c0c0">There are a few things I had to leave out as this article was
getting too long.  The first is the promised Navigation History module. 
This is 99.9% done but must have its own article.  </font>
        </p>
        <p>
          <font color="#c0c0c0">Second, and not very relevant here, is the Photo Gallery page. 
This will serve up a cool demo when I get to it.</font>
        </p>
        <p>
          <font color="#c0c0c0">Finally, If you read the source code closely you may have noticed
my comments about the reflection-based IView discovery mechanism.   The
mechanism makes the assumption that all IViews you can navigate to are in the initial
startup assembly.  In most applications that might be a safe assumption, but
MEF is working on changing that with dynamic XAP loading.  Once I’ve had a chance
to cozy up with MEF more I’ll post an updated solution.  Also related to MEF,
storing the composition catalog on the App class is probably not a best practice.</font>
        </p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=9ab5db77-6976-404a-b06e-87449a6ba99f" />
      </body>
      <title>View Based Navigation in Silverlight 3</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,9ab5db77-6976-404a-b06e-87449a6ba99f.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/nxIs_curD1c/ViewBasedNavigationInSilverlight3.aspx</link>
      <pubDate>Thu, 17 Sep 2009 01:28:20 GMT</pubDate>
      <description>&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Silverlight 3 ships with a Navigation mechanism.&amp;#160; A control that extends Page
can be stored inside the Frame control as content, and Navigation from Page to Page
is done by specifying the URI of the target Page.&amp;#160; This mechanism includes navigation
history and the ability to go forwards and backwards through said history.&amp;#160; I
would, however, like a slightly different programming model around application navigation.
&lt;/p&gt;
&lt;p&gt;
Design goals:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I would like to be able to navigate to a new “screen” from within
various places such as Presenters or Commands&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I do not want to have to configure each new View I add to the
system&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I don’t want to have to map Views to URIs to use the Frame&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I need to be able to pass parameters to Views as they are navigated
to&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I need to support having more than one View implementation in
the system&lt;/font&gt; for a given View interface type 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I need an inspect-able navigation history that I can also configure
in terms of how much history to keep and so forth&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;I would like to support animated transitions from View to View,
this is Silverlight after all&lt;/font&gt; 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
My sample application will be an “Audi Fan Site” in Silverlight.
&lt;/p&gt;
&lt;h1&gt;Views &amp;amp; ViewModels
&lt;/h1&gt;
&lt;p&gt;
I have a Silverlight “guidance” Solution called HandWaver.AG; I will use some concepts
from this solution to build the navigation framework and post the entire solution’s
source code.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#80ff80"&gt;Pattern: For separation of concerns, I’ll use the Model-View-ViewModel
pattern.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
I’ll start with a humble IView base interface:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.PresenationModel
{ &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;{ &lt;span style="color: blue"&gt;string &lt;/span&gt;Title
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; } &lt;span style="color: blue"&gt;void &lt;/span&gt;Activate(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;complete); &lt;span style="color: blue"&gt;void &lt;/span&gt;DeActivate(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;complete);
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Nothing complicated here.&amp;#160; The Activate and Deactivate methods can be called
to inform a IView that it is about to be “started” or shut down.&amp;#160; The Action
callbacks will facilitate animation later.
&lt;/p&gt;
&lt;p&gt;
Now I need a ViewModel base class that does a little bit of change notification work
for me.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.PresenationModel
{ &lt;span style="color: blue"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ViewModel &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler &lt;/span&gt;PropertyChanged; &lt;span style="color: blue"&gt;protected
void &lt;/span&gt;OnPropertyChanged(&lt;span style="color: blue"&gt;string &lt;/span&gt;propName) { &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;null &lt;/span&gt;!=
PropertyChanged) { PropertyChanged(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs&lt;/span&gt;(propName));
} } } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I will expand on these concepts in a little bit, for now assume that I will have Controls
of some kind that implement IView and bind to ViewModels.
&lt;/p&gt;
&lt;h1&gt;Navigation
&lt;/h1&gt;
&lt;p&gt;
For the most simple case, I want to be able to just go to an IVIew.&amp;#160; I’ve created
an INavigationService interface.&amp;#160; This is the calling convention that I’ll use.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.PresenationModel.Navigation
{ &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;INavigationService &lt;/span&gt;{ &lt;span style="color: gray"&gt;/// 
&lt;summary&gt;
/// 
&lt;/span&gt;&lt;span style="color: green"&gt;Display the first IView implementation we find &lt;/span&gt;&lt;span style="color: gray"&gt;/// &gt;
/// 
&lt;typeparam name="T"&gt;
&lt;/typeparam&gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;NavigateTo&amp;lt; T &amp;gt;() &lt;span style="color: blue"&gt;where &lt;/span&gt;T:&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;
At this point I can go ahead and create my sample application and my “Home” screen.&amp;#160;
I’m going to re-use the Frame and Page concepts from Silverlight.&amp;#160; I have some
navigation buttons that are disabled, a ComboBox that will contain my navigation history,
and some HyperlinkButtons up at the top to link to the various IViews I will create.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp0_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp0" border="0" alt="navapp0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp0_thumb.jpg" width="644" height="437" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The grey area is my navigation Frame.&amp;#160; What I want to do now is Navigate to the
Home screen when the app is loaded.&amp;#160; As you can see from the INavigationService
code above, I’m just going to do this:
&lt;/p&gt;
&lt;pre style="width: 680px; height: 53px" class="code"&gt;Navigation.NavigateTo&lt;span style="color: #2b91af"&gt;IHomeView&lt;/span&gt;&amp;gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Adding this now displays my Home screen in the navigation frame…
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp1_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp1" border="0" alt="navapp1" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp1_thumb.jpg" width="644" height="436" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
… but how did we get there?&amp;#160; The first item is the discovery of IViews.
&lt;/p&gt;
&lt;h2&gt;Discovering Views
&lt;/h2&gt;
&lt;p&gt;
In order to meet my design goals, I need to figure out how to find IView implementations
automatically without having to create some kind of View Registry.&amp;#160; In my implementation
of the INavigationService interface, reflection is the key.&amp;#160; It’s worth going
over this code as everything else depends on it:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;MapViewTypes() { _viewMap
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&amp;gt;&amp;gt;(); &lt;span style="color: green"&gt;//Note:
we're making a giant assumption here that all Views are in this assembly. MEF could
help? &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;types = _viewAssembly.GetTypes(); &lt;span style="color: blue"&gt;var &lt;/span&gt;viewType
= &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;); &lt;span style="color: blue"&gt;var &lt;/span&gt;pageType
= &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Page&lt;/span&gt;); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;t &lt;span style="color: blue"&gt;in &lt;/span&gt;types)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(t.ImplmentsInterface(viewType) &amp;amp;&amp;amp; t.IsSubclassOf(pageType))
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;viewImplType = t; &lt;span style="color: green"&gt;//if
we have a sub-class of IView, use that instead &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;subViewType
= (&lt;span style="color: blue"&gt;from &lt;/span&gt;v &lt;span style="color: blue"&gt;in &lt;/span&gt;viewImplType.GetInterfaces() &lt;span style="color: blue"&gt;where &lt;/span&gt;v.InterfaceExtends(viewType) &lt;span style="color: blue"&gt;select &lt;/span&gt;v).FirstOrDefault(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;null &lt;/span&gt;!=
subViewType) { EnsureList(subViewType); _viewMap[subViewType].Add(t); } &lt;span style="color: blue"&gt;else &lt;/span&gt;{ &lt;span style="color: blue"&gt;continue&lt;/span&gt;;
} } } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;ImplementsInterface&lt;/strong&gt; and &lt;strong&gt;InterfaceExtends&lt;/strong&gt; are extension
methods I wrote for System.Type.&amp;#160; When this class is created, I’m reflecting
the implementing assembly and finding all classes that implement a descended of IView
and storing them in a List.&amp;#160; The implementation for the most basic NavigateTo&lt;t&gt;
(), then, just looks like this:
&lt;/p&gt;
&lt;pre style="width: 680px; height: 122px" class="code"&gt;&lt;span style="color: blue"&gt;public
void &lt;/span&gt;NavigateTo&lt;t&gt;
() &lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;{ &lt;span style="color: blue"&gt;var &lt;/span&gt;viewType
= _viewMap[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T)][0]; &lt;span style="color: blue"&gt;var &lt;/span&gt;view
= (&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(viewType);
InitializeView(view); }
&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
In the most basic case, I am just using the first IView type that I find.&amp;#160; As
for how the navigation actually happens, I will have to expose a few more implementation
details now.
&lt;/p&gt;
&lt;pre style="width: 680px; height: 234px" class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.PresenationModel.Navigation.Modules
{ [&lt;span style="color: #2b91af"&gt;Export&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;INavigationService&lt;/span&gt;))] &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;URIFrameNavigationService &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INavigationService &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;URIFrameNavigationService(&lt;span style="color: blue"&gt;string &lt;/span&gt;rootNamespace, &lt;span style="color: #2b91af"&gt;Assembly &lt;/span&gt;va)
{ _viewAssembly = va; MapViewTypes(); } &lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;nullCallback
= () =&amp;gt; { }; [&lt;span style="color: #2b91af"&gt;Import&lt;/span&gt;(AllowRecomposition=&lt;span style="color: blue"&gt;true&lt;/span&gt;)] &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Frame &lt;/span&gt;TargetFrame
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Note that, for now, I am using MEF as an IoC container.&amp;#160; URIFrameNavigationService
uses the Export attribute to tell MEF that it implments the INavigationService contract.&amp;#160;
When I construct the instance I pass in some basic information and the class uses
MEF to import an instance of the &lt;strong&gt;Frame&lt;/strong&gt; class from Silverlight navigation.&amp;#160;
So for the most basic kind of navigation, I’m instantiating a new instance of a Page
implementing an IView interface and directly setting the Content of the Frame rather
than giving it a URI.
&lt;/p&gt;
&lt;h2&gt;Transitions
&lt;/h2&gt;
&lt;p&gt;
Next, I’m going to build the “Models” screen of my Audi fan application; in this case
we’re talking about Audi cars and not the usual “Model”.&amp;#160; We can go ahead and
look at the implementation for InitializeView() now:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;InitializeView(&lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;v)
{ &lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;go = () =&amp;gt; { TargetFrame.Content =
v; v.Activate(&lt;span style="color: #2b91af"&gt;URIFrameNavigationService&lt;/span&gt;.nullCallback);
History.Navigated(v); }; &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;null &lt;/span&gt;!=
TargetFrame.Content &amp;amp;&amp;amp; TargetFrame.Content &lt;span style="color: blue"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;)
{ ((&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;)TargetFrame.Content).DeActivate(go);
} &lt;span style="color: blue"&gt;else &lt;/span&gt;{ go(); } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
If the Frame is currently displaying an IView, call it’s deactivate method.&amp;#160;
When that method is done, the go action is called which activates the new IView.&amp;#160;
We also do something with a History object here which I’ll get to in a little bit.&amp;#160;
By using callbacks with Activate and Deactivate we’re able to incorporate animations
into the navigation mix.&amp;#160; Here is the Activate implementation for Models.xaml:
&lt;/p&gt;
&lt;pre style="width: 680px; height: 215px" class="code"&gt;&lt;span style="color: blue"&gt;public
void &lt;/span&gt;Activate(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;complete) { &lt;span style="color: blue"&gt;var &lt;/span&gt;trans
= (&lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;App&lt;/span&gt;.Current.Resources[&lt;span style="color: #a31515"&gt;&amp;quot;ExpandIn&amp;quot;&lt;/span&gt;];
trans.Stop(); &lt;span style="color: #2b91af"&gt;Storyboard&lt;/span&gt;.SetTarget(trans, LayoutRoot);
trans.Begin(); &lt;span style="color: #2b91af"&gt;EventHandler &lt;/span&gt;sbDone = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
sbDone = (o, e) =&amp;gt; { complete(); trans.Completed -= sbDone; }; trans.Completed
+= sbDone; }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I pull a common animation out of resources
and call the giving Action when the animation is complete, being sure to unhook event
handlers.&amp;#160; In my case, I’d like to use the same Enter and Exit transitions everywhere
in the application and I don’t want to keep copy &amp;amp; pasting this code.&amp;#160; I
can move this code to a helper class and invoke it this way everywhere else:
&lt;/p&gt;
&lt;pre style="width: 680px; height: 143px" class="code"&gt;&lt;span style="color: blue"&gt;public
void &lt;/span&gt;Activate(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;complete) { &lt;span style="color: #2b91af"&gt;Transition&lt;/span&gt;.Animate(&lt;span style="color: #a31515"&gt;&amp;quot;ExpandIn&amp;quot;&lt;/span&gt;,
LayoutRoot, complete); } &lt;span style="color: blue"&gt;public void &lt;/span&gt;DeActivate(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;complete)
{ &lt;span style="color: #2b91af"&gt;Transition&lt;/span&gt;.Animate(&lt;span style="color: #a31515"&gt;&amp;quot;ShrinkOut&amp;quot;&lt;/span&gt;,
LayoutRoot, complete); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
We could, in future refactoring, create some kind of IViewTransitionManager module
that automatically does transitions everywhere, and save the Activate and Deactivate
methods for other application specific uses.
&lt;/p&gt;
&lt;p&gt;
You can now run the sample application (link at the end of the article) to see the
transitions working from the Home screen to the Models screen.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp2_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp2" border="0" alt="navapp2" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp2_thumb.jpg" width="644" height="452" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;Passing Parameters
&lt;/h2&gt;
&lt;p&gt;
The &lt;u&gt;View Details&lt;/u&gt; Hyperlink buttons, above, should navigate to a special screen
showing even more&amp;#160; information about the chosen vehicle model.&amp;#160; When this
screen loads, then, the chosen Model will need to be available.&amp;#160; We could set
some global User State variable specifying what model of Audi was chosen but it would
be nicer to navigate to the chosen IView and directly pass parameters to it.&amp;#160;
What’s more, it would be ideal if this parameter passing could be strongly typed such
that we can determine the types of parameters using Intellisense.
&lt;/p&gt;
&lt;p&gt;
We have already seen how the INavigationService implementation locates and instantiates
an IView implementation.&amp;#160; Adding another overload the the interface implementation
meets all stated design goals.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Navigate
to a View, passing parameters to said view using the supplied Action &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; /// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt; /// &amp;lt;param
name=&amp;quot;paramSetter&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;NavigateTo&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;
paramSetter) &lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
It may not be apparent at first look what this allows us to do, but consider the current
example of choosing a vehicle model from a list.&amp;#160; The ViewModel of the screen
we are coming from knows what the selected VehicleModel is.&amp;#160; Using the excellent
Generics implementation in C# we can navigate to an IView and easily see what parameters
we might be able to pass to it via Intellisense.
&lt;/p&gt;
&lt;pre style="width: 680px; height: 117px" class="code"&gt;&lt;span style="color: blue"&gt;protected
void &lt;/span&gt;ViewDetailsClick(&lt;span style="color: blue"&gt;object &lt;/span&gt;s, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: #2b91af"&gt;VehicleModel &lt;/span&gt;mdl = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;var &lt;/span&gt;lnk
= (&lt;span style="color: #2b91af"&gt;HyperlinkButton&lt;/span&gt;)s; mdl = (&lt;span style="color: #2b91af"&gt;VehicleModel&lt;/span&gt;)lnk.Tag;
Navigation.NavigateTo&amp;lt; &lt;span style="color: #2b91af"&gt;IModelDetailView&lt;/span&gt;&amp;gt;((v)
=&amp;gt; v.TargetModel = mdl); }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I am cheating a little bit here, at least it feels like cheating.&amp;#160; My list of
vehicle models is implemented as a listbox and I am using (abusing?) the Tag property
that Controls expose in Silverlight:
&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; &lt;pre style="width: 680px; height: 50px" class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;HyperlinkButton &lt;/span&gt;&lt;span style="color: red"&gt;Grid.Column&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;5&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;MenuLinkStyle&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;View
Details&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;VerticalAlignment&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Center&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Click&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ViewDetailsClick&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Tag&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;&amp;gt;
&amp;lt;/&amp;lt; span&amp;gt;&lt;span style="color: #a31515"&gt;HyperlinkButton&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;font color="#808080"&gt;Since we can use Bindings for Tag, the actual data type for&amp;#160;
each VehicleModel is stored with the Hyperlinkbutton and we can pull it in the Click
event handler.&amp;#160; The real power of what we are now able to do is demonstrated
when we see the intellisense available when navigating to IModelDetailView and exploring
the overloads available:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp4_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp4" border="0" alt="navapp4" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp4_thumb.jpg" width="670" height="148" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp3_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp3" border="0" alt="navapp3" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp3_thumb.jpg" width="500" height="293" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Since we have a strongly-typed mechanism, we can create a lambda
expression that takes the IView instance and we can inspect the list of public properties
available on IModelDetailView.&amp;#160; In my opinion this makes for a perfectly discoverable
mechanism for passing parameters directly to an IView.&amp;#160; If you need further help,
you could create a class something like RequiredViewParameter&lt;t&gt;
;&amp;#160; having such a type in the Intellisene dropdown might help clients determine
which properties &lt;em&gt;must&lt;/em&gt; be set in order for a View to function.&amp;#160; Obviously,
we an set as many properties as we want using the lambda expression.
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;&lt;font color="#c0c0c0"&gt;Choosing Between Multiple IView Implementations &lt;/font&gt;
&lt;/h2&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Often, there may be only one implementation for a given IView
interface at run time and compile time.&amp;#160; For example, there may likely be only
a RealFooView (the real production visual) and a MockFooView (used only for unit testing
related logic) for a given IFooView interface.&amp;#160; In some cases, though, multiple
implementations for a given IView interface may be present in the real run-time system.&amp;#160;
For example, there may be “basic” and “expert” views of the same data and these views
my be different enough that it doesn’t make sense to use DataTemplates and configuration
properties to set up the View.&amp;#160; We’ve already seen that we are keeping a list
of IView in the INavigationService, we can now add another overload to this interface
to elegantly handle situations where more than one IView implementation is available.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;You may have noticed in the various screenshots that there is
a CheckBox on the UI with accompanying text “I am a Fanboy”.&amp;#160; The goal here is
that if users check this item, we will show them a more rich set of data when they
choose &lt;u&gt;View Details&lt;/u&gt;.&amp;#160; By passing a sort of Predicate to the INavigationService,
we can easily accomplish this goal:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp5_2.jpg"&gt;&lt;font color="#c0c0c0"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp5" border="0" alt="navapp5" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp5_thumb.jpg" width="644" height="94" /&gt;&lt;/font&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;For now, the Fanboy checkbox sets a global variable we can access
from within Models.xaml.cs.&amp;#160; I’ll use this value from within this Navigate code
to create a &lt;strong&gt;selector function&lt;/strong&gt;.&lt;/font&gt;
&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; &lt;pre style="width: 680px; height: 158px" class="code"&gt;&lt;span style="color: blue"&gt;protected
void &lt;/span&gt;ViewDetailsClick(&lt;span style="color: blue"&gt;object &lt;/span&gt;s, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: blue"&gt;bool &lt;/span&gt;isUserFanboy = &lt;span style="color: #2b91af"&gt;App&lt;/span&gt;.UserIsFanboy; &lt;span style="color: #2b91af"&gt;VehicleModel &lt;/span&gt;mdl
= &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;var &lt;/span&gt;lnk
= (&lt;span style="color: #2b91af"&gt;HyperlinkButton&lt;/span&gt;)s; mdl = (&lt;span style="color: #2b91af"&gt;VehicleModel&lt;/span&gt;)lnk.Tag;
Navigation.NavigateTo&amp;lt;&lt;span style="color: #2b91af"&gt;IModelDetailView&lt;/span&gt;&amp;gt;((v)
=&amp;gt; v.TargetModel = mdl, (v) =&amp;gt; v.IsFanboyView == isUserFanboy); }&lt;/pre&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;I can now create two implementations of IModelDetailView, a
basic page and the “fanboy” page displaying more detailed information.&amp;#160; Based
on this CheckBox option, Navigation will send the user to a completely different IView.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Regular View:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp6_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp6" border="0" alt="navapp6" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp6_thumb.jpg" width="644" height="451" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Fanboy View, with extra data:&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;#160;&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp7_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="navapp7" border="0" alt="navapp7" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/ViewBasedNavigationinSilverlight3_D14D/navapp7_thumb.jpg" width="644" height="453" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;In this case, the Fanboy view could probably have been done
without an extra view implementation, but you can likely see how this would be extremely
useful.&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;&lt;font color="#c0c0c0"&gt;What Explodes?&lt;/font&gt;
&lt;/h2&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;As more than one friend has pointed out to me, when building
classes to fit into a framework mechanism there can often be an Explosion of classes,
enums, interfaces, delegates etc.&amp;#160; What explodes under this approach, or under
MVVM in general?&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;For every Screen in your application you could potentially have
a new IView interface, a new ViewModel, and possibly a new Presenter or Commands.&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;&lt;font color="#c0c0c0"&gt;Preventing View Explosion&lt;/font&gt;
&lt;/h2&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;If a particular View does not need extra methods, you can prevent
View explosion by using Generics.&amp;#160; Generics are the answer to everything.&amp;#160;
If your View needs a ViewModel and nothing else special, use something like this:&lt;/font&gt; 
&lt;/p&gt;
&lt;pre style="width: 680px; height: 82px" class="code"&gt;&lt;span style="color: blue"&gt;public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;
&lt;t&gt;
: &lt;span style="color: #2b91af"&gt;IView &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;T
: &lt;span style="color: #2b91af"&gt;ViewModel &lt;/span&gt;{ T ViewModel { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} }
&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;This allows you to implement Views, gain the navigation benefits,
but not create a new IView interface.&lt;/font&gt;
&lt;/p&gt;
&lt;pre style="width: 680px; height: 82px" class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.NavDemo.Screens
{ &lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SampleView &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Page&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MainViewModel&lt;/span&gt;&amp;gt;
{&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;&lt;font color="#c0c0c0"&gt;Preventing ViewModel Explosion&lt;/font&gt;
&lt;/h2&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Given the previous example, you may be worried about your ViewModel
classes exploding, one per IView interface.&amp;#160; I may have mentioned before that
generics are the answer to everything, sometimes lambda expressions are part of the
answer but the answer definitely always involves generics.&amp;#160; You might, for example,
need a ViewModel that does nothing but encapsulate an already existing Data Type from
your domain.&amp;#160; There’s a generic type for that:&lt;/font&gt;
&lt;/p&gt;
&lt;pre style="width: 680px; height: 353px" class="code"&gt;&lt;span style="color: gray"&gt;/// 
&lt;summary&gt;
/// 
&lt;/span&gt;&lt;span style="color: green"&gt;If the ViewModel can be satisfied by a pre-existing
data type, &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;just
use that &lt;/span&gt;&lt;span style="color: gray"&gt;/// &gt;
/// 
&lt;typeparam name="T"&gt;
&lt;/typeparam&gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ViewModel&lt;/span&gt;
&lt;t&gt;
: &lt;span style="color: #2b91af"&gt;ViewModel &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;T
: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;ViewModel(T
source) { Payload = source; } &lt;span style="color: blue"&gt;private &lt;/span&gt;T _Payload; &lt;span style="color: blue"&gt;public &lt;/span&gt;T
Payload { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Payload;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Payload = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
OnPropertyChanged(&lt;span style="color: #a31515"&gt;&amp;quot;Payload&amp;quot;&lt;/span&gt;); } } }
&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;font color="#c0c0c0"&gt;Combined with
the previous example, you could get away without another IView or ViewModel implementation:&lt;/font&gt;
&lt;/p&gt;
&lt;pre style="width: 680px; height: 59px" class="code"&gt;&lt;span style="color: blue"&gt;public
partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SampleView &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Page&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IView&lt;/span&gt;&amp;lt; &lt;span style="color: #2b91af"&gt;ViewModel&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;VehicleModel&lt;/span&gt;&amp;gt;
&amp;gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;If VehicleModel satisfies all of your needs for a View, you
don’t need to create any new classes or interfaces.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;I’ll talk about Presenters and Commands in a future article.&lt;/font&gt;
&lt;/p&gt;
&lt;h1&gt;&lt;font color="#c0c0c0"&gt;Conclusion&lt;/font&gt;
&lt;/h1&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;While the current trends in MVVM may downplay the role of a
View interface, IView can be a powerful part of your programming toolbox.&amp;#160; Since
the View is an actor in this pattern, it makes sense to be able to treat Views a certain
way.&amp;#160; In this article you’ve seen how you can build a flexible Navigation framework
around Views and even support handy features like strongly typed parameter passing
and selecting from multiple implementations when present.&amp;#160; You can do all this
without manually creating any kind of mapping.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;&lt;u&gt;&lt;strong&gt;HandWaver.AG&lt;/strong&gt;&lt;/u&gt;: I mentioned elsewhere
I would be publishing the source for all of my Silverlight Guidance demos.&amp;#160; You
can find it &lt;/font&gt;&lt;a href="http://www.damonpayne.com/HandWaver.AG/HandWaverAG.zip"&gt;&lt;font color="#c0c0c0"&gt;here&lt;/font&gt;&lt;/a&gt;&lt;font color="#c0c0c0"&gt;,
though it may be updated by future articles and look slightly differently than what
you read here.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;You can also run the &lt;/font&gt;&lt;a href="http://www.damonpayne.com/HandWaver.AG/Demos/HandWaver.AG.NavDemoTestPage.html"&gt;&lt;font color="#c0c0c0"&gt;Demo
Application&lt;/font&gt;&lt;/a&gt;&lt;font color="#c0c0c0"&gt;.&lt;/font&gt;
&lt;/p&gt;
&lt;h4&gt;&lt;font color="#c0c0c0"&gt;Future Tasks&lt;/font&gt;
&lt;/h4&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;There are a few things I had to leave out as this article was
getting too long.&amp;#160; The first is the promised Navigation History module.&amp;#160;
This is 99.9% done but must have its own article.&amp;#160; &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Second, and not very relevant here, is the Photo Gallery page.&amp;#160;
This will serve up a cool demo when I get to it.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#c0c0c0"&gt;Finally, If you read the source code closely you may have noticed
my comments about the reflection-based IView discovery mechanism.&amp;#160;&amp;#160; The
mechanism makes the assumption that all IViews you can navigate to are in the initial
startup assembly.&amp;#160; In most applications that might be a safe assumption, but
MEF is working on changing that with dynamic XAP loading.&amp;#160; Once I’ve had a chance
to cozy up with MEF more I’ll post an updated solution.&amp;#160; Also related to MEF,
storing the composition catalog on the App class is probably not a best practice.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=9ab5db77-6976-404a-b06e-87449a6ba99f" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,9ab5db77-6976-404a-b06e-87449a6ba99f.aspx</comments>
      <category>Architecture and Design</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/09/17/ViewBasedNavigationInSilverlight3.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=2582a6a5-ad67-4509-ba3c-cb19d0806cc8</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,2582a6a5-ad67-4509-ba3c-cb19d0806cc8.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,2582a6a5-ad67-4509-ba3c-cb19d0806cc8.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=2582a6a5-ad67-4509-ba3c-cb19d0806cc8</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It is sometimes useful or necessary to represent complex data types as name/value
pairs, or Dictionaries if you like.  While the Data Binding implementation in
Silverlight 3 is admirable, there is not a built in way to do this cleanly. 
Doing some Internet searches, you may find some approaches like this, from <a title="http://silverlight.net/forums/t/51864.aspx" href="http://silverlight.net/forums/t/51864.aspx">http://silverlight.net/forums/t/51864.aspx</a> :
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">DictionaryItemConverter </span>:
IValueConverter { <span style="color: blue">public object </span>Convert(<span style="color: blue">object </span>value, <span style="color: #2b91af">Type </span>targetType, <span style="color: blue">object </span>parameter,
CultureInfo culture) { <span style="color: blue">var </span>dict = value <span style="color: blue">as </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">string</span>&gt;; <span style="color: blue">if </span>(dict
!= <span style="color: blue">null</span>) { <span style="color: blue">return </span>dict[parameter <span style="color: blue">as
string</span>]; } <span style="color: blue">throw new </span><span style="color: #2b91af">NotImplementedException</span>();
} <span style="color: blue">public object </span>ConvertBack(<span style="color: blue">object </span>value, <span style="color: #2b91af">Type </span>targetType, <span style="color: blue">object </span>parameter,
CultureInfo culture) { <span style="color: blue">throw new </span><span style="color: #2b91af">NotImplementedException</span>();
} }</pre>
        <p>
Note that in all cases you will find the “ConvertBack” strategy missing.  Since
the original source object is not passed to ConvertBack, two-way binding to 
name/value pairs is an elusive goal.  <strong>The binding mechanisms in Silverlight
3 are not very extensible</strong>.  I cannot create my own binding type or provide
a new implementation for Property Paths.  The Silverlight 3 Data Binding framework
wants real Properties to bind to.  If we <em>must</em> have two way bindings
to name/value pairs, we are left with pursuing more extreme measures.
</p>
        <h2>Dynamic Type Generation
</h2>
        <p>
I have <a href="http://www.damonpayne.com/2008/09/18/RunTimeIsDesignTimeForAGT5.aspx">written
in the past</a> about the complete Reflection Emit implementation in Silverlight. 
It seemed to me that given a means of representing the names and types of the items
we want to pull out of a dictionary as Properties, we could dynamically generate a
bindable version of a class represented as a dictionary.  We’ll start with creating
a very simple model for Objects with data represented as name/value pairs.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.EVModel
{ <span style="color: blue">public class </span><span style="color: #2b91af">Attribute </span>{ <span style="color: blue">public </span>Attribute(<span style="color: blue">int </span>id, <span style="color: blue">string </span>name, <span style="color: #2b91af">Type </span>t)
{ Id = id; Name = name; AttributeValueType = t; } <span style="color: blue">public
int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public string </span>Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} <span style="color: blue">public </span><span style="color: #2b91af">Type </span>AttributeValueType
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } }
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This attribute, not to be confused with the .NET Attribute, represents a sort of metadata
for a single property on an object.  We give it a name (which ultimately must
be friendly to becoming a Property name) and a run time type.  A single Attribute
might be the "Name” property of some object with a type of string.  We may
not ultimately want to bind to every single name/value pair available to us. 
A means of representing a certain sub-Model is therefore very helpful.  A List
of Attribute will suffice for this demo.
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.EVModel
{ <span style="color: blue">public class </span><span style="color: #2b91af">AttributeViewModel </span>:
System.Collections.Generic.<span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Attribute</span>&gt;
{ <span style="color: blue">public </span>AttributeViewModel() : <span style="color: blue">this</span>( <span style="color: #2b91af">Guid</span>.NewGuid()
) { } <span style="color: blue">public </span>AttributeViewModel(<span style="color: #2b91af">Guid </span>g)
{ UniqueId = g; } <span style="color: blue">public </span><span style="color: #2b91af">Guid </span>UniqueId
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } }
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The AttributeViewModel class exposes a UniqueId property of type Guid, which will
be useful later.
</p>
        <p>
Reflection Emit code, while incredibly powerful, can also be tedious, difficult, and
error prone.  Once practice that I follow is to have as little dynamic code as
possible, calling out to pre built classes and only doing what <em>must </em>be done
in dynamically created types.  In order to be friendly to binding and provide
a base to extend we’ll use a base class that does most of what we want to do already:
</p>
        <pre class="code">
          <span style="color: blue">namespace </span>HandWaver.AG.EVModel
{ <span style="color: blue">public class </span><span style="color: #2b91af">EntityWithAttributes </span>: <span style="color: #2b91af">INotifyPropertyChanged </span>{ <span style="color: blue">public </span>EntityWithAttributes()
{ _values = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
} <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;
_values; <span style="color: blue">public object this</span>[<span style="color: blue">string </span>key]
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>_values[key];
} <span style="color: blue">set </span>{ _values[key] = <span style="color: blue">value</span>;
OnPropertyChanged(key); } } <span style="color: blue">protected void </span>OnPropertyChanged(<span style="color: blue">string </span>propName)
{ <span style="color: blue">if </span>(<span style="color: blue">null </span>!= PropertyChanged)
{ PropertyChanged(<span style="color: blue">this</span>, <span style="color: blue">new </span><span style="color: #2b91af">PropertyChangedEventArgs</span>(propName));
} } <span style="color: blue">public event </span><span style="color: #2b91af">PropertyChangedEventHandler </span>PropertyChanged;</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Next, we’ll build a class to create the dynamic types.  I’m currently calling
this a Bindable Entity-Value Builder.  It’s worth going over the Reflection Emit
code in some detail.  The public interface for BindableEVBuilder is a single
method:
</p>
        <pre class="code">
          <span style="color: blue">public static </span>
          <span style="color: #2b91af">Type </span>GetDecoratorType(<span style="color: #2b91af">AttributeViewModel </span>prototype)
{ EnsureDynamicAssembly(); EnsureModelTypes(); <span style="color: blue">if </span>(!_modelTypes.ContainsKey(prototype.UniqueId))
{ BuildDecoratorType(prototype); } <span style="color: blue">return </span>_modelTypes[prototype.UniqueId];
}</pre>
        <p>
We can look at the first several methods later.  The bulk of our work is done
in BuildDecoratorType.
</p>
        <pre class="code">
          <span style="color: blue">static void </span>BuildDecoratorType(<span style="color: #2b91af">AttributeViewModel </span>prototype)
{ <span style="color: blue">var </span>typeBuilder = _moduleBuilder.DefineType(<span style="color: #a31515">"HandWaver.AG.EVModel.TypeDecorator.DictWrapper"</span>+
GetName(prototype.UniqueId), <span style="color: #2b91af">TypeAttributes</span>.Public
| <span style="color: #2b91af">TypeAttributes</span>.Serializable | <span style="color: #2b91af">TypeAttributes</span>.Class, <span style="color: blue">typeof</span>(<span style="color: #2b91af">EntityWithAttributes</span>)); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>attribute <span style="color: blue">in </span>prototype)
{ BuildProperty(typeBuilder, attribute); } _modelTypes.Add(prototype.UniqueId, typeBuilder.CreateType());
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
So far there’s nothing clever here, just creating a dynamic class that extends EntityWithAttributes
and making sure that we only generate one dynamic type for each AttributeViewModel
by coming up with a naming scheme.  Properties are somewhat special in the .NET
CLR.  In order to create a property using IL, the property itself must be defined
as well as a getter and setter method.
</p>
        <pre class="code">
          <span style="color: blue">static void </span>BuildProperty(<span style="color: #2b91af">TypeBuilder </span>typeBuilder, <span style="color: #2b91af">Attribute </span>attr)
{ <span style="color: blue">var </span>baseType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">EntityWithAttributes</span>); <span style="color: blue">var </span>propertyBuilder
= typeBuilder.DefineProperty(attr.Name, <span style="color: #2b91af">PropertyAttributes</span>.None,
attr.AttributeValueType, <span style="color: blue">null</span>); <span style="color: green">//
The property set and property get methods require a special // set of attributes. </span><span style="color: #2b91af">MethodAttributes </span>getSetAttr
= <span style="color: #2b91af">MethodAttributes</span>.Public | <span style="color: #2b91af">MethodAttributes</span>.SpecialName
| <span style="color: #2b91af">MethodAttributes</span>.HideBySig; <span style="color: blue">var </span>getterBuilder
= BuildGetter(typeBuilder, attr, baseType, getSetAttr); <span style="color: blue">var </span>setterBuilder
= BuildSetter(typeBuilder, attr, baseType, getSetAttr); propertyBuilder.SetGetMethod(getterBuilder);
propertyBuilder.SetSetMethod(setterBuilder); }</pre>
        <p>
The getter is more interesting than the setter in this situation.
</p>
        <pre class="code">
          <span style="color: blue">private static </span>
          <span style="color: #2b91af">MethodBuilder </span>BuildGetter(<span style="color: #2b91af">TypeBuilder </span>typeBuilder, <span style="color: #2b91af">Attribute </span>attr, <span style="color: #2b91af">Type </span>baseType, <span style="color: #2b91af">MethodAttributes </span>getSetAttr)
{ <span style="color: blue">var </span>getterBuilder = typeBuilder.DefineMethod(<span style="color: #a31515">"get_" </span>+
attr.Name, getSetAttr, attr.AttributeValueType, <span style="color: #2b91af">Type</span>.EmptyTypes); <span style="color: blue">var </span>getterILGen
= getterBuilder.GetILGenerator(); <span style="color: blue">var </span>baseGetterInfo
= baseType.GetMethod(<span style="color: #a31515">"get_Item"</span>, <span style="color: #2b91af">BindingFlags</span>.Instance
| <span style="color: #2b91af">BindingFlags</span>.Public | <span style="color: #2b91af">BindingFlags</span>.NonPublic, <span style="color: blue">null</span>, <span style="color: blue">new </span><span style="color: #2b91af">Type</span>[]
{ <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)
}, <span style="color: blue">null</span>); <span style="color: #2b91af">LocalBuilder </span>localValBuilder
= getterILGen.DeclareLocal(attr.AttributeValueType); <span style="color: green">//
Preparing labels </span><span style="color: #2b91af">Label </span>label20 = getterILGen.DefineLabel(); <span style="color: green">//
Writing body </span>getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Nop);
getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldarg_0); getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldstr,
attr.Name); getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Call, baseGetterInfo); <span style="color: blue">if </span>(attr.AttributeValueType.IsValueType)
{ getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Unbox_Any, attr.AttributeValueType);
} <span style="color: blue">else </span>{ getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Castclass,
attr.AttributeValueType); } getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Stloc_0);
getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Br_S, label20); getterILGen.MarkLabel(label20);
getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldloc_0); getterILGen.Emit(<span style="color: #2b91af">OpCodes</span>.Ret); <span style="color: blue">return </span>getterBuilder;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
It is a convention that property methods are named as get_ and set_ plus the actual
public property name.  What’s interesting here is the code we must generate to
call the dictionary methods on the base class.  You will note that the method
name is “get_Item”, which I only discovered by using Reflector.  I made the decision
that I wanted I wanted the generated Properties to be strongly typed rather than return
Object.  After emitting the OpCodes for calling the base class’s dictionary get
method, we cast the Object to the type specified by the Attribute used to build this
Property.  When I was first testing this I had properties of type string and
double; the string was working and the double was not.  The Castclass opcode
produces odd results when working with value types, so I found the correct Opcode
for non-reference types which is naturally OpCodes.Unbox.
</p>
        <p>
If you want to become more familiar with what the CLR is <em>really</em> doing, try
writing Reflection Emit versions of some common tasks.
</p>
        <p>
The setter code is straightforward as far as these things go.
</p>
        <pre class="code">
          <span style="color: blue">private static </span>
          <span style="color: #2b91af">MethodBuilder </span>BuildSetter(<span style="color: #2b91af">TypeBuilder </span>typeBuilder, <span style="color: #2b91af">Attribute </span>attr, <span style="color: #2b91af">Type </span>baseType, <span style="color: #2b91af">MethodAttributes </span>getSetAttr)
{ <span style="color: blue">var </span>setterBuilder = typeBuilder.DefineMethod(<span style="color: #a31515">"set_" </span>+
attr.Name, getSetAttr, <span style="color: blue">null</span>, <span style="color: blue">new </span><span style="color: #2b91af">Type</span>[]
{ attr.AttributeValueType }); <span style="color: blue">var </span>setterILGen = setterBuilder.GetILGenerator(); <span style="color: blue">var </span>baseSetterInfo
= baseType.GetMethod(<span style="color: #a31515">"set_Item"</span>, <span style="color: #2b91af">BindingFlags</span>.Instance
| <span style="color: #2b91af">BindingFlags</span>.Public | <span style="color: #2b91af">BindingFlags</span>.NonPublic, <span style="color: blue">null</span>, <span style="color: blue">new </span><span style="color: #2b91af">Type</span>[]
{ <span style="color: blue">typeof</span>(<span style="color: blue">string</span>), <span style="color: blue">typeof</span>(<span style="color: blue">object</span>)
}, <span style="color: blue">null</span>); setterBuilder.SetReturnType(<span style="color: blue">typeof</span>(<span style="color: blue">void</span>));
setterBuilder.SetParameters(attr.AttributeValueType); <span style="color: green">//
Parameter name=value </span><span style="color: #2b91af">ParameterBuilder </span>value
= setterBuilder.DefineParameter(1, <span style="color: #2b91af">ParameterAttributes</span>.None, <span style="color: #a31515">"value"</span>); <span style="color: #2b91af">ILGenerator </span>gen
= setterBuilder.GetILGenerator(); <span style="color: green">// Writing body </span>gen.Emit(<span style="color: #2b91af">OpCodes</span>.Nop);
gen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldarg_0); gen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldstr,
attr.Name); gen.Emit(<span style="color: #2b91af">OpCodes</span>.Ldarg_1); gen.Emit(<span style="color: #2b91af">OpCodes</span>.Call,
baseSetterInfo); gen.Emit(<span style="color: #2b91af">OpCodes</span>.Nop); gen.Emit(<span style="color: #2b91af">OpCodes</span>.Ret); <span style="color: blue">return </span>setterBuilder;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
 
</p>
        <h2>Proving it Out
</h2>
        <p>
With a first draft of the reflection code written, I wanted to write a simple UI to
smoke test three obvious Data Binding scenarios: 
</p>
        <ol>
          <li>
            <font face="Verdana">Basic binding to a dynamically generated property </font>
          </li>
          <li>
            <font face="Verdana">Invoking code manually to update a property to make sure update
notifications are firing</font>
          </li>
          <li>
            <font face="Verdana">Change a value using a Control of some kind to make sure TwoWay
binding is updating the instance</font> </li>
        </ol>
        <p>
These goals can be met with a simple and unglamorous user interface:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Grid </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="LayoutRoot" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="450" </span>
          <span style="color: red">HorizontalAlignment</span>
          <span style="color: blue">="Left"&gt;
&lt;</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">FontSize</span>
          <span style="color: blue">="30" </span>
          <span style="color: red">FontFamily</span>
          <span style="color: blue">="Verdana"&gt;</span>
          <span style="color: #a31515">Things
that I have time for:</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Text</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Code</span>
          <span style="color: blue">}"&gt;&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBox </span>
          <span style="color: red">Text</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Wine</span>
          <span style="color: blue">, </span>
          <span style="color: red">Mode</span>
          <span style="color: blue">=TwoWay}"&gt;&lt;/</span>
          <span style="color: #a31515">TextBox</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Border </span>
          <span style="color: red">CornerRadius</span>
          <span style="color: blue">="5" </span>
          <span style="color: red">BorderBrush</span>
          <span style="color: blue">="Red" </span>
          <span style="color: red">BorderThickness</span>
          <span style="color: blue">="2"&gt;
&lt;</span>
          <span style="color: #a31515">Border.Child</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Text</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Wine</span>
          <span style="color: blue">}"&gt;&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Border.Child</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Border</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="ChangeBtn" </span>
          <span style="color: red">Click</span>
          <span style="color: blue">="ChangeBtn_Click" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="Change"&gt;&lt;/</span>
          <span style="color: #a31515">Button</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">StackPanel </span>
          <span style="color: red">Orientation</span>
          <span style="color: blue">="Horizontal"&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;</span>
          <span style="color: #a31515">Money(double)</span>
          <span style="color: blue">&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">TextBlock </span>
          <span style="color: red">Text</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Money</span>
          <span style="color: blue">}"&gt;&lt;/</span>
          <span style="color: #a31515">TextBlock</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Grid</span>
          <span style="color: blue">&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Yes, I’ve identified Code, Wine, and Money (and little else) as things that I have
time for.  You’ve got to write what you know.  In the Loaded event handler
for this basic Control, we can see the Entity Value classes in action.  The first
step is to create a simple model to generate a bindable Type from.
</p>
        <pre class="code">
          <span style="color: blue">private void </span>UserControl_Loaded(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: blue">var </span>model = <span style="color: blue">new </span><span style="color: #2b91af">AttributeViewModel</span>();
model.Add(<span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(0, <span style="color: #a31515">"Wine"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)));
model.Add(<span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(1, <span style="color: #a31515">"Code"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)));
model.Add(<span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(2, <span style="color: #a31515">"Money"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">double</span>)));</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
First we’ll try one object with three attributes.  Creating an instance of the
dynamic type looks like this:
</p>
        <pre class="code">
          <p>
            <span style="color: blue">var </span>t = <span style="color: #2b91af">BindableEVBuilder</span>.GetDecoratorType(model);
_entity = (<span style="color: #2b91af">EntityWithAttributes</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(t);
</p>
          <pre class="code">
            <span style="color: blue">var </span>t = <span style="color: #2b91af">BindableEVBuilder</span>.GetDecoratorType(model);
_entity = (<span style="color: #2b91af">EntityWithAttributes</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(t);
_entity[<span style="color: #a31515">"Wine"</span>] = <span style="color: #a31515">"Chateauneuf
du Pape"</span>; _entity[<span style="color: #a31515">"Code"</span>]
= <span style="color: #a31515">"Silverlight"</span>; _entity[<span style="color: #a31515">"Money"</span>]
= 5555785.0; LayoutRoot.DataContext = _entity;</pre>
          <a href="http://11011.net/software/vspaste">
          </a>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Time for the moment of truth.  Running the code and interacting with the UI yields
the following:
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI0_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="UI0" border="0" alt="UI0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI0_thumb.jpg" width="473" height="149" />
          </a>
        </p>
        <p>
Everything works.  The one time binding “Silverlight” works, I typed Malbec in
the TextBox and it updated the object, the Property changed evens fired, and clicking
the Change button changed the bottom text to 1000000.  My dynamic property system
works.
</p>
        <h3>Going a Little Further
</h3>
        <p>
The basic binding situations work, but what about a slightly more complex scenario? 
Let’s go ahead and test multiple dynamic types, multiple instances, and IValueConverters.
</p>
        <pre class="code">
          <span style="color: blue">void </span>BindGrid() { <span style="color: blue">var </span>carModel
= <span style="color: blue">new </span><span style="color: #2b91af">AttributeViewModel</span>(); <span style="color: blue">var </span>year
= <span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(0, <span style="color: #a31515">"Year"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">int</span>)); <span style="color: blue">var </span>make
= <span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(1, <span style="color: #a31515">"Make"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)); <span style="color: blue">var </span>model
= <span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(2, <span style="color: #a31515">"Model"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)); <span style="color: blue">var </span>series
= <span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(3, <span style="color: #a31515">"Series"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">string</span>)); <span style="color: blue">var </span>msrp
= <span style="color: blue">new </span>HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>(4, <span style="color: #a31515">"MSRP"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">double</span>));
carModel.AddRange( <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;HandWaver.AG.EVModel.<span style="color: #2b91af">Attribute</span>&gt;()
{year,make,model,series, msrp} ); <span style="color: blue">var </span>carType = <span style="color: #2b91af">BindableEVBuilder</span>.GetDecoratorType(carModel); <span style="color: blue">var </span>wrx
= (<span style="color: #2b91af">EntityWithAttributes</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(carType); <span style="color: blue">var </span>s4
= (<span style="color: #2b91af">EntityWithAttributes</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(carType); <span style="color: blue">var </span>s4Prestige
= (<span style="color: #2b91af">EntityWithAttributes</span>)<span style="color: #2b91af">Activator</span>.CreateInstance(carType);
year.SetValue(wrx, 2002); make.SetValue(wrx, <span style="color: #a31515">"Subaru"</span>);
model.SetValue(wrx, <span style="color: #a31515">"Impreza"</span>); series.SetValue(wrx, <span style="color: #a31515">"WRX"</span>);
msrp.SetValue(wrx, 26000.00); year.SetValue(s4, 2010); make.SetValue(s4, <span style="color: #a31515">"Audi"</span>);
model.SetValue(s4, <span style="color: #a31515">"S4"</span>); series.SetValue(s4, <span style="color: #a31515">"Premium
Plus"</span>); msrp.SetValue(s4, 47900.00); year.SetValue(s4Prestige, 2010);
make.SetValue(s4Prestige, <span style="color: #a31515">"Audi"</span>); model.SetValue(s4Prestige, <span style="color: #a31515">"S4"</span>);
series.SetValue(s4Prestige, <span style="color: #a31515">"Prestige"</span>);
msrp.SetValue(s4Prestige, 53000.00); <span style="color: blue">var </span>carList
= <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">EntityWithAttributes</span>&gt;()
{wrx, s4, s4Prestige}; CarGrid.ItemsSource = carList; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
You can see that there’s a bit of code being written to create the instances and set
the values, but we could obviously write more code around that to automate more of
the tedious work.  I will also note that AutoGenerateColumns=”True” did not work
on the DataGrid: I leave that as an exercise for another day.  The resulting
display is exactly what you’d expect:
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI1_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="UI1" border="0" alt="UI1" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI1_thumb.jpg" width="495" height="241" />
          </a>
        </p>
        <p>
 
</p>
        <h2>A little polish
</h2>
        <p>
When first testing this code, I could see that it worked but inspecting the dynamically
generated EntityWithAttributes descendant was producing odd results in the Visual
Studio debugger.  Even trying to add a watch for the base class would show an
error like “Unable to evaluate expression”.  Silly rabbit, I had forgotten to
include debug information in my Reflection Emit code.  This turned out to be
a lot easier than it sounds.  Let’s take a look at the EnsureDynamicAssembly
method that I skipped earlier.
</p>
        <pre class="code">
          <span style="color: gray">/// &lt;summary&gt; /// </span>
          <span style="color: green">Debugger
info found at http://blogs.msdn.com/jmstall/archive/2005/02/03/366429.aspx </span>
          <span style="color: gray">///
&lt;/summary&gt; </span>
          <span style="color: blue">static void </span>EnsureDynamicAssembly()
{ <span style="color: blue">if </span>(<span style="color: blue">null </span>== _dynamicName)
{ <span style="color: blue">var </span>appDomain = <span style="color: #2b91af">AppDomain</span>.CurrentDomain;
_dynamicName = <span style="color: blue">new </span><span style="color: #2b91af">AssemblyName</span>(<span style="color: #a31515">"HandWaver.AG.EVModel.TypeDecorator"</span>);
_aBuilder = appDomain.DefineDynamicAssembly(_dynamicName, <span style="color: #2b91af">AssemblyBuilderAccess</span>.Run); <span style="color: blue">#if </span>DEBUG <span style="color: blue">var </span>debugAttrType
= <span style="color: blue">typeof</span>(<span style="color: #2b91af">DebuggableAttribute</span>); <span style="color: blue">var </span>debugAttrCtor
= debugAttrType.GetConstructor(<span style="color: blue">new </span><span style="color: #2b91af">Type</span>[]
{ <span style="color: blue">typeof</span>(<span style="color: #2b91af">DebuggableAttribute</span>.<span style="color: #2b91af">DebuggingModes</span>)
}); <span style="color: blue">var </span>customAttributeBuilder = <span style="color: blue">new </span><span style="color: #2b91af">CustomAttributeBuilder</span>(debugAttrCtor, <span style="color: blue">new
object</span>[] { <span style="color: #2b91af">DebuggableAttribute</span>.<span style="color: #2b91af">DebuggingModes</span>.DisableOptimizations
| <span style="color: #2b91af">DebuggableAttribute</span>.<span style="color: #2b91af">DebuggingModes</span>.Default
}); _aBuilder.SetCustomAttribute(customAttributeBuilder); <span style="color: blue">#endif
#if </span>DEBUG _moduleBuilder = _aBuilder.DefineDynamicModule(_dynamicName + <span style="color: #a31515">".dll"</span>, <span style="color: blue">true</span>); <span style="color: blue">#else </span><span style="color: gray">_moduleBuilder
= _aBuilder.DefineDynamicModule(_dynamicName + ".dll"); </span><span style="color: blue">#endif </span>}
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I don’t usually use preprocessor directives but this is exactly what they’re for. 
The stuff in between the #if DEBUG statements includes some symbol information in
the dynamic assembly.  There’s more work I could have done to associate the code
to a Document for source step-through, but at least going this far gets my Local/Watch
windows in VS working:
</p>
        <p>
          <a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/WatchWindow_2.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="WatchWindow" border="0" alt="WatchWindow" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/WatchWindow_thumb.jpg" width="644" height="142" />
          </a>
        </p>
        <p>
You can see that I can evaluate properties on the base class and even Reflect the
dynamic type to see that it exposes “real” properties like any other Type.
</p>
        <h2>Conclusion
</h2>
        <p>
There is a saying that every problem in computer science can be solved with <em>one
more</em> level of indirection.  When using the Full .NET Framework it often
seems they’ve thought of everything, even providing an effective layer of indirection
for Reflection in the form of <strong>TypeDescriptor</strong> and friends.  Lacking
TypeDescriptor in Silverlight, it’s up to us in the community to come up with solutions
while we simultaneously pester Microsoft to add more extensibility to the Data Binding
frameworks.
</p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=2582a6a5-ad67-4509-ba3c-cb19d0806cc8" />
      </body>
      <title>TwoWay binding to Name/Value Pairs</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,2582a6a5-ad67-4509-ba3c-cb19d0806cc8.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/TOGK-aAJki4/TwoWayBindingToNameValuePairs.aspx</link>
      <pubDate>Mon, 07 Sep 2009 18:59:55 GMT</pubDate>
      <description>&lt;p&gt;
It is sometimes useful or necessary to represent complex data types as name/value
pairs, or Dictionaries if you like.&amp;#160; While the Data Binding implementation in
Silverlight 3 is admirable, there is not a built in way to do this cleanly.&amp;#160;
Doing some Internet searches, you may find some approaches like this, from &lt;a title="http://silverlight.net/forums/t/51864.aspx" href="http://silverlight.net/forums/t/51864.aspx"&gt;http://silverlight.net/forums/t/51864.aspx&lt;/a&gt; :
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DictionaryItemConverter &lt;/span&gt;:
IValueConverter { &lt;span style="color: blue"&gt;public object &lt;/span&gt;Convert(&lt;span style="color: blue"&gt;object &lt;/span&gt;value, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;targetType, &lt;span style="color: blue"&gt;object &lt;/span&gt;parameter,
CultureInfo culture) { &lt;span style="color: blue"&gt;var &lt;/span&gt;dict = value &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;; &lt;span style="color: blue"&gt;if &lt;/span&gt;(dict
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) { &lt;span style="color: blue"&gt;return &lt;/span&gt;dict[parameter &lt;span style="color: blue"&gt;as
string&lt;/span&gt;]; } &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NotImplementedException&lt;/span&gt;();
} &lt;span style="color: blue"&gt;public object &lt;/span&gt;ConvertBack(&lt;span style="color: blue"&gt;object &lt;/span&gt;value, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;targetType, &lt;span style="color: blue"&gt;object &lt;/span&gt;parameter,
CultureInfo culture) { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NotImplementedException&lt;/span&gt;();
} }&lt;/pre&gt;
&lt;p&gt;
Note that in all cases you will find the “ConvertBack” strategy missing.&amp;#160; Since
the original source object is not passed to ConvertBack, two-way binding to&amp;#160;
name/value pairs is an elusive goal.&amp;#160; &lt;strong&gt;The binding mechanisms in Silverlight
3 are not very extensible&lt;/strong&gt;.&amp;#160; I cannot create my own binding type or provide
a new implementation for Property Paths.&amp;#160; The Silverlight 3 Data Binding framework
wants real Properties to bind to.&amp;#160; If we &lt;em&gt;must&lt;/em&gt; have two way bindings
to name/value pairs, we are left with pursuing more extreme measures.
&lt;/p&gt;
&lt;h2&gt;Dynamic Type Generation
&lt;/h2&gt;
&lt;p&gt;
I have &lt;a href="http://www.damonpayne.com/2008/09/18/RunTimeIsDesignTimeForAGT5.aspx"&gt;written
in the past&lt;/a&gt; about the complete Reflection Emit implementation in Silverlight.&amp;#160;
It seemed to me that given a means of representing the names and types of the items
we want to pull out of a dictionary as Properties, we could dynamically generate a
bindable version of a class represented as a dictionary.&amp;#160; We’ll start with creating
a very simple model for Objects with data represented as name/value pairs.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.EVModel
{ &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Attribute &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;Attribute(&lt;span style="color: blue"&gt;int &lt;/span&gt;id, &lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;t)
{ Id = id; Name = name; AttributeValueType = t; } &lt;span style="color: blue"&gt;public
int &lt;/span&gt;Id { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;AttributeValueType
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This attribute, not to be confused with the .NET Attribute, represents a sort of metadata
for a single property on an object.&amp;#160; We give it a name (which ultimately must
be friendly to becoming a Property name) and a run time type.&amp;#160; A single Attribute
might be the &amp;quot;Name” property of some object with a type of string.&amp;#160; We may
not ultimately want to bind to every single name/value pair available to us.&amp;#160;
A means of representing a certain sub-Model is therefore very helpful.&amp;#160; A List
of Attribute will suffice for this demo.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.EVModel
{ &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AttributeViewModel &lt;/span&gt;:
System.Collections.Generic.&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;&amp;gt;
{ &lt;span style="color: blue"&gt;public &lt;/span&gt;AttributeViewModel() : &lt;span style="color: blue"&gt;this&lt;/span&gt;( &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid()
) { } &lt;span style="color: blue"&gt;public &lt;/span&gt;AttributeViewModel(&lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;g)
{ UniqueId = g; } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Guid &lt;/span&gt;UniqueId
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The AttributeViewModel class exposes a UniqueId property of type Guid, which will
be useful later.
&lt;/p&gt;
&lt;p&gt;
Reflection Emit code, while incredibly powerful, can also be tedious, difficult, and
error prone.&amp;#160; Once practice that I follow is to have as little dynamic code as
possible, calling out to pre built classes and only doing what &lt;em&gt;must &lt;/em&gt;be done
in dynamically created types.&amp;#160; In order to be friendly to binding and provide
a base to extend we’ll use a base class that does most of what we want to do already:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;HandWaver.AG.EVModel
{ &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EntityWithAttributes &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged &lt;/span&gt;{ &lt;span style="color: blue"&gt;public &lt;/span&gt;EntityWithAttributes()
{ _values = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();
} &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;
_values; &lt;span style="color: blue"&gt;public object this&lt;/span&gt;[&lt;span style="color: blue"&gt;string &lt;/span&gt;key]
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_values[key];
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _values[key] = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
OnPropertyChanged(key); } } &lt;span style="color: blue"&gt;protected void &lt;/span&gt;OnPropertyChanged(&lt;span style="color: blue"&gt;string &lt;/span&gt;propName)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;null &lt;/span&gt;!= PropertyChanged)
{ PropertyChanged(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs&lt;/span&gt;(propName));
} } &lt;span style="color: blue"&gt;public event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler &lt;/span&gt;PropertyChanged;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Next, we’ll build a class to create the dynamic types.&amp;#160; I’m currently calling
this a Bindable Entity-Value Builder.&amp;#160; It’s worth going over the Reflection Emit
code in some detail.&amp;#160; The public interface for BindableEVBuilder is a single
method:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;GetDecoratorType(&lt;span style="color: #2b91af"&gt;AttributeViewModel &lt;/span&gt;prototype)
{ EnsureDynamicAssembly(); EnsureModelTypes(); &lt;span style="color: blue"&gt;if &lt;/span&gt;(!_modelTypes.ContainsKey(prototype.UniqueId))
{ BuildDecoratorType(prototype); } &lt;span style="color: blue"&gt;return &lt;/span&gt;_modelTypes[prototype.UniqueId];
}&lt;/pre&gt;
&lt;p&gt;
We can look at the first several methods later.&amp;#160; The bulk of our work is done
in BuildDecoratorType.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;BuildDecoratorType(&lt;span style="color: #2b91af"&gt;AttributeViewModel &lt;/span&gt;prototype)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;typeBuilder = _moduleBuilder.DefineType(&lt;span style="color: #a31515"&gt;&amp;quot;HandWaver.AG.EVModel.TypeDecorator.DictWrapper&amp;quot;&lt;/span&gt;+
GetName(prototype.UniqueId), &lt;span style="color: #2b91af"&gt;TypeAttributes&lt;/span&gt;.Public
| &lt;span style="color: #2b91af"&gt;TypeAttributes&lt;/span&gt;.Serializable | &lt;span style="color: #2b91af"&gt;TypeAttributes&lt;/span&gt;.Class, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)); &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;attribute &lt;span style="color: blue"&gt;in &lt;/span&gt;prototype)
{ BuildProperty(typeBuilder, attribute); } _modelTypes.Add(prototype.UniqueId, typeBuilder.CreateType());
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
So far there’s nothing clever here, just creating a dynamic class that extends EntityWithAttributes
and making sure that we only generate one dynamic type for each AttributeViewModel
by coming up with a naming scheme.&amp;#160; Properties are somewhat special in the .NET
CLR.&amp;#160; In order to create a property using IL, the property itself must be defined
as well as a getter and setter method.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;BuildProperty(&lt;span style="color: #2b91af"&gt;TypeBuilder &lt;/span&gt;typeBuilder, &lt;span style="color: #2b91af"&gt;Attribute &lt;/span&gt;attr)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;baseType = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;); &lt;span style="color: blue"&gt;var &lt;/span&gt;propertyBuilder
= typeBuilder.DefineProperty(attr.Name, &lt;span style="color: #2b91af"&gt;PropertyAttributes&lt;/span&gt;.None,
attr.AttributeValueType, &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: green"&gt;//
The property set and property get methods require a special // set of attributes. &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodAttributes &lt;/span&gt;getSetAttr
= &lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.SpecialName
| &lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.HideBySig; &lt;span style="color: blue"&gt;var &lt;/span&gt;getterBuilder
= BuildGetter(typeBuilder, attr, baseType, getSetAttr); &lt;span style="color: blue"&gt;var &lt;/span&gt;setterBuilder
= BuildSetter(typeBuilder, attr, baseType, getSetAttr); propertyBuilder.SetGetMethod(getterBuilder);
propertyBuilder.SetSetMethod(setterBuilder); }&lt;/pre&gt;
&lt;p&gt;
The getter is more interesting than the setter in this situation.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodBuilder &lt;/span&gt;BuildGetter(&lt;span style="color: #2b91af"&gt;TypeBuilder &lt;/span&gt;typeBuilder, &lt;span style="color: #2b91af"&gt;Attribute &lt;/span&gt;attr, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;baseType, &lt;span style="color: #2b91af"&gt;MethodAttributes &lt;/span&gt;getSetAttr)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;getterBuilder = typeBuilder.DefineMethod(&lt;span style="color: #a31515"&gt;&amp;quot;get_&amp;quot; &lt;/span&gt;+
attr.Name, getSetAttr, attr.AttributeValueType, &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.EmptyTypes); &lt;span style="color: blue"&gt;var &lt;/span&gt;getterILGen
= getterBuilder.GetILGenerator(); &lt;span style="color: blue"&gt;var &lt;/span&gt;baseGetterInfo
= baseType.GetMethod(&lt;span style="color: #a31515"&gt;&amp;quot;get_Item&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance
| &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic, &lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[]
{ &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)
}, &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;LocalBuilder &lt;/span&gt;localValBuilder
= getterILGen.DeclareLocal(attr.AttributeValueType); &lt;span style="color: green"&gt;//
Preparing labels &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Label &lt;/span&gt;label20 = getterILGen.DefineLabel(); &lt;span style="color: green"&gt;//
Writing body &lt;/span&gt;getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Nop);
getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_0); getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldstr,
attr.Name); getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Call, baseGetterInfo); &lt;span style="color: blue"&gt;if &lt;/span&gt;(attr.AttributeValueType.IsValueType)
{ getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Unbox_Any, attr.AttributeValueType);
} &lt;span style="color: blue"&gt;else &lt;/span&gt;{ getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Castclass,
attr.AttributeValueType); } getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Stloc_0);
getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Br_S, label20); getterILGen.MarkLabel(label20);
getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldloc_0); getterILGen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ret); &lt;span style="color: blue"&gt;return &lt;/span&gt;getterBuilder;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
It is a convention that property methods are named as get_ and set_ plus the actual
public property name.&amp;#160; What’s interesting here is the code we must generate to
call the dictionary methods on the base class.&amp;#160; You will note that the method
name is “get_Item”, which I only discovered by using Reflector.&amp;#160; I made the decision
that I wanted I wanted the generated Properties to be strongly typed rather than return
Object.&amp;#160; After emitting the OpCodes for calling the base class’s dictionary get
method, we cast the Object to the type specified by the Attribute used to build this
Property.&amp;#160; When I was first testing this I had properties of type string and
double; the string was working and the double was not.&amp;#160; The Castclass opcode
produces odd results when working with value types, so I found the correct Opcode
for non-reference types which is naturally OpCodes.Unbox.
&lt;/p&gt;
&lt;p&gt;
If you want to become more familiar with what the CLR is &lt;em&gt;really&lt;/em&gt; doing, try
writing Reflection Emit versions of some common tasks.
&lt;/p&gt;
&lt;p&gt;
The setter code is straightforward as far as these things go.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodBuilder &lt;/span&gt;BuildSetter(&lt;span style="color: #2b91af"&gt;TypeBuilder &lt;/span&gt;typeBuilder, &lt;span style="color: #2b91af"&gt;Attribute &lt;/span&gt;attr, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;baseType, &lt;span style="color: #2b91af"&gt;MethodAttributes &lt;/span&gt;getSetAttr)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;setterBuilder = typeBuilder.DefineMethod(&lt;span style="color: #a31515"&gt;&amp;quot;set_&amp;quot; &lt;/span&gt;+
attr.Name, getSetAttr, &lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[]
{ attr.AttributeValueType }); &lt;span style="color: blue"&gt;var &lt;/span&gt;setterILGen = setterBuilder.GetILGenerator(); &lt;span style="color: blue"&gt;var &lt;/span&gt;baseSetterInfo
= baseType.GetMethod(&lt;span style="color: #a31515"&gt;&amp;quot;set_Item&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance
| &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic, &lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[]
{ &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;), &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;)
}, &lt;span style="color: blue"&gt;null&lt;/span&gt;); setterBuilder.SetReturnType(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;void&lt;/span&gt;));
setterBuilder.SetParameters(attr.AttributeValueType); &lt;span style="color: green"&gt;//
Parameter name=value &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ParameterBuilder &lt;/span&gt;value
= setterBuilder.DefineParameter(1, &lt;span style="color: #2b91af"&gt;ParameterAttributes&lt;/span&gt;.None, &lt;span style="color: #a31515"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;ILGenerator &lt;/span&gt;gen
= setterBuilder.GetILGenerator(); &lt;span style="color: green"&gt;// Writing body &lt;/span&gt;gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Nop);
gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_0); gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldstr,
attr.Name); gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_1); gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Call,
baseSetterInfo); gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Nop); gen.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ret); &lt;span style="color: blue"&gt;return &lt;/span&gt;setterBuilder;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;Proving it Out
&lt;/h2&gt;
&lt;p&gt;
With a first draft of the reflection code written, I wanted to write a simple UI to
smoke test three obvious Data Binding scenarios: 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;Basic binding to a dynamically generated property &lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;Invoking code manually to update a property to make sure update
notifications are firing&lt;/font&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;Change a value using a Control of some kind to make sure TwoWay
binding is updating the instance&lt;/font&gt;&amp;#160;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
These goals can be met with a simple and unglamorous user interface:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;LayoutRoot&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;450&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;HorizontalAlignment&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Left&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;FontSize&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;30&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;FontFamily&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Verdana&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Things
that I have time for:&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Code&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Wine&lt;/span&gt;&lt;span style="color: blue"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;Mode&lt;/span&gt;&lt;span style="color: blue"&gt;=TwoWay}&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Border &lt;/span&gt;&lt;span style="color: red"&gt;CornerRadius&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;5&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;BorderBrush&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Red&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;BorderThickness&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;2&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Border.Child&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Wine&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Border.Child&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Border&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ChangeBtn&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Click&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ChangeBtn_Click&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Change&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Horizontal&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Money(double)&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Money&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Grid&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Yes, I’ve identified Code, Wine, and Money (and little else) as things that I have
time for.&amp;#160; You’ve got to write what you know.&amp;#160; In the Loaded event handler
for this basic Control, we can see the Entity Value classes in action.&amp;#160; The first
step is to create a simple model to generate a bindable Type from.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;UserControl_Loaded(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;model = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AttributeViewModel&lt;/span&gt;();
model.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(0, &lt;span style="color: #a31515"&gt;&amp;quot;Wine&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)));
model.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(1, &lt;span style="color: #a31515"&gt;&amp;quot;Code&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)));
model.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(2, &lt;span style="color: #a31515"&gt;&amp;quot;Money&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;double&lt;/span&gt;)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
First we’ll try one object with three attributes.&amp;#160; Creating an instance of the
dynamic type looks like this:
&lt;/p&gt;
&lt;pre class="code"&gt;
&lt;p&gt;
&lt;span style="color: blue"&gt;var &lt;/span&gt;t = &lt;span style="color: #2b91af"&gt;BindableEVBuilder&lt;/span&gt;.GetDecoratorType(model);
_entity = (&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(t);
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;t = &lt;span style="color: #2b91af"&gt;BindableEVBuilder&lt;/span&gt;.GetDecoratorType(model);
_entity = (&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(t);
_entity[&lt;span style="color: #a31515"&gt;&amp;quot;Wine&amp;quot;&lt;/span&gt;] = &lt;span style="color: #a31515"&gt;&amp;quot;Chateauneuf
du Pape&amp;quot;&lt;/span&gt;; _entity[&lt;span style="color: #a31515"&gt;&amp;quot;Code&amp;quot;&lt;/span&gt;]
= &lt;span style="color: #a31515"&gt;&amp;quot;Silverlight&amp;quot;&lt;/span&gt;; _entity[&lt;span style="color: #a31515"&gt;&amp;quot;Money&amp;quot;&lt;/span&gt;]
= 5555785.0; LayoutRoot.DataContext = _entity;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Time for the moment of truth.&amp;#160; Running the code and interacting with the UI yields
the following:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI0_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="UI0" border="0" alt="UI0" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI0_thumb.jpg" width="473" height="149" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Everything works.&amp;#160; The one time binding “Silverlight” works, I typed Malbec in
the TextBox and it updated the object, the Property changed evens fired, and clicking
the Change button changed the bottom text to 1000000.&amp;#160; My dynamic property system
works.
&lt;/p&gt;
&lt;h3&gt;Going a Little Further
&lt;/h3&gt;
&lt;p&gt;
The basic binding situations work, but what about a slightly more complex scenario?&amp;#160;
Let’s go ahead and test multiple dynamic types, multiple instances, and IValueConverters.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;BindGrid() { &lt;span style="color: blue"&gt;var &lt;/span&gt;carModel
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AttributeViewModel&lt;/span&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;year
= &lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(0, &lt;span style="color: #a31515"&gt;&amp;quot;Year&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;)); &lt;span style="color: blue"&gt;var &lt;/span&gt;make
= &lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(1, &lt;span style="color: #a31515"&gt;&amp;quot;Make&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)); &lt;span style="color: blue"&gt;var &lt;/span&gt;model
= &lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(2, &lt;span style="color: #a31515"&gt;&amp;quot;Model&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)); &lt;span style="color: blue"&gt;var &lt;/span&gt;series
= &lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(3, &lt;span style="color: #a31515"&gt;&amp;quot;Series&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;)); &lt;span style="color: blue"&gt;var &lt;/span&gt;msrp
= &lt;span style="color: blue"&gt;new &lt;/span&gt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;(4, &lt;span style="color: #a31515"&gt;&amp;quot;MSRP&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;double&lt;/span&gt;));
carModel.AddRange( &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;HandWaver.AG.EVModel.&lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;&amp;gt;()
{year,make,model,series, msrp} ); &lt;span style="color: blue"&gt;var &lt;/span&gt;carType = &lt;span style="color: #2b91af"&gt;BindableEVBuilder&lt;/span&gt;.GetDecoratorType(carModel); &lt;span style="color: blue"&gt;var &lt;/span&gt;wrx
= (&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(carType); &lt;span style="color: blue"&gt;var &lt;/span&gt;s4
= (&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(carType); &lt;span style="color: blue"&gt;var &lt;/span&gt;s4Prestige
= (&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(carType);
year.SetValue(wrx, 2002); make.SetValue(wrx, &lt;span style="color: #a31515"&gt;&amp;quot;Subaru&amp;quot;&lt;/span&gt;);
model.SetValue(wrx, &lt;span style="color: #a31515"&gt;&amp;quot;Impreza&amp;quot;&lt;/span&gt;); series.SetValue(wrx, &lt;span style="color: #a31515"&gt;&amp;quot;WRX&amp;quot;&lt;/span&gt;);
msrp.SetValue(wrx, 26000.00); year.SetValue(s4, 2010); make.SetValue(s4, &lt;span style="color: #a31515"&gt;&amp;quot;Audi&amp;quot;&lt;/span&gt;);
model.SetValue(s4, &lt;span style="color: #a31515"&gt;&amp;quot;S4&amp;quot;&lt;/span&gt;); series.SetValue(s4, &lt;span style="color: #a31515"&gt;&amp;quot;Premium
Plus&amp;quot;&lt;/span&gt;); msrp.SetValue(s4, 47900.00); year.SetValue(s4Prestige, 2010);
make.SetValue(s4Prestige, &lt;span style="color: #a31515"&gt;&amp;quot;Audi&amp;quot;&lt;/span&gt;); model.SetValue(s4Prestige, &lt;span style="color: #a31515"&gt;&amp;quot;S4&amp;quot;&lt;/span&gt;);
series.SetValue(s4Prestige, &lt;span style="color: #a31515"&gt;&amp;quot;Prestige&amp;quot;&lt;/span&gt;);
msrp.SetValue(s4Prestige, 53000.00); &lt;span style="color: blue"&gt;var &lt;/span&gt;carList
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;EntityWithAttributes&lt;/span&gt;&amp;gt;()
{wrx, s4, s4Prestige}; CarGrid.ItemsSource = carList; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
You can see that there’s a bit of code being written to create the instances and set
the values, but we could obviously write more code around that to automate more of
the tedious work.&amp;#160; I will also note that AutoGenerateColumns=”True” did not work
on the DataGrid: I leave that as an exercise for another day.&amp;#160; The resulting
display is exactly what you’d expect:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI1_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="UI1" border="0" alt="UI1" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/UI1_thumb.jpg" width="495" height="241" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;h2&gt;A little polish
&lt;/h2&gt;
&lt;p&gt;
When first testing this code, I could see that it worked but inspecting the dynamically
generated EntityWithAttributes descendant was producing odd results in the Visual
Studio debugger.&amp;#160; Even trying to add a watch for the base class would show an
error like “Unable to evaluate expression”.&amp;#160; Silly rabbit, I had forgotten to
include debug information in my Reflection Emit code.&amp;#160; This turned out to be
a lot easier than it sounds.&amp;#160; Let’s take a look at the EnsureDynamicAssembly
method that I skipped earlier.
&lt;/p&gt;
&lt;pre class="code"&gt;        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Debugger
info found at http://blogs.msdn.com/jmstall/archive/2005/02/03/366429.aspx &lt;/span&gt;&lt;span style="color: gray"&gt;///
&amp;lt;/summary&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;EnsureDynamicAssembly()
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;null &lt;/span&gt;== _dynamicName)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;appDomain = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain;
_dynamicName = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AssemblyName&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;HandWaver.AG.EVModel.TypeDecorator&amp;quot;&lt;/span&gt;);
_aBuilder = appDomain.DefineDynamicAssembly(_dynamicName, &lt;span style="color: #2b91af"&gt;AssemblyBuilderAccess&lt;/span&gt;.Run); &lt;span style="color: blue"&gt;#if &lt;/span&gt;DEBUG &lt;span style="color: blue"&gt;var &lt;/span&gt;debugAttrType
= &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DebuggableAttribute&lt;/span&gt;); &lt;span style="color: blue"&gt;var &lt;/span&gt;debugAttrCtor
= debugAttrType.GetConstructor(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[]
{ &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DebuggableAttribute&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;DebuggingModes&lt;/span&gt;)
}); &lt;span style="color: blue"&gt;var &lt;/span&gt;customAttributeBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomAttributeBuilder&lt;/span&gt;(debugAttrCtor, &lt;span style="color: blue"&gt;new
object&lt;/span&gt;[] { &lt;span style="color: #2b91af"&gt;DebuggableAttribute&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;DebuggingModes&lt;/span&gt;.DisableOptimizations
| &lt;span style="color: #2b91af"&gt;DebuggableAttribute&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;DebuggingModes&lt;/span&gt;.Default
}); _aBuilder.SetCustomAttribute(customAttributeBuilder); &lt;span style="color: blue"&gt;#endif
#if &lt;/span&gt;DEBUG _moduleBuilder = _aBuilder.DefineDynamicModule(_dynamicName + &lt;span style="color: #a31515"&gt;&amp;quot;.dll&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;); &lt;span style="color: blue"&gt;#else &lt;/span&gt;&lt;span style="color: gray"&gt;_moduleBuilder
= _aBuilder.DefineDynamicModule(_dynamicName + &amp;quot;.dll&amp;quot;); &lt;/span&gt;&lt;span style="color: blue"&gt;#endif &lt;/span&gt;}
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I don’t usually use preprocessor directives but this is exactly what they’re for.&amp;#160;
The stuff in between the #if DEBUG statements includes some symbol information in
the dynamic assembly.&amp;#160; There’s more work I could have done to associate the code
to a Document for source step-through, but at least going this far gets my Local/Watch
windows in VS working:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/WatchWindow_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="WatchWindow" border="0" alt="WatchWindow" src="http://www.damonpayne.com/content/binary/WindowsLiveWriter/TwoWaybindingtoNameValuePairs_13697/WatchWindow_thumb.jpg" width="644" height="142" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
You can see that I can evaluate properties on the base class and even Reflect the
dynamic type to see that it exposes “real” properties like any other Type.
&lt;/p&gt;
&lt;h2&gt;Conclusion
&lt;/h2&gt;
&lt;p&gt;
There is a saying that every problem in computer science can be solved with &lt;em&gt;one
more&lt;/em&gt; level of indirection.&amp;#160; When using the Full .NET Framework it often
seems they’ve thought of everything, even providing an effective layer of indirection
for Reflection in the form of &lt;strong&gt;TypeDescriptor&lt;/strong&gt; and friends.&amp;#160; Lacking
TypeDescriptor in Silverlight, it’s up to us in the community to come up with solutions
while we simultaneously pester Microsoft to add more extensibility to the Data Binding
frameworks.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=2582a6a5-ad67-4509-ba3c-cb19d0806cc8" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,2582a6a5-ad67-4509-ba3c-cb19d0806cc8.aspx</comments>
      <category>Architecture and Design</category>
      <category>By Request</category>
      <category>Silverlight</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/09/07/TwoWayBindingToNameValuePairs.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://www.damonpayne.com/Trackback.aspx?guid=7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8</trackback:ping>
      <pingback:server>http://www.damonpayne.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.damonpayne.com/PermaLink,guid,7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8.aspx</pingback:target>
      <dc:creator>Damon Payne</dc:creator>
      <wfw:comment>http://www.damonpayne.com/CommentView,guid,7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8.aspx</wfw:comment>
      <wfw:commentRss>http://www.damonpayne.com/SyndicationService.asmx/GetEntryCommentsRss?guid=7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have been happily using the Unity container for over a year now.  I had previously
written my own IoC framework in Silverlight and used some other off the shelf and
custom home-grown containers.  I have recently been getting started using MEF
in Silverlight as part of some up and coming Patterns articles.
</p>
        <p>
When you scratch the surface of MEF, I’ll warn you that you may be Underwhelmed. 
What, exactly, is this?  The Ultimate IoC container?  What is this doing
for me that Unity wasn’t already doing for me?  In fact, you can implement the
IUnityContainer interface with MEF so what’s the big deal?
</p>
        <p>
Is the big deal…
</p>
        <ul>
          <li>
            <font face="Verdana">That MEF is the Visual Studio 2010 extensibility mechanism? 
No. Microsoft has claimed that they <em>needed</em> MEF for internal efforts like
this.  I believe they are earnest, that this is not just a public “Look at us
eating our own dogfood” showing.  Still, Microsoft <strong>needed</strong> the
Entity Relation model beneath the Entity Framework and we saw how well <em>that</em> was
received by the community.</font>
          </li>
          <li>
            <font face="Verdana">That MEF works in Silverlight?  No.  Other IoC containers
were ported to Silverlight 2 long before Unity or MEF.  We’ve been able to use
the same container for Silverlight/WPF/ASP.net/Winforms/Console apps for a long time.</font>
          </li>
          <li>
            <font face="Verdana">That MEF has cool features like Catalogues, Dynamic Recomposition,
Lazy&lt;T&gt; ? No.  These features <em>are</em> cool and I plan on exploring
them further and talking about them to anyone who will listen.</font>
          </li>
        </ul>
        <p>
          <strong>No, the big deal is that MEF is shipping with .NET 4.</strong>
        </p>
        <p>
So what, you say? Let me explain.  A lot of you probably listen to <a href="http://www.dotnetrocks.com/">.NET
Rocks</a>.  I do, I enjoy it.  The hosts are funny, the guests are bright,
and I get bite-sized updates on what is going on out there in areas of the development
universe that are important to me but not so important as to be in my overloaded RSS
list.  .NET rocks succeeds for various reasons and to hone in on one of the big
whys let’s take a look at one possible way to group developers:
</p>
        <ol>
          <li>
            <font face="Verdana">Brand new developers.</font> </li>
          <li>
Developers who are lazy clock-punchers who don’t care about their craft whatsoever. 
Enough said about these folks.</li>
          <li>
Developers who know enough to know there are better ways.  They struggle within
the bureaucracy.  They’ve heard testing is good, but why?  They feel they
probably need abstractions, but where?  Some don’t <em>quite </em>have the motivation
or the skills the find the answers themselves; some have never been afforded the opportunity
to break out.</li>
          <li>
Developers who think they have all of the answers but are full of $(‘.crap’).</li>
          <li>
Developers who mostly “get it”, who have the motivation and skills to find the answers,
refine their craft, and make things better.</li>
        </ol>
        <p>
          <font face="Arial">Based on my own experience Group #3 makes up an awful large percentage
of the developer world.  A lot of the folks in Group #5 start here until something
snaps within them and they decide to stop playing WoW and get serious about figuring
out what it takes to get better.   These #3 folks want to learn but the
lure of American Idol keeps them from catching up on those blogs.  They want
to try a new pattern but the Architecture Police have decreed that they must <em>never
do X </em></font>and <em>always do Z</em>.  Maybe the political war required
to get a new library into the solution to try a different way kills any initiative. 
Maybe their employer never springs for training.  More often than not, the high
turnover in IT isolates developers from the long term perspective that would otherwise
be a great teacher.  Which abstractions worked and which turned out to be nightmares
or at best simply unnecessary?  Who knows.  We don’t go back and ask because
we’re afraid of the answers, and to be fair we all hate each others’ code anyway.
</p>
        <p>
The .NET rocks show and things like it is a little beacon of light, a reminder that
some people are really doing things right out there, and staying on the cutting edge,
and delivering a lot of value.   But, Damon, how does this relate back to
MEF?  <u>Because Group #3 is looking for guidance</u>.  How many guests
have debated which Data Access technology Microsoft “wants us to use” ?  Do they
want me to use LINQ to SQL or Entity Framework?  Yes, the good folks behind door
#3 who are in the .NET space overwhelming look to Microsoft for that guidance. 
When Microsoft showed creating SQL DataAdapters inside button click event handlers
that’s what they did.  When Microsoft said Stored Procedures are <u>the way</u> that’s
what they built.  When Microsoft said SOA was good we learned SOAP.  When
Microsoft endorsed jQuery we learned jQuery.  
</p>
        <p>
By putting MEF in the .NET framework the community of well-meaning folks who want
to do better are going to see a new message, some new guidance.  “<em>Microsoft
wants me to think of my applications differently</em>.”  Microsoft is telling
me that my apps should be <em>composed</em> out of <em>modules</em>, perhaps modules
hidden behind interfaces.  I should worry about the physical location of a module
or its implementation <em>later</em> and program against abstractions.  When
I write a class I should think about what the responsibility of this class is and
what its dependencies are.  Microsoft is telling me to get onboard with loose
coupling.  I don’t need to petition my overlords to bring in a new framework
because the only thing between me and MEF is: 
</p>
        <pre class="code">
          <strong>
            <span style="color: blue">using </span>System.ComponentModel.Composition;</strong>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Microsoft has always been a Platform company.  I like my Zune, the Xbox is cool,
I’m a PC, and maybe the Microsoft Stores will be cool.  But let’s be real: what
Microsoft does better than anything else is to build something that other people use
to build something.  By including MEF in the core toolbox Microsoft is getting
behind a lot of solid development ideas and removing another barrier between the Average
Corporate Developer and <em><strong>Better</strong></em>.
</p>
        <p>
          <font face="Arial">
          </font>
        </p>
        <p>
          <font face="Arial"> </font>
        </p>
        <img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8" />
      </body>
      <title>Why MEF Matters</title>
      <guid isPermaLink="false">http://www.damonpayne.com/PermaLink,guid,7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8.aspx</guid>
      <link>http://feedproxy.google.com/~r/DamonPayne/~3/mUXHj_pww0Q/WhyMEFMatters.aspx</link>
      <pubDate>Sun, 16 Aug 2009 01:30:59 GMT</pubDate>
      <description>&lt;p&gt;
I have been happily using the Unity container for over a year now.&amp;#160; I had previously
written my own IoC framework in Silverlight and used some other off the shelf and
custom home-grown containers.&amp;#160; I have recently been getting started using MEF
in Silverlight as part of some up and coming Patterns articles.
&lt;/p&gt;
&lt;p&gt;
When you scratch the surface of MEF, I’ll warn you that you may be Underwhelmed.&amp;#160;
What, exactly, is this?&amp;#160; The Ultimate IoC container?&amp;#160; What is this doing
for me that Unity wasn’t already doing for me?&amp;#160; In fact, you can implement the
IUnityContainer interface with MEF so what’s the big deal?
&lt;/p&gt;
&lt;p&gt;
Is the big deal…
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;That MEF is the Visual Studio 2010 extensibility mechanism?&amp;#160;
No. Microsoft has claimed that they &lt;em&gt;needed&lt;/em&gt; MEF for internal efforts like
this.&amp;#160; I believe they are earnest, that this is not just a public “Look at us
eating our own dogfood” showing.&amp;#160; Still, Microsoft &lt;strong&gt;needed&lt;/strong&gt; the
Entity Relation model beneath the Entity Framework and we saw how well &lt;em&gt;that&lt;/em&gt; was
received by the community.&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;That MEF works in Silverlight?&amp;#160; No.&amp;#160; Other IoC containers
were ported to Silverlight 2 long before Unity or MEF.&amp;#160; We’ve been able to use
the same container for Silverlight/WPF/ASP.net/Winforms/Console apps for a long time.&lt;/font&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;That MEF has cool features like Catalogues, Dynamic Recomposition,
Lazy&amp;lt;T&amp;gt; ? No.&amp;#160; These features &lt;em&gt;are&lt;/em&gt; cool and I plan on exploring
them further and talking about them to anyone who will listen.&lt;/font&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;No, the big deal is that MEF is shipping with .NET 4.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So what, you say? Let me explain.&amp;#160; A lot of you probably listen to &lt;a href="http://www.dotnetrocks.com/"&gt;.NET
Rocks&lt;/a&gt;.&amp;#160; I do, I enjoy it.&amp;#160; The hosts are funny, the guests are bright,
and I get bite-sized updates on what is going on out there in areas of the development
universe that are important to me but not so important as to be in my overloaded RSS
list.&amp;#160; .NET rocks succeeds for various reasons and to hone in on one of the big
whys let’s take a look at one possible way to group developers:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;font face="Verdana"&gt;Brand new developers.&lt;/font&gt;&amp;#160;&lt;/li&gt;
&lt;li&gt;
Developers who are lazy clock-punchers who don’t care about their craft whatsoever.&amp;#160;
Enough said about these folks.&lt;/li&gt;
&lt;li&gt;
Developers who know enough to know there are better ways.&amp;#160; They struggle within
the bureaucracy.&amp;#160; They’ve heard testing is good, but why?&amp;#160; They feel they
probably need abstractions, but where?&amp;#160; Some don’t &lt;em&gt;quite &lt;/em&gt;have the motivation
or the skills the find the answers themselves; some have never been afforded the opportunity
to break out.&lt;/li&gt;
&lt;li&gt;
Developers who think they have all of the answers but are full of $(‘.crap’).&lt;/li&gt;
&lt;li&gt;
Developers who mostly “get it”, who have the motivation and skills to find the answers,
refine their craft, and make things better.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;font face="Arial"&gt;Based on my own experience Group #3 makes up an awful large percentage
of the developer world.&amp;#160; A lot of the folks in Group #5 start here until something
snaps within them and they decide to stop playing WoW and get serious about figuring
out what it takes to get better.&amp;#160;&amp;#160; These #3 folks want to learn but the
lure of American Idol keeps them from catching up on those blogs.&amp;#160; They want
to try a new pattern but the Architecture Police have decreed that they must &lt;em&gt;never
do X &lt;/em&gt;&lt;/font&gt;and &lt;em&gt;always do Z&lt;/em&gt;.&amp;#160; Maybe the political war required
to get a new library into the solution to try a different way kills any initiative.&amp;#160;
Maybe their employer never springs for training.&amp;#160; More often than not, the high
turnover in IT isolates developers from the long term perspective that would otherwise
be a great teacher.&amp;#160; Which abstractions worked and which turned out to be nightmares
or at best simply unnecessary?&amp;#160; Who knows.&amp;#160; We don’t go back and ask because
we’re afraid of the answers, and to be fair we all hate each others’ code anyway.
&lt;/p&gt;
&lt;p&gt;
The .NET rocks show and things like it is a little beacon of light, a reminder that
some people are really doing things right out there, and staying on the cutting edge,
and delivering a lot of value.&amp;#160;&amp;#160; But, Damon, how does this relate back to
MEF?&amp;#160; &lt;u&gt;Because Group #3 is looking for guidance&lt;/u&gt;.&amp;#160; How many guests
have debated which Data Access technology Microsoft “wants us to use” ?&amp;#160; Do they
want me to use LINQ to SQL or Entity Framework?&amp;#160; Yes, the good folks behind door
#3 who are in the .NET space overwhelming look to Microsoft for that guidance.&amp;#160;
When Microsoft showed creating SQL DataAdapters inside button click event handlers
that’s what they did.&amp;#160; When Microsoft said Stored Procedures are &lt;u&gt;the way&lt;/u&gt; that’s
what they built.&amp;#160; When Microsoft said SOA was good we learned SOAP.&amp;#160; When
Microsoft endorsed jQuery we learned jQuery.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
By putting MEF in the .NET framework the community of well-meaning folks who want
to do better are going to see a new message, some new guidance.&amp;#160; “&lt;em&gt;Microsoft
wants me to think of my applications differently&lt;/em&gt;.”&amp;#160; Microsoft is telling
me that my apps should be &lt;em&gt;composed&lt;/em&gt; out of &lt;em&gt;modules&lt;/em&gt;, perhaps modules
hidden behind interfaces.&amp;#160; I should worry about the physical location of a module
or its implementation &lt;em&gt;later&lt;/em&gt; and program against abstractions.&amp;#160; When
I write a class I should think about what the responsibility of this class is and
what its dependencies are.&amp;#160; Microsoft is telling me to get onboard with loose
coupling.&amp;#160; I don’t need to petition my overlords to bring in a new framework
because the only thing between me and MEF is: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel.Composition;&lt;/strong&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Microsoft has always been a Platform company.&amp;#160; I like my Zune, the Xbox is cool,
I’m a PC, and maybe the Microsoft Stores will be cool.&amp;#160; But let’s be real: what
Microsoft does better than anything else is to build something that other people use
to build something.&amp;#160; By including MEF in the core toolbox Microsoft is getting
behind a lot of solid development ideas and removing another barrier between the Average
Corporate Developer and &lt;em&gt;&lt;strong&gt;Better&lt;/strong&gt;&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Arial"&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Arial"&gt;&amp;#160;&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.damonpayne.com/aggbug.ashx?id=7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8" /&gt;</description>
      <comments>http://www.damonpayne.com/CommentView,guid,7e83e1bf-bcc8-4039-b8d3-75ea0f94cbe8.aspx</comments>
      <category>.NET</category>
      <category>Architecture and Design</category>
      <category>Silverlight</category>
      <category>WPF</category>
    <feedburner:origLink>http://www.damonpayne.com/2009/08/16/WhyMEFMatters.aspx</feedburner:origLink></item>
  </channel>
</rss>
