gpt4 book ai didi

wpf - XAML 文件 (WPF) 的编译

转载 作者:行者123 更新时间:2023-12-02 05:32:29 26 4
gpt4 key购买 nike

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

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

最佳答案

根据我基于以下引用的理解,XAML 中声明的所有内容都会编译为 BAML; .g.cs.xaml.cs 文件编译为 IL; .xaml.cs IL 由 .xaml.cs 文件中的代码生成(显然),而 g.cs IL 包含生成的用于交互的代码BAML(而不是从 BAML 本身生成的 IL 代码)。

检查这个blog post以供引用。总而言之,作者说 XAML 的编译分为 2 个步骤:

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.

关于wpf - XAML 文件 (WPF) 的编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com