There are a number of pop-up windows in the project, which are naturally different from the window's own borders (including maximizing, minimizing, and closing buttons), but the outer border of each pop-up box is the same. For one of the windows, we want to remove the window border and put three buttons in the top right corner and write their click events, but it would be too much if each pop-up window was done this way. We want to avoid duplication of work, the most natural association with the "Inheritance." But WPF has got us into some trouble and has been frustrated a few times. Today after 2 hours of fighting, finally finished, share.
Frustration 1, compile error on inheritance
Suppose we write a parent window class of Basewindow, corresponding to BaseWindow.cs and Basewindow.xaml, to inherit its window for Window1, In response to Window1.cs and Window1.xaml, we often do the following statements in the code that will be automatically generated by VS for us:
public partial class Window1:window
Modified into:
public partial class Window1:basewindow
But after compiling, you'll get an error: Window1 has different base classes.
This is because in the 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=" Height= ">"
<grid x:name= "LayoutRoot"/>
</Window>
Our window inherits the window class and opens Window1.g.cs to see this (this is an intermediate file generated by vs automatically, which you can jump to by "go To Definition" on the Window1 InitializeComponent () method. can also be found in the obj "Debug directory." This allows our Window1 to inherit both window and Basewindow classes, and multiple inheritance is not allowed.
So naturally, you need to modify the Window1.xaml to change the root "window" into 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-namespac E:inheritwindowdemo "
height="
width= ">
<Grid>
</Grid>
</" Src:basewindow>
Thought, this can compile pass, sorry, no, and get another compile error: Src:basewindow cannot be the root of a XAML file because it is defined by XAML, and the way I avoid this problem is to let Basewindow just define it in C # (that is, No Basewindow.xaml, only BaseWindow.cs).
OK, compile smoothly, inherit successfully.