XAML 和 QML 等声明式 UI 语言的实际优势是什么? [英] What are the real-world benefits of declarative-UI languages such as XAML and QML?

查看:16
本文介绍了XAML 和 QML 等声明式 UI 语言的实际优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在评估 QtQuick (Qt 用户界面创建工具包)将作为 Qt 4.7 的一部分发布.QML 是 QtQuick 背后基于 JavaScript 的声明性语言.

I'm currently evaluating QtQuick (Qt User Interface Creation Kit) which will be released as part of Qt 4.7. QML is the JavaScript-based declarative language behind QtQuick.

这似乎是一个非常强大的概念,但我想知道是否有人广泛使用其他更成熟的声明式 UI 语言,如 XAML 可以让您深入了解可以从这种编程风格中获得的实际好处.各种优点经常被引用:

It seems to be a very powerful concept, but I'm wondering if anybody that's made extensive use of other, more mature declarative-UI languages like XAML in WPF or Silverlight can give any insight into the real-world benefits that can be gained from this style of programming. Various advantages are often cited:

  • 发展速度
  • 强制分离表示和逻辑
  • 编码人员和设计师之间的更好集成
  • UI 更改不需要重新编译

另外,有什么缺点吗?我想到了一些潜在的关注领域:

Also, are there any downsides? A few potential areas of concern spring to mind:

  • 执行速度
  • 内存使用情况
  • 增加了复杂性

还有其他需要考虑的因素吗?

Are there any other considerations that should be taken into account?

推荐答案

(已更新)

对 XAML 的误解是它没有被编译.它确实被编译为 BAML 二进制预标记 XAML.显然有一个 IL 编译版本的 XAML 也称为 CAML.OP 向我指出了这个 好文章解释什么是 XAML/BAML 和 CAML.

The misconception with XAML is that it's not compiled. It is indeed compiled down to BAML a binary pre-tokenized XAML. Apparently there was a IL compiled version of XAML too called CAML. The OP pointed me to this good article explaining what XAML/BAML and CAML are.

无论如何,关于为什么要使用它的问题:

Anyway, to the question why to use it:

XAML 只是 C# 对象的序列化格式,它特别适合描述分层对象结构,例如 WPF GUI 中的结构.

XAML is simply a Serialization Format for C# objects that it is particularly well suited to describe hierarchical object structures, like found in WPF GUIs.

WPF 可以帮助您编写不那么枯燥的 C# 代码,如下所示:

What WPF helps you do is write less boring C# code like this:

var grid = new Grid();
grid.Content.add(new TextBlock() {Text = "Hello"});
grid.Content.add(new TextBlock() {Text = "World"});

并以更易读的方式表达,如下所示:

and just express it in a more readable way like this:

<Grid>
  <TextBlock Text="Hello">
  <TextBlock Text="World">
</Grid>

由于 WPF 对象嵌套(将内容放入其他对象中)可能会变得非常深,因此 WPF 使其比生成的 C# 代码更易于阅读.

Since WPF object nesting (putting stuff inside other objects) can get very deep, WPF makes it much easier to read than the resulting C# code.

至于关注点分离:XAML 在这里也有帮助,因为它只允许您表达对象及其关系/属性,而不是逻辑.这迫使您将逻辑与 UI 布局分开.MVVM 模式非常适合这项任务,并允许简单的可测试性和可互换的视图.

As for separation of concerns: XAML helps here too since it does only allow you to express objects and their relationships/properties, rather than logic. That forces you to separate logic from UI layout. The MVVM Pattern is very well suited for this task and allows for eay testability and interchangeable Views.

XAML 中增加的复杂性也很容易被忽略,因为 C# 中的相同代码很容易变得比 XAML 标记更复杂.

Added complexity in XAML can be also easily dismissed because the same code in C# gets easily more complex than the XAML markup.

不过,我无法让您深入了解 QTQuick.对不起

I can't give you any insight into QTQuick though. Sorry

这篇关于XAML 和 QML 等声明式 UI 语言的实际优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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