XAML 文件的编译 (WPF)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21357330/
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 10:36:18  来源:igfitidea点击:

Compilation of XAML files (WPF)

wpfxaml

提问by canahari

I would like to understand the compilation process of XAML files. Sorry for putting this question here but I really did not find any resource explaining this process in-depth.

我想了解XAML文件的编译过程。很抱歉把这个问题放在这里,但我真的没有找到任何深入解释这个过程的资源。

I understand that XAML is compiled into a .baml file. But: Is the .baml compiled from the generated .g.cs file? Or is the .baml independent and is the IL code generated from the generated .g.cs AND the original .xaml.cs file - this would explain why MainWindow is partial. Which parts of the XAML declarations go into the BAML file? I would like to know also when the loading of a .baml file takes place (for example when talking about a window). Thanks for the help.

我知道 XAML 被编译成一个 .baml 文件。但是: .baml 是从生成的 .g.cs 文件编译的吗?或者 .baml 是独立的,并且是从生成的 .g.cs 和原始 .xaml.cs 文件生成的 IL 代码 - 这将解释为什么 MainWindow 是部分的。XAML 声明的哪些部分会进入 BAML 文件?我还想知道何时加载 .baml 文件(例如在谈论窗口时)。谢谢您的帮助。

采纳答案by har07

In my understanding based on the reference below, everything declared in XAML gets compiled to BAML; .g.csand .xaml.csfiles compiled to IL; .xaml.csIL generated from codes in .xaml.csfile (obviously), and g.csIL contains codes generated to interact with BAML (instead of IL codes generated from BAML it self).

根据我基于以下参考资料的理解,在 XAML 中声明的所有内容都被编译为 BAML;.g.cs.xaml.cs编译为 IL 的文件;.xaml.cs.xaml.cs文件g.cs中的代码生成的 IL (显然),并且IL 包含生成的与 BAML 交互的代码(而不是从 BAML 生成的 IL 代码它自己)。

Check this blog postfor reference. To summarize, the author said that compilation of XAML happened in 2 steps :

检查此博客文章以供参考。总结一下,作者说XAML的编译分两步:

Step 1. The first step is to compile the XAML files into BAML using the xamlc.exe compiler. For example, if our project includes a file name Window1.xaml, the compiler will create a temporary file named Window1.baml and place it in the obj\Debug subfolder (in our project folder). At the same time, a partial class is created for our window, using the language of our choice. For example, if we're using C#, the compiler will create a file named Window1.g.cs in the obj\Debug folder. The g stands for generated.

The partial class includes three things:

? Fields for all the controls in our window.

? Code that loads the BAML from the assembly, thereby creating the tree of objects. This happens when the constructor calls Initialize Component ().

? Code that assigns the appropriate control object to each field and connects all the event handlers. This happens in a method named Connect (), which the BAML parser calls every time it finds a named object.

Step 2. When the XAML-to-BAML compilation stage is finished, Visual Studio uses the appropriate language compiler to compile our code and the generated partial class files. In the case of a C# application, it's the csc.exe compiler that handles this task. The compiled code becomes a single assembly Window1.exe) and the BAML for each window is embedded as a separate resource.

第 1 步。第一步是使用 xamlc.exe 编译器将 XAML 文件编译为 BAML。例如,如果我们的项目包含一个名为 Window1.xaml 的文件,编译器将创建一个名为 Window1.baml 的临时文件并将其放置在 obj\Debug 子文件夹中(在我们的项目文件夹中)。同时,使用我们选择的语言为我们的窗口创建了一个部分类。例如,如果我们使用 C#,编译器将在 obj\Debug 文件夹中创建一个名为 Window1.g.cs 的文件。g 代表生成。

分部类包括三件事:

? 我们窗口中所有控件的字段。

? 从程序集中加载 BAML 的代码,从而创建对象树。这发生在构造函数调用 Initialize Component() 时。

? 将适当的控制对象分配给每个字段并连接所有事件处理程序的代码。这发生在名为 Connect() 的方法中,BAML 解析器每次找到命名对象时都会调用该方法。

第 2 步。当 XAML 到 BAML 编译阶段完成时,Visual Studio 使用适当的语言编译器来编译我们的代码和生成的部分类文件。对于 C# 应用程序,它是处理此任务的 csc.exe 编译器。编译后的代码成为单个程序集 Window1.exe),并且每个窗口的 BAML 作为单独的资源嵌入。