Creating a Base User Control Class that Inherits from UserControl


  • I have multiple UserControls with a large amount of shared code. I would have thought that the best way to do this would be to create a base class that inherits from UserControl, and then have my controls inherit that base class. For example:

    Public Class MyBaseControl : Inherits UserControl
    Public Class MyFirstControl : Inherits MyBaseControl
    Public Class MySecondControl : Inherits MyBaseControl

    But when I tried this by creating a *.vb file that inherits from UserControl, and then creating a user control that inherits that, I receive the following error:

    Base class 'Windows.UI.Xaml.Controls.UserControl' specified for class 'MyFirstControl' cannot be different from the base class 'MyBaseControl' of one of its other partial types.

    All I want is to use basic inheritance for my user controls. We all use inheritance for other stuff all the time, so why am I having so much trouble using it for my user controls? Do I need to manually change the base class somewhere that I am forgetting? I notice that the error says that it is in the *.g.i.vb file, but this is an automatically generated file, so even if I fixed it there, Visual Studio 2013 would just change it back again. Should I be creating the user controls some other way? I just want to take full advantage of code reusability. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, May 18, 2014 1:12 AM


  • I think I may have finally found a solution! Here are the steps I took:

    1: Create a new UserControl as you normally would. This will be your base control (I will refer to it as MyBaseControl).

    2: In the codebehind for MyBaseControl, change the modifier from NotInheritable to MustInherit (or just remove, if you want) so that it can be inherited.

    3. Create another UserControl as you normally would. This will be your control that inherits MyBaseControl (I will refer to it as MyControl).

    4. In the codebehind for MyControl, change it from Inherits UserControl to Inherits MyBaseControl.

    5. In MyControl.xaml, make sure the XML Namespaces include your project, for example: xmlns:local="using:MyBaseControlProject"

    6. Change the root element from UserControl to local:MyBaseControl

    Hopefully I didn't leave any steps out. One thing that I noticed while doing this is that Visual Studio 2013 does not immediately recognize these changes, so I have had to recompile every time I changed it, so if you see the squiggly line underneath local:MyBaseControl, try recompiling and see if it fixes it. Because of this, I suggest creating all of the controls before writing any codebehind, if possible, so that you can compile at least once before worrying about your other code. But regardless of whether I'm doing everything right or not, I have managed to get a running app that allows me to create user controls based on another UserControl. Hopefully this can help everyone else as well. Good luck!

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, May 18, 2014 5:50 PM