Polymorphism in WPF

Here is my desire to create something abstract in WPF. You have a main window (for example, the main one, even if it is not correct, we don’t care) with two buttons. These 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 edit them as follows:

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

    ...
}

public partial class B : A
{
    public B()
    {
        InitializeComponent(); // Since it 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 "should not indicate different base classes."

Removing the "partial" from class A gives: "error CS0260: There is no partial modifier when declaring type" namespace.A ", there is another partial declaration of this type."

, "partial" , - (, xaml cs one), , , . , B ?

(), Windows Forms, xaml , , . , , , xaml, , ": ". , ?

, WPF, , .

!

+4
2

XAML:

A:

public abstract class A : Window { }

B:

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

xaml 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"...
+2

# - , . "" Visual Studio , InitializeComponent()

Window, XAML. .

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

WindowB WindowA:

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

, XAML, WindowB XAML Window :

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

( )

"" , WindowA, .

InitializeComponent() , , "" XAML InitializeComponent() resourceLocator WindowB.

+1

Source: https://habr.com/ru/post/1539440/


All Articles