[WPF Troubleshooter] inherit custom windows

Source: Internet
Author: User
Tags inheritance xmlns

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.