<?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>Avivo Tech Blog &#187; generic</title>
	<atom:link href="http://tech.avivo.si/tag/generic/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.avivo.si</link>
	<description>Solving problems</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:46:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Silverlight: inherit a generic class from UserControl</title>
		<link>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=silverlight-inherit-a-generic-class-from-usercontrol</link>
		<comments>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 17:57:49 +0000</pubDate>
		<dc:creator>developer</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Silverlight/WPF]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[derive]]></category>
		<category><![CDATA[generic]]></category>
		<category><![CDATA[inherit]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[UserControl]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://tech.avivo.si/?p=208</guid>
		<description><![CDATA[Note! This blog post is just an idea than well tested solution, more a review that detailed description. In Silverlight UserControl and Page classes are usually used with xaml. Root tags in xaml define the type, like so: xaml &#60;UserControl x:Class="MyView"&#62; ... &#60;/UserControl&#62; code-behind namespace MyNamespace { class MyView : UserControl } Sometimes, developers want [...]]]></description>
			<content:encoded><![CDATA[<p>Note! This blog post is just an idea than well tested solution, more a review that detailed description.<br />
In Silverlight UserControl and Page classes are usually used with xaml. Root tags in xaml define the type, like so:</p>
<pre class="c-sharp">xaml
&lt;UserControl x:Class="MyView"&gt;
...
&lt;/UserControl&gt;

code-behind
namespace MyNamespace
{
  class MyView : UserControl
}</pre>
<p>Sometimes, developers want to extend UserControl, i.e. as View by MVVM pattern.</p>
<pre class="c-sharp">xaml
&lt;v:ViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace"&gt;
...
&lt;/v:ViewBase&gt;

code-behind
namespace MyNamespace
{
  class MyView : ViewBase /* ViewBase is a custom class */
}</pre>
<p>Step further, every View should have it&#8217;s specialized model (ViewModel), thus base class could integrate a Model property. To avoid casting one should use generics, like ViewBase&lt;MyViewModel&gt;.</p>
<pre class="c-sharp">xaml
&lt;!-- There's a problem, generics cannot be written in xaml. --&gt;

code-behind
namespace MyNamespace
{
  class MyView : ViewBase&lt;MyViewModel&gt; /* ViewBase is a custom class */
}</pre>
<p>Generics cannot be written directly in xaml? There is a workaround:<br />
1. Use a wrapper, described <a href="http://www.lab101.be/2008/07/silverlight-usercontrol-inheritance/">here</a><br />
2. Create a new non-generic class, like in this sample:</p>
<pre class="c-sharp">xaml
&lt;v:MyViewBase x:Class="MyNamespace.MyView" xmlns:v="clr-namespace:MyNamespace"&gt;
...
&lt;/v:MyViewBase&gt;

code-behind
namespace MyNamespace
{
  class MyView : MyViewBase
}

public abstract class MyViewBase : ViewBase&lt;MyViewModel&gt;
{
  /* non-generic class */
}</pre>
<p><strong>Appendix: ViewBase&lt;TViewModel&gt; class</strong></p>
<pre class="c-sharp">public abstract class ViewBase&lt;TViewModel&gt; : UserControl, INotifyPropertyChanged where TViewModel : ViewModelBase, new()
{
	private TViewModel model;
	public TViewModel Model
	{
		get
		{
			if (this.model == null)
			{
				this.model = new TViewModel();
				base.DataContext = this.model;
			}
			return this.model;
		}
		set
		{
			if (this.model != value)
			{
				this.model = value;
				base.DataContext = this.model;
				OnPropertyChanged("Model");
			}
		}
	}
}</pre>
<p>References:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/400778/usercontrol-that-has-a-generic-class-in-its-inheritance-tree">StackOverflow: UserControl that has a generic class in its inheritance tree</a></li>
<li><a href="http://stackoverflow.com/questions/225878/how-to-correctly-inherit-from-a-usercontrol-defined-in-xaml-in-silverlight">StackOverflow: How to correctly inherit from a UserControl defined in xaml in Silverlight</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tech.avivo.si/2009/11/silverlight-inherit-a-generic-class-from-usercontrol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

