Ben
Ben

Reputation: 1032

Trying to get a WPF UserControl to Inherit a Class

I have four UserControls in my WPF Application - e.g.

VisualA, VisualB, VisualC, VisualD

I want each of them need to inherit a generic "Player" Class which contains a heap of shared code -. e.g. methods, timers etc

So far this is what I have tried in my Control's XAML

<UserControl x:Class="VisualA"

And here is what I have in a separate Class file.

Partial Public Class VisualA
    Inherits Player
End Class

Public Class Player
    Inherits UserControl
End Class

In my Window, I'm referencing the UserControl as normal:

<local:VisualA></local:VisualA>

But, I'm getting the following error:

Base class 'System.Windows.Controls.UserControl' specified for class 'VisualA' cannot be different from the base class 'Player' of one of its other partial types

What am I doing wrong?

I was also under the impression any code (i.e. methods) inside the inherited class (Player) would be able to access the Controls in the UserControl by referencing by name - is that correct?

Upvotes: 0

Views: 1621

Answers (1)

Tyson
Tyson

Reputation: 14734

The base class in the XAML is still set to UserControl. Change it to Player. Also note that the namespace for the Player type will have to be defined. i.e:

<BaseClasses:Player x:Class="VisualA"
        xmlns:BaseClasses="clr-namespace:MyProject.BaseClasses" 
        ... all your other namespaces used

Upvotes: 1

Related Questions