Johnny
Johnny

Reputation: 3328

c# - inheritance WPF layout - Window from Window

I have problem with my inheritance of 'Window', I don't understand what the problem is?

I think, my layout (MediaLibrary.xaml) have to inherit of MainWindow... But I don't know how do that :/

There are 2 class:

MainWindow.xaml

<Window x:Class="WindowsMediaPlayerV2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyWindowsMediaPlayer v2" Height="350" Width="525" MinHeight="350" MinWidth="525">
    <Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

namespace WindowsMediaPlayerV2
{
    public partial class MainWindow : Window
    {

        public MediaLibrary myMediaLibrary = new MediaLibrary();

        public MainWindow()
        {
            InitializeComponent();
        }
     }
}

MediaLibrary.xaml

<Window x:Class="WindowsMediaPlayerV2.MediaLibrary"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MediaLibrary" Height="350" Width="300" MinHeight="350" MinWidth="300" Closing="Window_Closing">
    <Grid>
    </Grid>
</Window>

MediaLibrary.xaml.cs

namespace WindowsMediaPlayerV2
{
    public partial class MediaLibrary : MainWindow //problem here when I run
    {
        public MediaLibrary()
        {
            InitializeComponent();

        }
     }
}

Error when I run:

FR: Les déclarations partielles de 'WindowsMediaPlayerV2.MediaLibrary' ne doivent pas spécifier des classes de base différentes

EN: Partial declarations of 'WindowsMediaPlayerV2.MediaLibrary' must not specify different base classes Can we help my ? Thanks

Upvotes: 0

Views: 3223

Answers (1)

DRapp
DRapp

Reputation: 48179

You can not subclass a visual .XAML class such as you are attempting. Only classes that are built in your C# code can be inherited.

Now, that said, you can create your own visual theme of a window with respect to style, colors, etc (or do in code), then build your classes upon that class from code.

public class MyWindow : Window
{
   public class MyWindow()
   {
      SomeProperty = SomeValue;
   }

   protected void SomeCustomFunction(int AnyParameter)
   {
      SomethingCommon = AnyParameter;
   }

   etc...
}

Build your project so this class is then known/available for derivation purposes.

Now, when you create your .XAML-based Window, let it create the default based on "Window". Then, modify both your .XAML.cs and your .cs versions and change the reference to YOUR "MyWindow" class something like...

From

<Window x:Class="blah...

to

<myLib:MyWindow x:Class="blah
   xmlns:myLib="clr-namespace:MyWpfClassLibrary"

in the XAML, you will also need to add reference to your class library namespace, something like... the xmlns if your window class is in another project/namespace. The "myLib" is like an "alias" to that class library so it can be used within the rest of the XAML, it knows how/where to resolve the class references.

In the .cs code, change

public partial class blah : Window

to

public partial class blah : MyWindow

If your class library is in the same namespace, you should be good to go. If not, you can either add a

using MyLibrary;   before the public partial class -- OR

public partial class blah : MyLibrary.MyWindow

Upvotes: 3

Related Questions