Windows inherited by WPF are not as easy as winform and Asp.net
Assume that the parent window class we have written is basewindow, which corresponds to basewindow. CS and basewindow. in XAML, the window to inherit from is window1, corresponding to window1.cs and window1.xaml. The common action we perform is to use vs as the following statement in the automatically generated code:
Public partial class window1: Window
Modify:
Public partial class window1: basewindow
However, after compilation, you will get an error: window1 has different base classes.
This is because in window1.xaml
<Window Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" X: class = "inheritwindowdemo. window1" Width = "300" Height = "300"> <Grid X: Name = "layoutroot"/> </WINDOW> |
Our window inherits the window class and opens window1.g. CS can also see this (this is an intermediate file automatically generated by vs. You can "go to definition" on the initializecomponent () method of window1 to jump to this file, you can also find it in the OBJ "DEBUG directory ). This makes window1 inherit both the window and basewindow classes. Multi-inheritance is not allowed.
Naturally, you need to modify window1.xaml to change the root "window" to our basewindow:
<SRC: basewindow xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" X: class = "inheritwindowdemo. window1" Xmlns: src = "CLR-namespace: inheritwindowdemo" Height = "300" Width = "300"> <Grid> </GRID> </Src: basewindow> |
I thought, you can compile it now. Sorry, no, but I got another compilation error: SRC: basewindow cannot be the root of the XAML file, because it is defined by XAML, currently, the solution I want to avoid this problem is to make basewindow defined only in C # (that is, there is no basewindow. XAML, only basewindow. CS ).