<?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>esmithy.net &#187; Silverlight</title>
	<atom:link href="http://esmithy.net/category/programming/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://esmithy.net</link>
	<description>Stuff Hammered Out by Eric Smith</description>
	<lastBuildDate>Fri, 02 Jul 2010 20:35:17 +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>Silverlight Localization</title>
		<link>http://esmithy.net/2009/07/08/silverlight-localization/</link>
		<comments>http://esmithy.net/2009/07/08/silverlight-localization/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 02:00:57 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=252</guid>
		<description><![CDATA[The official Silverlight documentation about localization gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in [...]]]></description>
			<content:encoded><![CDATA[<p>The official <a href="http://msdn.microsoft.com/en-us/library/cc838238(VS.95).aspx">Silverlight documentation about localization</a> gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in a page&#8217;s code-behind file. A reasonable way is to use data binding.</p>
<p><span id="more-252"></span></p>
<p>I found a good example of this in Chapter 5 of <em>Professional Silverlight 2 for ASP.NET Developers</em> by Jonathan Swift, Salvador Alvarez Patuel, Chris Barker and Dan Wahlin. As I describe it, and continue the discussion through this post, you&#8217;ll notice that the term &#8220;resource&#8221; is overloaded in Silverlight: There are resources in the normal .NET sense, backed by .resx files, and there are resources that are simply a dictionary of objects instantiated in XAML within a Resources element. Hopefully the usage will not be overly confusing.</p>
<p>In the localization example from the book, they created a resource file called LocalizedStrings.resx. Then they instantiated the automatically generated strongly typed class by including it in the Resources associated with a user control. Finally, they could then bind the Text property of a couple of TextBlock controls to a particular string from the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;SilverlightLocalizationExample.Page&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:Localized</span>=<span style="color: #ff0000;">&quot;clr-namespace:LocalizationExample.Resources&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Localized:LocalizedStrings</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LocalizedStrings&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding TextBlock1,</span>
<span style="color: #009900;">            Source={StaticResource LocalizedStrings}}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding TextBlock2,</span>
<span style="color: #009900;">            Source={StaticResource LocalizedStrings}}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You&#8217;ll want to be sure to use the PublicResXFileCodeGenerator custom tool to generate the code from the .resx file, otherwise the binding doesn&#8217;t work.</p>
<p>So what if you want to dynamically change languages, such as with a language drop-down?</p>
<p>The solution I came up with incorporates string resources into the view model. I was hesitant to do this at first because it felt funny having resources in the view model. Aren&#8217;t the strings purely a view component? Ultimately I decided that supplying the correct strings to the view based on language choice is a reasonable thing for the view model to do, and the resulting implementation and usage felt pretty good.</p>
<p>The base class for all view models simply has a Resources member:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">object</span> Resources
<span style="color: #000000;">&#123;</span>
  get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Each subclass then returns the appropriate object containing the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyViewModel <span style="color: #008000;">:</span> ViewModel
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> MyViewModelResources _resources<span style="color: #008000;">;</span>
&nbsp;
  MyViewModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    _resources <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyViewModelResorces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Resources
  <span style="color: #000000;">&#123;</span>
    get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _resources<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The class MyViewModelResources is the class generated from MyViewModelResources.resx. You might be saying, &#8220;Wait a minute. That class is basically <em>static</em>. What are you doing creating an instance of it?&#8221; Well, I don&#8217;t think there&#8217;s a way to bind to statics, so you need to have an actual instance around. Of course, the view will actually end up calling the static property (such as SubmitButtonText) when you include one in the binding path. Also, this isn&#8217;t any different from instantiating the resource class in XAML as the first example does.</p>
<p>Modifying the first example to use the view model (and setting the view model as the DataContext) makes the XAML look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;SilverlightLocalizationExample.Page&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:Localized</span>=<span style="color: #ff0000;">&quot;clr-namespace:LocalizationExample&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Localized:MyViewModel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ViewModel&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;LightBlue&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{StaticResource ViewModel}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Resources.TextBlock1}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Resources.TextBlock2}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So what about the dynamic switching of languages at run time?</p>
<p>Since the &#8220;Resources&#8221; property is in the binding path, changing its value will update the bindings and the UI updates with the new strings. You just need to notify the view that the Resources property has changed. To make this easy, I created a helper method in the ViewModel base class, RaiseResourcesChanged, that effectively says, &#8220;Attention all view models, please update your views&#8217; resources.&#8221; The message gets propagated to each view model instance by having a static ResourcesChanged event in the ViewModel base class that each ViewModel instance subscribes to at construction. When the event fires, the base class in turn raises the INotifyPropertyChanged.PropertyChanged event for the &#8220;Resources&#8221; property.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">event</span> EventHandler ResourcesChanged<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> ViewModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    ResourcesChanged <span style="color: #008000;">+=</span> ViewModel_ResourcesChanged<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> RaiseResourcesChanged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    var handler <span style="color: #008000;">=</span> ResourcesChanged<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>handler <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      ResourcesChanged<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, EventArgs.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">void</span> ViewModel_ResourcesChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    RaisePropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Resources&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Any single view model can then change the CurrentUICulture, call the helper method, and the UI updates automatically for all views:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Thread.<span style="color: #0000FF;">CurrentThread</span>.<span style="color: #0000FF;">CurrentUICulture</span> <span style="color: #008000;">=</span> newCulture<span style="color: #008000;">;</span>
RaiseResourcesChanged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/07/08/silverlight-localization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Silverlight View Models</title>
		<link>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/</link>
		<comments>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 15:16:23 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=246</guid>
		<description><![CDATA[Being relatively new to Silverlight development, I&#8217;ve not had the good sense to accept conventional wisdom that the Visual Studio unit test framework can&#8217;t be used to test Silverlight code. For testing view models at least, I&#8217;ve been successfully using Mstest and Rhino Mocks for a few months now. One of the goals of the [...]]]></description>
			<content:encoded><![CDATA[<p>Being relatively new to Silverlight development, I&#8217;ve not had the good sense to accept conventional wisdom that the Visual Studio unit test framework can&#8217;t be used to test Silverlight code. For testing view models at least, I&#8217;ve been successfully using Mstest and Rhino Mocks for a few months now.</p>
<p><span id="more-246"></span></p>
<p>One of the goals of the <em>model-view-view model</em> pattern is to avoid code in the XAML code-behind files. Instead, the view model exposes what the UI needs and the UI consumes it through declarative data binding. <a href="http://msdn.microsoft.com/en-us/magazine/dd458800.aspx">Shawn Wildermuth&#8217;s MSDN Magazine article</a> does a good job of explaining the basics of the pattern. The view model ends up having the logic that you&#8217;d really like to test.</p>
<p>The project structure looks like this:</p>
<ul>
<li><strong>SilverlightApp</strong> &#8211; This project is the <em>view</em>, created with the Visual Studio Silverlight Application project template. There&#8217;s lots of XAML in here.</li>
<li><strong>SilverlightApp.Model</strong> &#8211; This project contains both the <em>model</em> and the <em>view model</em>. It is a Silverlight Class Library project, and the view project has a project reference to this. There&#8217;s no XAML in here.</li>
<li><strong>SilverlightApp.Model.Test</strong> &#8211; This project has the unit tests, and is a normal C# Unit Test project. It has a project reference to the model/view model project.</li>
</ul>
<p>If you write up a simple unit test, it will compile, run, and pass.</p>
<div id="attachment_249" class="wp-caption aligncenter" style="width: 519px"><img class="size-full wp-image-249" title="test-passed" src="http://esmithy.net/content/test-passed.jpg" alt="Woohoo." width="509" height="121" /><p class="wp-caption-text">Woohoo -- passing test.</p></div>
<p><strong>Problem #1</strong></p>
<p>Suppose your view model uses something from System.Core (such as the Action delegate). Now when you try to run your unit test, you get an error:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.IO.FileNotFoundException: Could not load file or assembly &#8216;System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e&#8217; or one of its dependencies. The system cannot find the file specified.</p></blockquote>
<p>The problem is that the test project is referencing the .NET 3.5 System.Core assembly. You can fix that by referencing the Silverlight assembly instead. Remove the existing System and System.Core references, and add the ones in the Silverlight SDK (for example, C:\Program Files (x86)\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\). Now the test will pass again.</p>
<p><strong>Problem #2</strong></p>
<p>Now suppose that you want your view model to do something like use the IsolatedStorageSettings class. When you run your test, it will be broken again:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.IO.FileNotFoundException: Could not load file or assembly &#8216;System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e&#8217; or one of its dependencies. The system cannot find the file specified.</p></blockquote>
<p>Well, that just looks like problem #1 again, so we&#8217;ll add the Silverlight System.Windows assembly as a reference. Hmm&#8230; still doesn&#8217;t work:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.MissingMethodException: Method not found: &#8216;System.IO.IsolatedStorage.IsolatedStorageFile System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite()&#8217;.</p></blockquote>
<p>According to the <a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(VS.95).aspx">documentation</a>, IsolatedStorageFile is in mscorlib.dll. Can I just add a reference to that? No; when you try you get an error:</p>
<blockquote><p>A reference to &#8216;C:\Program Files (x86)\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\mscorlib.dll&#8217; could not be added. This component is automatically referenced by the project system and cannot be referenced directly.</p></blockquote>
<p>It&#8217;s time to step back and think a little. Do we really want to have Silverlight isolated storage as part of our unit tests anyway? Probably not. Just like when you&#8217;re unit testing code that would access a database, you&#8217;d really rather mock the database so you can efficiently test just the unit in question. The same applies here, so you can create an interface to hide the concrete implementation. From there, it&#8217;s a matter of choosing the right implementation at the right time.</p>
<p>The view model can accept an ISettings in its constructor (or it can use a service locator or dependency injection). When creating a test instance, pass in a mock implementation. In the real application, you can use the isolated storage implementation.</p>
<p><strong>So Far So Good</strong></p>
<p>By using these two techniques (referencing Silverlight assemblies in the test project and adding a level of abstraction where needed), I&#8217;ve been able to effectively test my view models with the normal desktop tools. The tests all run as part of the continuous integration builds, just as you&#8217;d hope.</p>
<p>Maybe at some point I&#8217;ll run into something that just doesn&#8217;t work, but so far it&#8217;s working out just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
