从 WPF 中的自定义窗口继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14373729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:05:19  来源:igfitidea点击:

Inheriting from custom window in WPF

wpfwindow

提问by Idov

I have a custom window in WPF which I want to use as a base window for other windows.
When I tried to inherit it, I wrote in the XAML:

我在 WPF 中有一个自定义窗口,我想将其用作其他窗口的基本窗口。
当我试图继承它时,我在 XAML 中写道:

<my:MyWindow x:Class="NewWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:my="clr-namespace:MyNamesapce;assembly=MyAssembly"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

In the .cs code I wrote:

在我写的 .cs 代码中:

namespace SomeOtherNamespace
{
    public partial class NewWindow: MyWindow
    {
        internal NewWindow(Control ctrl) : base(ctrl)
        {
            InitializeComponent();
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
        }
    }
}

But then I got the error:

但后来我得到了错误:

cannot be the root of a XAML file because it was defined using XAML.

不能是 XAML 文件的根,因为它是使用 XAML 定义的。

What am I doing wrong and how can I fix it?

我做错了什么,我该如何解决?

回答by Wiley Marques

If what you are trying to achieve is setting ResizeModeto NoResizein every window you could use a style like this:

如果您想要实现的是在每个窗口中设置ResizeModeNoResize,您可以使用这样的样式:

<Style TargetType="Window" x:Key="windowStyle">
    <Setter Property="ResizeMode" Value="NoResize" />
</Style>

Put this style in a ResourceDictionary and make it be the window style:

将此样式放入 ResourceDictionary 并使其成为窗口样式:

Style="{StaticResource windowStyle}"

But if you want to go further you'll have to make a new class inheriting from Window

但是如果你想更进一步,你必须创建一个继承自 Window 的新类

public class MyWindow : Window
{
    public MyWindow()
    {
        this.ResizeMode = ResizeMode.NoResize;
    }
}

Now you are able to instanciate a new MyWindow

现在您可以实例化一个新的 MyWindow

<mn:MyWindow x:Class="Project.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mn="clr-namespace:MyControls"
        Height="300" Width="300">
</mn:MyWindow>

Be aware the class that will be the "code behind" of this new window need to inherit from your new MyWindowclass as below:

请注意,将成为此新窗口“代码隐藏”的MyWindow类需要从您的新类继承,如下所示:

namespace Project
{
    public partial class Window1 : MyControls.MyWindow
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}