为什么XAML被编译为BAML而不是C# [英] Why XAML is compiled into BAML and not in C#

查看:432
本文介绍了为什么XAML被编译为BAML而不是C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在XAML中完成的所有操作都可以在C#中完成.

As far as I know, everything done in XAML can be done in C#.

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

Why XAML is compiled in BAML and not in C# ? Wouldn't be more efficient to parse the XAML at compile-time and create the corresponding C# code ?

推荐答案

此博客文章应提供全面的答案:

This blog post should provide a comprehensive answer: http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/05/25/Compiled-XAML-3D00-BAML-not-IL.aspx

昨天我讲了有关XAML的演讲,并提出了以下问题:为什么XAML被编译为BAML而不是直接编译为IL以提高性能?

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?

在给出正确答案之前,我想解释一下BAML是什么.

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

实际上,有两种处理XAML文件的方法:松散或已编译.

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

  1. 松散的XAML文件应在运行时进行解析,并且可以作为简单的XML文件部署在本地,远程或嵌入到程序集中.
  2. 已编译的XAML文件在Visual Studio(在MSBuild中)中标记为页面",部署为BAML(二进制应用程序标记语言)文件并作为程序集资源嵌入.

松散的XAML文件不能包含 x:Class XAML关键字,也不能嵌入源代码,也不能通过任何方式发出代码.通过调用XamlReader.Load()方法并将返回值强制转换为根元素,可以加载松散的XAML文件.松散的XAML版本提供了一种动态方式来加载和更改视图,但是由于XML文件是在运行时进行解析的,因此性能很差.

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.

已编译的XAML文件(BAML)可以通过使用 x:Class 或注册事件来发出代码.可以通过调用 Application.LoadComponent()从BAML内部加载元素,并将返回值转换为根元素.编译后的XAML版本具有更好的性能,因为它是XAML文件的预加令牌的二进制版本,因此它更小并且可以更快地加载,但是它不是动态的.

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.

从前,有CAML. CAML是已编译XAML文件的确切IL版本.不幸的是,WPF团队决定删除它,并保留BAML版本,原因如下:

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紧凑,因此可以更快地下载(适用于XBAP应用程序)
  2. 与代码执行相比,BAML对安全性的威胁要小(对于XBAP应用程序而言很好)
  3. BAML可以在编译后本地化
  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

最重要的是,BAML比IL慢,但比CAML具有更多优势.

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

这篇关于为什么XAML被编译为BAML而不是C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆