简体   繁体   English

为什么MainWindow的根元素是Window而不是MainWindow?

[英]Why is the root element of MainWindow Window instead of MainWindow?

I notice that the root element in any XAML file (in WPF) seems to be one of:我注意到任何 XAML 文件(在 WPF 中)中的根元素似乎是以下之一:

  • Window
  • Page
  • UserControl
  • ResourceDictionary
  • Application

I tried to change the root element to local:MainWindow , but then the project cannot compile, saying the base class of a partial class should be the same.我试图将根元素更改为local:MainWindow ,但是项目无法编译,说部分 class 的基数 class 应该相同。 Then I guess the root element is the base class of the actual class?那么我猜根元素是实际 class 的基数 class? What is the reason for it?这是什么原因呢? Since the root element cannot be changed to the actual class, I cannot access the dependency properties written in MainWindow.xaml.cs .由于无法将根元素更改为实际的 class,因此我无法访问MainWindow.xaml.cs中编写的依赖属性。 How can those DPs be referenced in XAML?如何在 XAML 中引用这些 DP?

Besides, I also notice that some third-party themes also provide special window classes, and in that case, the root element is often changed.此外,我还注意到一些第三方主题还提供了特殊的 window 类,在这种情况下,根元素经常会发生变化。 How is this being achieved?这是如何实现的? Eg GlowWindow from HandyControl例如来自 HandyControl 的 GlowWindow

I tried to change the root element to local:MainWindow, and then the project cannot compile, saying the base class of a partial class should be the same.我尝试将根元素更改为local:MainWindow,然后项目无法编译,说部分class的基数class应该相同。 Then I guess the root element is the base class of the actual class?那么我猜根元素是实际 class 的基数 class?

Yes, this is correct, the root element is the base class that can , but does not necessarily have to be specified in code-behind.是的,这是正确的,根元素是基数 class可以,但不一定必须在代码隐藏中指定。 The connection between the partial classes (compiled from XAML markup and the code-behind file) is specified using the x:Class attribute in XAML, see Code-Behind and XAML in WPF .部分类之间的连接(从 XAML 标记和代码隐藏文件编译)使用 XAML 中的x:Class属性指定,请参阅代码隐藏和 WPF 中的 XAML

The XAML language includes language-level features that make it possible to associate code files with markup files, from the markup file side. XAML 语言包括语言级别的功能,可以从标记文件端将代码文件与标记文件相关联。 Specifically, the XAML language defines the language features x:Class Directive, x:Subclass Directive, and x:ClassModifier Directive.具体来说,XAML 语言定义了语言特性x:Class指令、 x:Subclass指令和x:ClassModifier指令。 [...] [...]

The partial class must derive from the type that backs the root element.部分 class必须派生自支持根元素的类型。


What is the reason for it?这是什么原因呢?

A code-behind file is not mandatory if there is no custom code, see x:Class .如果没有自定义代码,则代码隐藏文件不是必需的,请参阅x:Class

In existing programming models that use x:Class , x:Class is optional in the sense that it is entirely valid to have a XAML page that has no code-behind.在使用x:Class的现有编程模型中, x:Class是可选的,因为拥有一个没有代码隐藏的 XAML 页面是完全有效的。 However, that capability interacts with the build actions as implemented by frameworks that use XAML.但是,该功能与使用 XAML 的框架实现的构建操作交互。

Even if there is one, the base class can be omitted , but then again the base class must be determined somehow and that is done through the root element type, see Code-behind, Event Handler, and Partial Class Requirements in WPF .即使有一个,基数 class也可以省略,但是基数 class 必须以某种方式确定,这是通过根元素类型完成的,请参阅WPF 中的代码隐藏、事件处理程序和部分 Class 要求

Note that under the default behavior of the markup compile build actions, you can leave the derivation blank in the partial class definition on the code-behind side.请注意,在标记编译构建操作的默认行为下,您可以在代码隐藏端的部分 class 定义中将派生留空。 The compiled result will assume the page root's backing type to be the basis for the partial class, even if it not specified.编译结果将假设页面根的支持类型是部分 class 的基础,即使它没有指定。


How can those DPs be referenced in xaml?如何在 xaml 中引用这些 DP?

Simply use a Binding with a RelativeSource that specifies the MainWindow as AncestorType .只需将Binding与将MainWindow指定为AncestorTypeRelativeSource一起使用。

{Binding YourDependencyProperty, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}

If you assign an x:Name to your window, you could alternatively use ElementName in the binding.如果您将x:Name分配给您的 window,您也可以在绑定中使用ElementName

<Window ...
        x:Name="MyMainWindow">
{Binding YourDependencyProperty, ElementName=MyMainWindow}

Besides, I also notice that some third-party themes also provide special Window classes, and in that case, the root element is often changed.此外,我还注意到一些第三方主题还提供了特殊的 Window 类,在这种情况下,根元素经常会发生变化。 How is this being achieved?这是如何实现的?

No it is not.不它不是。 For instance, the GlowWindow is exactly defined like your MainWindow with code-behind and x:Class to refer to it and Window as root element.例如, GlowWindow定义与您的MainWindow完全相同,带有代码隐藏和x:Class来引用它, Window作为根元素。 What you see in the link is that a new derivative of the GlowWindow is created, just like you create MainWindow from Window , they just happen to use the same name GlowWindow , unfortunately.您在链接中看到的是创建了GlowWindow的新派生,就像您从Window创建MainWindow一样,不幸的是,它们恰好使用相同的名称GlowWindow Notice the namespaces.注意名称空间。

GlowWindow XAML 标记。

Try it yourself and create a new window by specifying your MainWindow as root element.亲自尝试并通过将MainWindow指定为根元素来创建一个新的 window。 It is exactly the same scenario.这是完全相同的场景。

I will supplement the answer from @thatguy.我将补充@thatguy 的答案。

In WPF, it is customary to separate the logic part of the control (which is written in Sharpe) from the visual part (which is written in XAML in the theme template).在WPF中,习惯上将控件的逻辑部分(用Sharpe写)和视觉部分(在主题模板中用XAML写)分开。

For your example, creating a template is redundant.对于您的示例,创建模板是多余的。 But it could be done like this:但它可以这样做:

    public class MainWindowBase : Window
    {
        public int SomeProperty
        {
            get { return (int)GetValue(SomePropertyProperty); }
            set { SetValue(SomePropertyProperty, value); }
        }

        public static readonly DependencyProperty SomePropertyProperty =
            DependencyProperty.Register("SomeProperty", typeof(int), typeof(MainWindowBase), new PropertyMetadata(0));
    }

    public partial class MainWindow : MainWindowBase
    {
        public MainWindow()
        {
            InitializeComponent();
<local:MainWindowBase x:Class="****.MainWindow"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM