views:

2371

answers:

3

In Silverlight 2 I have the following class declaration for a control:

public partial class ClassX : UserControl

I wish to replace UserControl with ClassXBase which derives from UserControl but I'm getting the reasonable error "Partial declarations of 'ClassX' must not specify different base classes"

However, I'm unable to find the other partial class to replace its base class. Any idea where this other partial class is or how I do this?

+2  A: 

The UserControl's partial class is defined by the XAML and the framework expects it to be derived from UserControl. What are you trying to accomplish? You might be better off using encapsulation rather than inheritance. If you must use inheritance, then look to derive from other You might be better off deriving from one the other control classes like ContentControl or Control. Jesse Liberty does a great series of videos on that at Silverlight.net.

Michael S. Scherotter
+7  A: 

If you include the namespace of your base class of UserControl, you can do it as long as you use the namespace. For example:

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

Then you have to use this class in the XAML (Note the my namespace and then using the new namespace as the root of the document):

<!-- Page.xaml -->
<my:BaseUserControl 
    x:Class="SilverlightApplication11.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:my="clr-namespace:SilverlightApplication11"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</my:BaseUserControl>

This won't magically change the base class in the code-behind so change that code to your base class:

public partial class Page : BaseUserControl
{
  public Page()
  {
    InitializeComponent();
  }
}
Shawn Wildermuth
This worked perfectly. Thank you.
Jon Sagara
A: 

My Base class contains generic parameters in that case the above solution doesn't work. What can I do ?

Reply as a comment to Shawn's post.He's right, that's how you do it. You just need to realise this isn't directly supported behaviour at the moment.
TreeUK