gpt4 book ai didi

wpf - 为什么 XAML 被编译成 BAML 而不是 C#

转载 作者:行者123 更新时间:2023-12-03 10:15:00 26 4
gpt4 key购买 nike

据我所知,在 XAML 中所做的一切都可以在 C# 中完成。

为什么 XAML 是在 BAML 中编译而不是在 C# 中?在编译时解析 XAML 并创建相应的 C# 代码不是更有效吗?

最佳答案

这篇博文应该提供一个全面的答案:http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/05/25/Compiled-XAML-3D00-BAML-not-IL.aspx

Yesterday I lectured about XAML and the following question was asked: Why XAML is compiled into BAML and not directly into IL for better performance?

Before giving the correct answer, I want to explain what BAML is.

There are actually two ways for handling a XAML file: Loose or Compiled.

  1. Loose XAML file should be parsed at runtime, and can be deployed as a simple XML file, locally, remotely or embedded into the assembly.
  2. Compiled is a XAML file marked as “Page” in Visual Studio ( in MSBuild), deployed as a BAML (Binary Application Markup Language) file and embedded as an assembly resource.

A loose XAML file can’t include the x:Class XAML keyword, also it can’t embed a source code, nor it can emit code by all means. It is possible to load a loose XAML file by calling XamlReader.Load() method, casting the return value into the root element. The loose XAML version provides a dynamic way to load and change the view, but provides poor performance due to the fact that the XML file is parsed at runtime.

A compiled XAML file (BAML) can emit code, by using x:Class, or by registering events for example. It is possible to load an element from inside the BAML by calling the Application.LoadComponent(), casting the return value into the root element. The compiled XAML version provides better performance since it is pre-tokenized binary version of the XAML file, hence it is smaller and can be loaded faster, but it is not dynamic.

Once upon a time, there was CAML. CAML was the exact IL version of the compiled XAML file. Unfortunately, the WPF team has decided to eliminate it, and keep the BAML version for the following reasons:

  1. BAML is compact hence it can be downloaded faster (good for XBAP applications)
  2. BAML is less security threat than code execution (good for XBAP applications)
  3. BAML can be localized after compilation

In the bottom line, BAML is a little slower than IL but has more advantages than CAML.

关于wpf - 为什么 XAML 被编译成 BAML 而不是 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1423728/

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