Question

Here's my wish to create something abstract in WPF. You've got a main window (called main for example, even if it's not correct we don't care) with two buttons. Those two buttons have the same function : they open a new window, the same for both of them, but with different things inside. So I decided to create an abstract class to rule them like that :

public abstract (partial ?) class A : Window
{
    public A()
    {
        InitializeComponent(); // Not sure about that, it's kinda weird to use it here no ?
    }

    ...
}

public partial class B : A
{
    public B()
    {
        InitializeComponent(); // Since it's already in A I shouldn't have to use it here right ?
    }

    ...
}

public partial class C : A
{
    public C()
    {
        InitializeComponent(); // Same thing here...
    }

    ...
}

Debugging gives me something like : "error CS0263: Partial declarations of 'namespace.B' must not specify different base classes".

Removing 'partial' from A class gives : "error CS0260: Missing partial modifier on declaration of type 'namespace.A'; another partial declaration of this type exists".

I know that 'partial' specifies if a class has another part of her somewhere else (like the xaml file beside the cs one), so I guess the abstract class has to be partial too since it contains my controls. Maybe the B class shouldn't be partial ?

I know (memories) it works with Windows Forms, but there's no xaml files so it's easier, and I didn't find any useful tips. I think I understood that this problem occurs when I don't change something in my xaml file, which doesn't works as simply as "class : abstract class". Maybe the subclass thing ?

Please note that I'm a beginner in WPF apps, so I thank you in advance if your answer is as detailed as possible.

Thanks !

Was it helpful?

Solution

You need to define your base class all in code without using XAML :

Code for class A :

public abstract class A : Window { }

Code for class B :

public partial class B : A
{
    public B()
    {
        InitializeComponent();
    }
}

xaml for class B :

<yourNamespace:A x:Class="yourNamespace.B"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:yourNamespace="clr-namespace:yourNamespace"...

OTHER TIPS

A C# partial class is one that is defined over two or more source files. The other 'part' of Window is generated by Visual Studio, and includes things like InitializeComponent()

If you want a base Window class, then it has to be defined entirely in code, and have no XAML component. It will then no longer be a partial class.

public abstract class WindowA : Window
{
    // define base methods here
}

Then you can derive WindowB from WindowA:

public partial class WindowB : WindowA
{
    public WindowB()
    {
        InitializeComponent();
    }
}

But you also need to do it in the other 'part', i.e. in XAML, so in WindowB's XAML file, the root Window tag needs to be changed to:

<wpfApp:WindowA x:Class="WpfApp.WindowB"
        wpfApp:WindowA ="clr-namespace:WpfApp"

(you'll need to change the namespace appropriately)

This will generate the other 'part' deriving from WindowA, so there will be no inconsistency.

The InitializeComponent() method should be called in each derived class' constructor, as it's specific to that class, i.e. in this case, the other 'part' is generated from the XAML, and defines an InitializeComponent() with resourceLocator code that specific to WindowB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top