0

Is there any chance to add an abstract class to a wpf window?

Heres my Code so far:

My window:

 public partial class VORLAGE : Window, IWindow, IDisposable
    {
       //SOME CODE
    }

IWindow.cs:

public interface IWindow
    {
        void Open(MachineOrder machineOrder, Boolean showWindow);
        void OpenEditMode(MachineOrder machineOrder);
        void Close();
        void OpenManualOrder();
    }

There are a lot of windows based on that "VORLAGE" window and i want to add an abstract class to define some base functionality. Is there a way to achieve this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tommehh
  • 872
  • 1
  • 17
  • 44
  • 1
    Am I understanding the question well? Create the abstract class, and let Window be it's base class. – ntohl Jan 22 '16 at 09:00
  • I have a lot of windows which do the same thing. and i want to implement an abstract class to the window where i can define some functions – Tommehh Jan 22 '16 at 09:06

2 Answers2

3

You should be able to just make your VORLAGE class abstract, and then make the other windows that share its functionality be child classes. For example:

// VORLAGE class containing the shared base code
public abstract partial class VORLAGE : Window, IWindow, IDisposable
{
    // Shared base code
}

// Your multiple window classes which utilize the same base code
public partial class MyVorlageWindowA : VORLAGE
{
    // Specific concrete code
}

public partial class MyVorlageWindowB : VORLAGE
{
    // Different, specific concrete code
}
William Thomas
  • 2,108
  • 3
  • 22
  • 32
  • sounds great, but i get an error: `partial declarations of must not specify different base classes` – Tommehh Jan 22 '16 at 09:14
  • It's been a while since I messed with XAML and partial classes, so I'm a bit rusty. Try leaving your concrete classes as partial, but take the partial keyword out of the abstract VORLAGE class definition. – William Thomas Jan 22 '16 at 09:16
  • 1
    the xaml's designer code should inherit from VORLAGE not Window. That's the other part of partial – ntohl Jan 22 '16 at 09:16
  • the windows does not look excactly the same. is there a chance to overtake just a part of the designer? – Tommehh Jan 22 '16 at 09:21
  • That's the idea of using abstract classes. Your abstract class should serve as the template, containing only the same, shared code that all of your actual window implementations will use. Then, in your concrete windows, you will program the specific components that make each window implementation different from one another. – William Thomas Jan 22 '16 at 09:33
  • i cant resolve this error `partial declarations of must not specify different base classes` – Tommehh Jan 22 '16 at 09:35
  • If you post the XAML code where you have designed one of the concrete windows, along with the actual abstract and concrete classes you're having issues with, it would probably help us out. – William Thomas Jan 22 '16 at 09:50
  • I tried doing it that way: [link](http://stackoverflow.com/questions/7174315/understanding-wpf-deriving-window-class) but how can i set the style to WindowBase.cs if it doesnt have a designer? – Tommehh Jan 22 '16 at 10:00
  • WindowBase doesn't need a designer because it is not the concrete partial class being referenced as the x:Class of the XAML element. If you look at the XAML element in that answer, the x:Class attribute is set to the concrete partial class (MyVorlageWindowA in my answer) and the element's base class is set to the abstract base (VORLAGE in my answer). – William Thomas Jan 22 '16 at 10:05
  • so i cant set a button (for example btnProduce) to every window based on WindowBase.cs? – Tommehh Jan 22 '16 at 10:08
0
  1. You have to inherit from Window, you don't have to inherit required features from abstract. You can bring those features as a sub component of that window, maybe using IoC.
  2. You can make MyBaseWindow inheriting from Window, IWindow, ... and you can inherit other windows from that MyBaseWindow...
efaruk
  • 882
  • 9
  • 25