Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance with Silverlight User Control Partial Classes

I am trying to allow several classes to inherit a more general Silverlight user control to avoid redundancy in my code. The classes inherit the extended control, which then inherits the User Control class. The issue I have been running into is that the ExtendedControlExtension.g.cs file regenerates every time I compile, with the incorrect inheritance (it inherits User Control not my Extended Control).

Note that I have been inheriting the Extended Control in the .cs and g.cs files, but continuing to use the User Control tag in the .aspx file as this causes the error

Error 29 The tag 'ExtendedControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

Is there a way to fix this?

Thanks!

like image 462
badpanda Avatar asked May 13 '11 18:05

badpanda


People also ask

Can partial class be inherited?

Inheritance cannot be applied to partial classes.

Can different parts of a partial class inherit from different interfaces?

All the parts that specify a base class must agree, but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all the interfaces listed by all the partial declarations.

Do partial classes have to be in same namespace?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project.

Can two classes have same partial name?

It doesn't compile as you can't have two methods with the same name in one class. Save this answer.


1 Answers

You cannot change the .g.cs file, in fact is says so right in the file. Also, it's unfortunate to use the term "custom control" as this means something specific and not what you are trying to do. But, the good news is that what you are trying to do is possible.

Derive from UserControl:

public class FancyUserControl : UserControl
{
    // Your added common functionality.
}

and then add a new UserControl to your project using the normal mechanism, let's say UserControl1. Then edit the UserControl.xaml files as follows:

<local:FancyUserControl x:Class="SilverlightApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SilverlightApplication1"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</local:FancyUserControl>

paying special attention to the three lines with local in them, adjusting to your application. Then edit the UserControl1.xaml.cs file as follows:

public partial class UserControl1 : FancyUserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }
}

and Visual Studio won't be quite happy yet but finally rebuild your project and all will be well.

The class UserControl1 is now derived from FancyUserControl instead of UserControl and you can begin adding your common functionality. To add more controls you will need to manually edit the XAML and code-behind once after initially adding each new control to the project.

like image 194
Rick Sladkey Avatar answered Sep 19 '22 02:09

Rick Sladkey