<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JAPF &#187; WPF</title>
	<atom:link href="http://www.japf.fr/category/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Attributes-based validation in a WPF MVVM application</title>
		<link>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/</link>
		<comments>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 07:29:32 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[codeproject]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=630</guid>
		<description><![CDATA[Today, I&#8217;m proud to share with you my very first article available on CodeProject. This article presents a technique which can be used in order to add validation in a WPF MVVM application based on attribute. Basically, it means that you can write validation logic like that (notice the attribute associated to this property): ?View [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I&#8217;m proud to share with you my very first article available on <a href="http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx">CodeProject</a>. This article presents a technique which can be used in order to add validation in a WPF MVVM application based on attribute. Basically, it means that you can write validation logic like that (notice the attribute associated to this property):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p630code2'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6302"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p630code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Field 'FirstName' is required.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Of course the article comes with a nice demo application:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/07/02.png" rel="lightbox[630]"><img class="alignnone size-full wp-image-631" title="02" src="http://www.japf.fr/wp-content/uploads/2010/07/02.png" alt="" width="411" height="160" /></a></p>
<p>You can read the full article here: <a href="http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx">Attributes-based validation in a WPF MVVM application</a></p>
<p><em><br />
</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVVM Frameworks Explorer updated</title>
		<link>http://www.japf.fr/2010/07/mvvm-frameworks-explorer-updated/</link>
		<comments>http://www.japf.fr/2010/07/mvvm-frameworks-explorer-updated/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 18:44:25 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[mvvm]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=625</guid>
		<description><![CDATA[Today I&#8217;m releasing a new version of my MVVM Frameworks Explorer application. You can find the updated version here: http://www.japf.fr/silverlight/mvvm/index.html Here is a list of the changes in this new version: application updated to Silverlight 4 support is now detailed for WPF, Silverlight and Windows Phone new frameworks added (MEFedMVVM&#8230;) framework&#8217;s logo added download count [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m releasing a new version of my MVVM Frameworks Explorer application. You can find the updated version here: <a href="http://www.japf.fr/silverlight/mvvm/index.html">http://www.japf.fr/silverlight/mvvm/index.html</a></p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" rel="lightbox[625]"><img class="alignnone size-full wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" alt="" width="341" height="177" /></a></p>
<p>Here is a list of the changes in this new version:</p>
<ul>
<li>application updated to Silverlight 4</li>
<li>support is now detailed for WPF, Silverlight and Windows Phone</li>
<li>new frameworks added (MEFedMVVM&#8230;)</li>
<li>framework&#8217;s logo added</li>
<li>download count added (based on the information I found on CodePlex website)</li>
<li>about window</li>
</ul>
<p>As always, feel free to give feedback <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/07/mvvm-frameworks-explorer-updated/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Where does the default TwoWay binding comes from ?</title>
		<link>http://www.japf.fr/2010/07/where-does-the-default-twoway-binding-comes-from/</link>
		<comments>http://www.japf.fr/2010/07/where-does-the-default-twoway-binding-comes-from/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 11:50:43 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[dependency properties]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=620</guid>
		<description><![CDATA[I got a comment on my post about a very simple MVVM application about the fact that removing the TwoWay mode on a binding did not change the behavior of the application. This is a quick occasion for me to share a quick explanation about this. Actually and as you already know if you can [...]]]></description>
			<content:encoded><![CDATA[<p>I got a comment on my post about <a href="http://www.japf.fr/2009/02/very-simple-mvvm-demo-application/">a very simple MVVM application</a> about the fact that removing the TwoWay mode on a binding did not change the behavior of the application. This is a quick occasion for me to share a quick explanation about this.</p>
<p>Actually and as you already know if you can write <em>XAML </em>like <em>Text={Binding &#8230;}</em> it is only because <em>Text </em>is a<em> Dependency Property</em>. Also, dependency properties are defined in a static way (so that if you have 50 textboxes you don&#8217;t have to instantiate 50 times the Text property). The default behavior for the mode of the binding (TwoWay, OneWay, etc.) can be found in the static declaration of the dependency property. For example, in the case of the Text property of the TextBox we have:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p620code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6204"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p620code4"><pre class="csharp" style="font-family:monospace;">TextProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span>
	<span style="color: #666666;">&quot;Text&quot;</span>, 
	<span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, 
	<span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>, 
	<span style="color: #008000;">new</span> FrameworkPropertyMetadata<span style="color: #008000;">&#40;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, 
		FrameworkPropertyMetadataOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Journal</span> 
		<span style="color: #008000;">|</span> FrameworkPropertyMetadataOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">BindsTwoWayByDefault</span>,
	<span style="color: #008000;">new</span> PropertyChangedCallback<span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">OnTextPropertyChanged</span><span style="color: #008000;">&#41;</span>, 
	<span style="color: #008000;">new</span> CoerceValueCallback<span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">CoerceText</span><span style="color: #008000;">&#41;</span>,
	<span style="color: #0600FF; font-weight: bold;">true</span>, 
	UpdateSourceTrigger<span style="color: #008000;">.</span><span style="color: #0000FF;">LostFocus</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The interesting part here if of course the <em>BindsTwoWayByDefault</em> option. Note that this is the only default option available (we can&#8217;t have a OneWayToSource binding by default).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/07/where-does-the-default-twoway-binding-comes-from/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leveraging expression trees to unit test ViewModel classes</title>
		<link>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/</link>
		<comments>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 13:36:53 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[moq]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=483</guid>
		<description><![CDATA[Introduction: In this article, I&#8217;m describing a technique which leverage the expression trees of C# 3.0 in order to facilitate the unit testing of ViewModel&#8217;s properties. My final goal is to be able to unit test a ViewModel property in 1 line. Without any doubt MVVM is now the most used framework to leverage WPF [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction:</strong> In this article, I&#8217;m describing a technique which leverage the expression trees of C# 3.0 in order to facilitate the unit testing of ViewModel&#8217;s properties. My final goal is to be able to unit test a ViewModel property in 1 line.</p>
<p>Without any doubt MVVM is now the most used framework to leverage WPF and Silverlight functionalities in the best way ! During the last <a href="http://live.visitmix.com">Mix</a>, 3 sessions were dedicated to this methodology (you can watch the videos online <a href="http://live.visitmix.com/Videos">here</a>).</p>
<p>As you already know one of the key advantage of the MVVM methodology is to <strong>improve the testability</strong> of the overall application by reducing the amount of code in the code-behind and producing ViewModel classes which are testable. We use to say that ViewModel classes are testable because:</p>
<ul>
<li>they are not coupled to UI concepts (controls, focus, keyboard input&#8230;)</li>
<li>they can wrap model objects using interfaces (for instance a PersonViewModel wraps a IPerson object)</li>
<li>they are not subclassing a UI control (such as Button or Window)</li>
</ul>
<p>Today I&#8217;d like to share a technique I&#8217;m using to facilitate the unit tests of some properties of my ViewModel classes.</p>
<p>Let&#8217;s use a very simple ViewModel class as example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code12'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p483code12"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PersonViewModel <span style="color: #008000;">:</span> ViewModelBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> IPerson person<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> isSelected<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name 
  <span style="color: #008000;">&#123;</span>
     get
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
     set
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsSelected
  <span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isSelected</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isSelected</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;IsSelected&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// rest of the code omitted for simplicity</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Name property, as usually with the MVVM pattern gets its value from the wrapped model object. The easiest way to unit test this property is to use a mocking library. Here is a example using <a href="http://code.google.com/p/moq/">MOQ</a> (my favourite mocking library):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code13'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48313"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p483code13"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var mockPerson <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mock<span style="color: #008000;">&lt;</span>IPerson<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonViewModel<span style="color: #008000;">&#40;</span>mockPerson<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Jeremy&quot;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// verify that the Name property of the IPerson interface has been set</span>
  mockPerson<span style="color: #008000;">.</span><span style="color: #0000FF;">VerifySet</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Jeremy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Selected property is different because it doesn&#8217;t wrap a model property. It&#8217;s an information that is added to the ViewModel layer in order to control a UI-related property (for example the IsSelected property of a ListBoxItem). This technique is heavily used to have ViewModel classes interact with the WPF or Silverlight TreeView or ListBox control (you can check out this <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">excellent article</a> of Josh Smith for more detail).</p>
<p>In order to unit test this property, we must:<br />
1/ ensure the PropertyChanged event of the INotifyPropertyChanged is  raised properly<br />
2/ ensure we can write a value and read back the  correct value</p>
<p>Here is a sample code which does this unit test:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code14'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p483code14"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span>  PersonViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">bool</span> propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
vm<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> propertyChanged <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">==</span>  <span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">;</span>
vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;newName&quot;</span><span style="color: #008000;">;</span>
&nbsp;
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;newName&quot;</span>, vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It quickly become cumbersome to copy/paste this unit test for all the ViewModel properties we have. That&#8217;s the reason I started thinking about another way to do it&#8230;</p>
<p>Here is the feature I&#8217;m proposing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code15'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48315"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p483code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TestHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">TestProperty</span><span style="color: #008000;">&#40;</span>vm, v <span style="color: #008000;">=&gt;</span> v<span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In this sample, I&#8217;m telling I want to test the IsSelected property of the PersonViewModel type. The advantages are:<br />
1/ less code involved : 1 line to test 1 property<br />
2/ intellisense support in order to prevent typing error and no more &#8220;magic&#8221; string to give the name of the property<br />
3/ refactoring the name of the property will refactor this sample code too<br />
4/ automatic generation of default test values behind the scene</p>
<p><strong>How does it works ?</strong></p>
<ul>
<li>TestProperty treats the second parameter as an Expression&lt;Func&gt; and not as a Func directly</li>
<li>Using expression tree (the &#8220;v =&gt; v.IsSelected&#8221; part),  I&#8217;m able to retrieve the name of the property and its type</li>
<li>Using reflection, I&#8217;m able to get and set the value</li>
<li>Depending on the type of the property (string, bool, int, double), I have default values write and read back (with a test to ensure that the PropertyChanged event has been raised properly).</li>
</ul>
<p>Here is the code of the TestPropertyMethod:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code16'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p483code16"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestProperty<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T viewmodel, Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;&gt;</span> expression<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span> <span style="color: #008000;">is</span> MemberExpression<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        MemberExpression memberExpression <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MemberExpression<span style="color: #008000;">&#41;</span> expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #666666;">&quot;value1&quot;</span>, <span style="color: #666666;">&quot;value2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span> 
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">99</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #FF0000;">1.0</span>, <span style="color: #FF0000;">99.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Type is not supported&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And the TestViewModelProperty:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code17'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p483code17"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestViewModelProperty<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T viewModel, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName, U value1, U value2<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> propertyChanged<span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> propertyChanged <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">==</span> propertyName<span style="color: #008000;">;</span>
&nbsp;
    propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>propertyName, value1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&lt;</span>U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>value1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>propertyName, value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&lt;</span>U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m using 2 extensions methods in order to get and set value from the ViewModel object using reflection. Here they are:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p483code18'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p483code18"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> T GetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">object</span> obj, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var propertyInfo <span style="color: #008000;">=</span> obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>propertyInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>obj, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">object</span> obj, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName, T value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var propertyInfo <span style="color: #008000;">=</span> obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    propertyInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>obj, value, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Please feel free to <a href="http://www.japf.fr/wp-content/uploads/2010/03/TestViewModelHelper.txt">download the source code</a> of the ViewModelTestHelper class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Changing the layout of a WPF TreeView to look like multiple ListBox</title>
		<link>http://www.japf.fr/2010/03/changing-the-layout-of-a-treeview-to-look-like-multiple-listbox/</link>
		<comments>http://www.japf.fr/2010/03/changing-the-layout-of-a-treeview-to-look-like-multiple-listbox/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 14:31:35 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[listbox]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=457</guid>
		<description><![CDATA[Last week at work I was requested to create a new control which should have the behavior of the WPF treeview with a template involving multiple ListBox. To understand what I mean, I wanted to change the layout of this: To this: Of course, selection must work properly: I asked a question on StackOverflow about [...]]]></description>
			<content:encoded><![CDATA[<p>Last week at work I was requested to create a new control which should have the behavior of the WPF treeview with a template involving multiple ListBox. To understand what I mean, I wanted to change the layout of this:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/03/treeview1.png" rel="lightbox[457]"><img class="alignnone size-full wp-image-459" title="treeview1" src="http://www.japf.fr/wp-content/uploads/2010/03/treeview1.png" alt="" width="121" height="140" /></a></p>
<p>To this:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/03/treeview2.png" rel="lightbox[457]"><img class="alignnone size-full wp-image-460" title="treeview2" src="http://www.japf.fr/wp-content/uploads/2010/03/treeview2.png" alt="" width="342" height="124" /></a></p>
<p>Of course, selection must work properly:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/03/treeview3.png" rel="lightbox[457]"><img class="alignnone size-full wp-image-461" title="treeview3" src="http://www.japf.fr/wp-content/uploads/2010/03/treeview3.png" alt="" width="340" height="124" /></a></p>
<p>I asked a question <a href="http://stackoverflow.com/questions/2369901/change-the-layout-of-a-treeview-to-looks-like-multiple-listbox">on StackOverflow</a> about this control because I didn&#8217;t find any easy way to do it. I though at the beginning that playing with the templates of the TreeView and TreeViewItem would do the trick but it didn&#8217;t.</p>
<p>The solution I finally choose involves creating multiple ListBox and wire them together using DataBinding:</p>
<ul>
<li>I create a new CustomControl which inherits Control (I couldn&#8217;t use neither Selector or TreeView because I wouldn&#8217;t have been able to manage the SelectedItem property from the derived class)</li>
<li>In the template of this CustomControl is an ItemsControl. This ItemsControl has its ItemTemplate property set to a DataTemplate containing a ListBox.</li>
<li>The CustomControl has a Depth property of type int. This property indicates the number of ListBox that should be generated.</li>
<li>The CustomControl automatically databound ListBoxes together: each ListBox&#8217;s ItemsSource property is databound to the SelectedItem&#8217;s children property of the previous ListBox in the visual tree.</li>
<li>The CustomControl has a SelectedItem property and a SelectionChanged event (like Selector-derived class).</li>
<li>I added an IsReallySelected attached property to the ListBoxItem which are generated. This enables to databing an IsSelected property of the ViewModel class behind the control with the IsSelected of the ListBoxItem. I had to create an attached property because its value is true when the ListBoxItem is selected AND the parent ListBox has IsSelectionActive set to true.</li>
</ul>
<p>Here is the result:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/03/control.png" rel="lightbox[457]"><img class="alignnone size-full wp-image-462" title="control" src="http://www.japf.fr/wp-content/uploads/2010/03/control.png" alt="" width="499" height="162" /></a></p>
<p>You can <a href="http://www.japf.fr/wp-content/uploads/2010/03/TestTreeViewList.zip">download the source code</a> of this sample (VS2010 RC solution).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/03/changing-the-layout-of-a-treeview-to-look-like-multiple-listbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minor update to the Silverlight MVVM frameworks explorer</title>
		<link>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/</link>
		<comments>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 12:48:19 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[toolkit]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=428</guid>
		<description><![CDATA[Thanks to the readers who gave me feedback on my Silverlight MVVM frameworks explorer I updated the application this morning in order to fix some problems. Here is the change set: fix incorrect URLs fix incorrect &#8220;Silverlight Support&#8221; options. As Laurent Bugnion said in the comments, his MVVM Light framework was the only one supporting [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the readers who gave me feedback on my <a href="http://www.japf.fr/silverlight/mvvm/index.html">Silverlight MVVM frameworks explorer</a> I updated the application this morning in order to fix some problems.</p>
<p>Here is the change set:</p>
<ul>
<li>fix incorrect URLs</li>
<li>fix incorrect &#8220;Silverlight Support&#8221; options. As Laurent Bugnion said in the comments, his MVVM Light framework was the only one supporting Silverlight which was strange&#8230;</li>
<li>links now open in a new window</li>
</ul>
<p>Click on the following image to launch the Silverlight application.</p>
<p><a href="http://www.japf.fr/silverlight/mvvm/index.html"><img class="alignnone size-medium wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app-300x156.png" alt="" width="314" height="163" /></a></p>
<p>About adding new frameworks, I&#8217;m not sure to add those which targets a much larger domain than MVVM itself. CompositeApplication guidance for example is a lot more than MVVM&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF internals part 2 : how the WPF controls are organized ?</title>
		<link>http://www.japf.fr/2010/01/wpf-internals-how-the-wpf-controls-are-organised/</link>
		<comments>http://www.japf.fr/2010/01/wpf-internals-how-the-wpf-controls-are-organised/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 19:35:32 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[architecture]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=394</guid>
		<description><![CDATA[A couple of weeks ago, I started a series of articles about WPF internals organization. In the first article I did a tour of the core WPF classes. In this second part, I&#8217;m reviewing the organization of the various controls that exist in the framework. Because the image of the diagram is pretty big, I [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I started a series of articles about <a href="http://www.japf.fr/wpf-internals/">WPF internals</a> organization. In <a href="http://www.japf.fr/2009/10/wpf-internals-part-1-what-are-the-core-wpf-classes/">the first article</a> I did a tour of the core WPF classes. In this second part, I&#8217;m reviewing the organization of the various controls that exist in the framework.</p>
<p>Because the image of the diagram is pretty big, I decided to use Silverlight DeepZoom and the result is just below this text <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  You can download the <a href="http://www.japf.fr/wp-content/uploads/2010/01/WpfControlsFull.png" target="_blank" rel="lightbox[394]">full image resolution here</a>. Please use the full screen button in the upper right corner of the viewer for the best browsing experience.</p>
<p><script type="text/javascript">
        function onSilverlightError(sender, args) 
        {
            var appSource = "";
            if (sender != null &#038;&#038; sender != 0)
            {
                appSource = sender.getHost().Source;
            }
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;</p>
<p>            var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";</p>
<p>            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";</p>
<p>            if (errorType == "ParserError") 
            {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") 
            {
                if (args.lineNumber != 0) 
                {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }</p>
<p>            throw new Error(errMsg);
        }
</script></p>
<div id="slPluginHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="540" height="500"><param name="minRuntimeVersion" value="3.0.40307.0" /><param name="autoUpgrade" value="false" /><param name="source" value="http://www.japf.fr/silverlight/wpfcontrolsexplorer/WpfControlsExplorer.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="initParams" value="adFile=http://www.japf.fr/silverlight/wpfcontrolsexplorer/GeneratedImages" /><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><br />
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
            </a><br />
        </object><br />
	<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>
<ul></ul>
<p>Here are general remarks that might help you get information from those diagrams.</p>
<p>The top level <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.control.aspx">Control</a> class:</p>
<ul>
<li>Defines general UI properties such as Background, Foreground, BorderBrush and BorderThickness</li>
<li>Defines a set of properties to control font rendering: FontFamily, FontSize, FontStyle&#8230;</li>
<li>Has a DoubleClick event (other mouse events such as MouseUp/MouseDown comes from the UIElement class)</li>
</ul>
<p>Below the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.control.aspx">Control</a> class, we have (this list is not complete):</p>
<ul>
<li>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx">ContentControl</a> class: those controls have&#8230; a single Content</li>
<li>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx">ItemsControls</a> class which handles a collection of child controls</li>
<li>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.textboxbase.aspx">TextBoxBase</a> class which is the parent of <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.aspx">TextBox</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a></li>
<li>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.rangebase.aspx">RangerBase</a> class which handles a range (minimum, maximum) and which is the parent of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.scrollbar.aspx">ScrollBar</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.slider.aspx">Slider</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar.aspx">ProgressBar</a> classes</li>
<li>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb.aspx">Thumb</a> class which has the ability to be dragged over a surface</li>
</ul>
<p>Below the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx">ContentControl</a> class we can find many existing WPF controls:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.aspx">ButtonBase</a> and its child: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx">Button</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.aspx">ToggleButton</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.repeatbutton.aspx">RepeatButton</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.label.aspx">Label</a> (see <a href="http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/">this post</a> for an explanation of the differences between a Label and a TextBlock)</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem.aspx">ListBoxItem</a> and its children: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.listviewitem.aspx">ListViewItem</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.comboboxitem.aspx">ComboBoxItem</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.aspx">ScrollViewer</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.headeredcontentcontrol.aspx">HeaderedContentControl</a>, a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx">ContentControl</a> that has both a Content and a Header. This is the case of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.expander.aspx">Expander</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.groupbox.aspx">GroupBox</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tabitem.aspx">TabItem</a> controls</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tooltip.aspx">Tooltip</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.window.aspx">Window</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspx">UserControl</a></li>
</ul>
<p>General other remarks:</p>
<ul>
<li>It&#8217;s funny to see that both Window and UserControl inherits from ContentControl. Before doing the diagram I though that Window came from somewhere else <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Having those diagrams in mind (or on a screen !) is very useful when you need to create your own custom control</li>
<li>We can see the differences between creating a custom control (inherit from Control or derived class) and a UserControl (inherit from UserControl)</li>
<li>.Net4 will introduce new controls (not in this diagram) in the WPF framework such: DataGrid, Calendar, TimePicker</li>
</ul>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.japf.fr%2f2010%2f01%2fwpf-internals-how-the-wpf-controls-are-organised%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.japf.fr%2f2010%2f01%2fwpf-internals-how-the-wpf-controls-are-organised%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/01/wpf-internals-how-the-wpf-controls-are-organised/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Discover and compare existing MVVM frameworks !</title>
		<link>http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/</link>
		<comments>http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:31:50 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlifgt]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=369</guid>
		<description><![CDATA[A couple of weeks ago, I wrote a blog post where I compared the existing MVVM frameworks. This post became a bit famous in the WPF/Silverlight blog world and I received a lot of feedback to update the list, fix information, etc. I also got a request from Erik suggesting me to put all the [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I wrote <a href="http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/">a blog post</a> where I compared the existing MVVM frameworks. This post became a bit famous in the WPF/Silverlight blog world and I received a lot of feedback to update the list, fix information, etc. I also got a request from Erik suggesting me to put all the datas in a matrix.</p>
<p>Today I&#8217;m proud to announce the <a href="http://www.japf.fr/silverlight/mvvm/index.html">MVVM frameworks Silverlight application</a> (click the image to open the Silverlight3 page).</p>
<p><a href="http://www.japf.fr/silverlight/mvvm/index.html"><img class="alignnone size-full wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" alt="silverlight-mvvm-app" width="562" height="293" /></a></p>
<p>A couple of observation:</p>
<ul>
<li>please contact me via <a href="http://japf.fr/contact">this blog</a> or <a href="http://www.twitter.com/jalpf">twitter</a> if you find incorrect information</li>
<li>I&#8217;m not judging anybody&#8217;s work by giving rating, it&#8217;s just my personal feeling to have an easiest way to sort the data</li>
</ul>
<p>Hope you&#8217;ll like it <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>PDC09 : How VS2010 was built with WPF ?</title>
		<link>http://www.japf.fr/2009/11/pdc09-how-vs2010-was-built-with-wpf/</link>
		<comments>http://www.japf.fr/2009/11/pdc09-how-vs2010-was-built-with-wpf/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 11:24:01 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Stutio]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[pdc]]></category>
		<category><![CDATA[vs2010]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=362</guid>
		<description><![CDATA[In my last blog post, I did a review of a PDC09 Session &#8220;Advanced performance tuning with WPF&#8221;. Today, I&#8217;m doing a review for another very interesting session &#8220;How VS2010 was built with WPF ?&#8221;. The video is available here. Why did Microsoft choose WPF for VS2010 ? Technological: prove the capabilities of WPF4 Architectural [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.japf.fr/wp-content/uploads/2009/08/PDC09.jpg" rel="lightbox[362]"><img class="alignnone size-full wp-image-245" title="PDC09" src="http://www.japf.fr/wp-content/uploads/2009/08/PDC09.jpg" alt="PDC09" width="79" height="109" /></a></p>
<p>In my <a href="http://www.japf.fr/2009/11/pdc09-wpf-application-performance-tuning/">last blog post</a>, I did a review of a PDC09 Session &#8220;Advanced performance tuning with WPF&#8221;. Today, I&#8217;m doing a review for another very interesting session &#8220;<strong>How VS2010 was built with WPF</strong> ?&#8221;. The video is available <a href="http://microsoftpdc.com/Sessions/CL09">here</a>.</p>
<p style="text-align: center;"><a href="http://www.japf.fr/wp-content/uploads/2009/11/vs2008tovs2010.png" rel="lightbox[362]"><img class="size-large wp-image-365 aligncenter" title="vs2008tovs2010" src="http://www.japf.fr/wp-content/uploads/2009/11/vs2008tovs2010-1024x361.png" alt="vs2008tovs2010" width="543" height="191" /></a></p>
<p>Why did Microsoft choose WPF for VS2010 ?</p>
<ul>
<li>Technological: prove the capabilities of WPF4</li>
<li>Architectural
<ul>
<li>Separated presentation</li>
<li>Support for long range road map (+10 years)</li>
</ul>
</li>
<li>Key VS2010 features need it: Editor, Architecture Editor, Parallel tools debugging</li>
<li>Take an opportunity to give feedback for WPF4: when VS2010 development started, .Net 3.5 SP1 was just shipped and it was the right time to give feedback for the new features and fixes for WPF4.</li>
</ul>
<p>It is worth noting that VS2010 is a project that is being watched carefully inside Microsoft. We can expect that more Microsoft applications will move to WPF in the next coming years.</p>
<p>What WPF features are used ?</p>
<ul>
<li>Declarative UI using XAML</li>
<li>Databinding</li>
<li>Styles and templates</li>
<li>Application Resources</li>
<li>Interop Win32 (because they did not have the time to rewrite everything using WPF or because features doest not need to use WPF for example de WinForms designer&#8230;)</li>
<li>Integration with the message loop (to deal with particular focus issues)</li>
<li>New text stack (part of WPF4) based on DirectWrite</li>
</ul>
<p>Staged approach</p>
<ul>
<li>Define data models: a huge diagramming and architectural exercice</li>
<li>Replace the main window with WPF (only the window not its content !) to start the mix approach (managed/unmanaged, WPF/Hwnds)</li>
<li>Write new managed components: Window manager, command bar presentation</li>
<li>Scout with other VS teams</li>
<li>Test, Test, Test&#8230;</li>
</ul>
<p>What were the challenges ?</p>
<ul>
<li>Mixed mode application: native and managed code; WPF and HWNDs (Win32 or WinForms)</li>
<li>Keep existing extensions working and allow new extensions to take advantage of WPF</li>
<li>Don&#8217;t &#8220;stop the train&#8221; other teams were working at the same time on the product to add new functionalities</li>
<li>Text clarity</li>
<li>Performance</li>
<li>Focus management</li>
</ul>
<p>You can watch the session <a href="http://microsoftpdc.com/Sessions/CL09">here</a> if you want more detail and demos of what I mention in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/11/pdc09-how-vs2010-was-built-with-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PDC09: Advanced WPF Application Performance Tuning</title>
		<link>http://www.japf.fr/2009/11/pdc09-wpf-application-performance-tuning/</link>
		<comments>http://www.japf.fr/2009/11/pdc09-wpf-application-performance-tuning/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 13:30:14 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=347</guid>
		<description><![CDATA[In this post, I&#8217;m doing a review of the Advanced WPF Application Performance Tuning session of PDC09. 1. Introduction WPF = Designer + Developer: Designer: Resources constraints, Drawing effects, Excessive use of images, &#8220;Rich&#8221; template Developer: Loaded modules, Startup time (cold and warm time), Memory leaks (it&#8217;s not because we have a GC that leak [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.japf.fr/wp-content/uploads/2009/08/PDC09.jpg" rel="lightbox[347]"><img class="alignnone size-full wp-image-245" title="PDC09" src="http://www.japf.fr/wp-content/uploads/2009/08/PDC09.jpg" alt="PDC09" width="79" height="109" /></a></p>
<p>In this post, I&#8217;m doing a review of the <a href="http://microsoftpdc.com/Sessions/CL11">Advanced WPF Application Performance Tuning</a> session of PDC09.</p>
<p><strong>1. Introduction</strong></p>
<p>WPF = Designer + Developer:</p>
<ul>
<li><strong>Designer</strong>: Resources constraints, Drawing effects, Excessive use of images, &#8220;Rich&#8221; template</li>
<li><strong>Developer</strong>: Loaded modules, Startup time (cold and warm time), Memory leaks (it&#8217;s not because we have a GC that leak doesn&#8217;t exist), Large element count</li>
</ul>
<p>Performance general truth:</p>
<ul>
<li>Measure, measure, measure (identify key scenarios and set clear goals)</li>
<li>Low hanging fruit: identify parts that need optimized (to avoid spending time on optimizations that will not be perceived by the user)</li>
<li>Perceived performance is the most important thing: make it <strong>feel</strong> fast</li>
<li>Trade-offs: CPU vs memory&#8230;</li>
</ul>
<p>After this short intro, the speaker introduce <a href="www.fishbowlclient.com">FishBowl</a> a sample WPF application that brings Facebook on our desktop. This app will be used as a reference in the rest of the session to demonstration optimization techniques.</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/11/fishbowl.png" rel="lightbox[347]"><img src="http://www.japf.fr/wp-content/uploads/2009/11/fishbowl-300x144.png" alt="fishbowl" title="fishbowl" width="300" height="144" class="alignnone size-medium wp-image-354" /></a></p>
<p><strong>2. Memory usage</strong></p>
<p>The demonstration starts using Process Explorer to have a look at the memory usage of the app which is about 150MB. This seems quite high for such an application. By using VMMAP, the speaker finds out that the native heap is 100MB whereas the managed heap is only 50MB. This seems strange for a pure WPF application.</p>
<p>There is always native heap (render thread&#8230;), but when it&#8217;s particularly large, it&#8217;s generally related to images used in the application.</p>
<p>By browsing the source code of FishBowl, the speaker shows that the startup animation is not XAML based but uses more than 100 PNG images holding more than 30MB of memory (that will be in the native heap).</p>
<p>Using .Net memory profiler, the speaker finds out that the bitmap images used in the startup animation are not released when the animation is over causing a memory leak.</p>
<p><strong>3. Cold Start</strong></p>
<p>Cold start is all about disk I/O, we must minimize the amount of data that must be read from disk. The speaker shows that System.Window.Forms and System.Drawing assemblies are loaded during startup whereas it should not be the case for a pure WPF app (and about 300ms could be saved).</p>
<p>Then using the &#8220;Show hierarchical&#8221; feature of the .Net memory profiler, he finds out that System.Drawing only has 1 instanciated type (the Rectangle class !). By changing the code to remove those dependencies he prevents those 2 DLLs from loading in the startup process and improve the startup time !</p>
<p>Although by changing this code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p347code21'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p34721"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p347code21"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Settings</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnableLoggin</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// LogEntry is defined in the Entreprise Logging Library DLL</span>
  <span style="color: #008080; font-style: italic;">// and use this class will cause the assembly to be loaded</span>
  var entry <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LogEntry<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Into this one:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p347code22'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p34722"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p347code22"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>Properties<span style="color: #008000;">.</span><span style="color: #0000FF;">Settings</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnableLoggin</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// the previous code has been moved to a dedicated method</span>
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LogMessage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>MethodImpl<span style="color: #008000;">&#40;</span>MethodImplOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">NoInlining</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> LogMessage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// code that uses LogEntry and needs to load the Loggin assembly</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can prevent the DLL from being loaded when the loggin option is not enabled ! Notice the MethodImpl attribute that prevents the compiler from inlining the method (which would take us back to the original code&#8230;)</p>
<p><strong>4. Warm start</strong></p>
<p>During warm start we&#8217;re not expecting to see as much I/O as during cold start. The first tool that should be used to analyze warm start is a CPU profiler.</p>
<p>In the FishBowl app, the main ListBox does not have virtualization turned on because each item has a variable height which cause a hang in the startup process. In order to improve the experience of the user, the app has been updated to load the elements in a background thread.</p>
<p>The real performance is not changed here, but the perceived performance for the user is much better because he has a visual feedback instantly !</p>
<p><strong>5. RunTime</strong></p>
<p>Using WPFPerf, it&#8217;s possible to find out what the CPU usage is (between animation, layout, rendering&#8230;). The perforator tool gives various graphs such as Frame Rate, Software and Hardware IRTs (Intermediate Render Target).</p>
<p>Currently if a Rectangle has DrapShadowEffect with an Opacity of 0, it is still computed by the rendering thread (it could be an optimization that WPF does internally but it&#8217;s not yet the case) which can cause performance issue when the parent item is animated.</p>
<p>A possible optimization is to split the Rectangle into 2 Rectangles:</p>
<ul>
<li>the first one with the effect and an opacity of 0 (on the Rectangle itself not on the effect like previously)</li>
<li>the second one without any effect and an opacity of 1 (visible), a storyboard is set on this rectangle to make the first element visible by animating its opacity</li>
</ul>
<p>In this case, because the first Rectangle is hidden (Opacity = 0), the cost needed to animate this rectangle is much better.</p>
<p>Aditionnal recommendations:</p>
<ul>
<li>don&#8217;t block on the UI thread</li>
<li>use virtualization, if you can&#8217;t use it, improve the perceived performance by loading the items in a background thread</li>
<li>data virtualization</li>
<li>freeze your freezable !</li>
</ul>
<p><strong>6. Summary</strong></p>
<ul>
<li>Memory: images size, memory leaks, elements count</li>
<li>Cold start: disk I/O, module loads, NGen</li>
<li>Warm start: avoid blocking, delay work, perceived perf</li>
<li>RunTime: beware of IRTs, eventing</li>
</ul>
<p><strong>7. Tools</strong></p>
<ul>
<li><a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> in order to know how much memory has been allocated</li>
<li><a href="http://technet.microsoft.com/en-us/sysinternals/dd535533.aspx">VMMap</a> where the memory is going ? (native head, managed heap&#8230;)</li>
<li><a href="http://memprofiler.com/">.NET memory profiler</a> the profiler that is being used by the WPF team internally at Microsoft</li>
<li>ETW: event tracing</li>
<li>WPFPerf: WPF performance analyzer</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/11/pdc09-wpf-application-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
