Base Classes for User Controls

August 21, 2008
No Comments.

Silverlight Logo
I am still working with Siebrand Dijkstra and his people at School Master, BV and they’ve opened my eyes to another interesting development.

In looking at their code, I convinced them that their interface might be better as an Abstract class that derives from UserControl for some user controls they are creating:

public abstract class OurUserControl : UserControl{  // ...}

All was well until we tried to get the XAML partial class to compile with their class.  The problem revolved the problem that the partial class that the XAML file creates derives from UserControl, no matter what we did in our 1/2 of the partial class:

public partial class Page : OurUserControl{  public Page()  {    InitializeComponent();  }}

So I set out to see if there was a solution.  In looking at the XAML editor in Visual Studio I noticed the x:Subclass property of the UserControl element:

<UserControl x:Class="Foo.Page"             x:Subclass="Foo.OurUserControl"    xmlns="..."    xmlns:x="...">  ...</UserControl>

This didn’t work, the base class for the generated class (page.g.cs) was still UserControl. I tried to figure out another way when their developers decided to try and change the UserControl to the XAML instance of their class, adding their namespace to the XAML:

<my:OurUserControl x:Class="Foo.Page"                   xmlns:my="clr-namespace:Foo"                   xmlns="..."                   xmlns:x="...">  ...</my:OurUserControl>

I was so sure that this wouldn’t work that I dissuaded them from even trying, but they persisted. Oddly, it worked…even without the x:Subclass attribute. I am still a little surprised it worked. The only drawback is that Blend does not support it but expect that it will be fixed by release…at least I hope so.

Now I have to figure out what the heck x:Subclass is really for.  Where’s my Reflector?