Base Classes for User Controls

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?

Comments:

Gravatar

I find it helpful to think of the Xaml as a declarative Class Factory (pattern). You're telling the rendering engine to create you a UserControl, the concrete type of which is that which you state in the x:Class.

Visual Studio happens to infer from this factory declaration that you'll actually need that concrete class and creates it for you in a partial class.

In this way you are sort of working backwards from how you may implement a factory pattern in the pure coding sense, but I like the analogy.

Gravatar

I understand that, its just that x:Subclass implies that it should work...I understand how it does...but since its not doc'd its hard to make sense...

Gravatar

The x:Subclass is targeted at classes that don't support partial classes and need to use subclassing in order to combine the WPF class and the code behind file.

Gravatar

Maurice,

But it doesn't do anything...does it use Subclass in WPF maybe? Because this technique is the only one to subclass in SL that I could find.

Gravatar

Shawn-
The reason that this does not work in Blend is that in order to design the UserControl.xaml file we need to instantiate the base class on the design surface. In this case the base class is abstract and thus Blend can't instantiate it.

Try making OurUserControl non-abstract, it should work.


 



 
Save Cancel