XAML 和 QML 等声明式 UI 语言在现实世界中有何好处?

发布于 2024-08-29 23:58:32 字数 687 浏览 7 评论 0原文

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

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

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

另外,有什么缺点吗?我想到了一些潜在的问题:

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

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

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.

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:

  • Speed of development
  • Forces separation between presentation and logic
  • Better integration between coders and designers
  • UI changes don't require re-compilation

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

  • Execution speed
  • Memory usage
  • Added complexity

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

無處可尋 2024-09-05 23:58:32

(已更新)

对 XAML 的误解是它未经过编译。它确实被编译为 BAML(一种二进制预标记化 XAML)。显然还有一个 XAML 的 IL 编译版本,称为 CAML。 OP 向我指出了这个 好文章解释了 XAML/BAML 和 CAML 是什么。

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

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

WPF 帮助您编写不那么无聊的 C# 代码,如下所示:

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

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

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

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

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

XAML 中增加的复杂性也可以轻松消除,因为 C# 中的相同代码很容易变得比 XAML 标记更复杂。

不过,我无法让您了解 QTQuick。对不起

(Updated)

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 is simply a Serialization Format for C# objects that it is particularly well suited to describe hierarchical object structures, like found in WPF GUIs.

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>

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.

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.

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

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

凉薄对峙 2024-09-05 23:58:32

QtQuick 可以通过 C++ 插件进行扩展,实际上 Qt 人员的建议是,您在 QtQuick/QML 中执行 UI、动画、转换等,而所有业务逻辑都在 C++/Qt 中。因此,通过这种方式,您可以两全其美,您可以像平常一样调试 C++ 代码,同时使 UI 变得毫不费力且极其简单。

关于 QtQuick/XAML 的另一个重要想法是它们是硬件加速的,因此例如您可以毫不费力地获得相当好的 fps。因此,他们对于实现目标的速度一点也不慢。

它节省了时间,非常多的时间。我在 3 天内用代码做了一个 UI,在 2 小时内用 QML 做了同样的事情。

QtQuick is extensible via C++ plugins, actually what the Qt guys recomment is that you do the UI, Animations, Transitions etc in QtQuick/QML while all of your business logic is in C++/Qt. So this way you get the best of both worlds, you can debug your C++ code like you usually do, while at the same time making UIs becomes effortless and extremely easy.

Also another important think about QtQuick/XAML is that they are hardware accelerated, so for example you can get pretty good fps without any effort. So they are not slow at all for what they set out to accomplish.

It saves time, soo much time. I did a UI with code in 3 days, did the same in QML in 2 hours.

痴者 2024-09-05 23:58:32

声明性编码(即 WPF 或 QTQuick)的要点是在开发人员和可能实现应用程序视觉方面的美工之间提供分离。对于 WPF,我发现调试变得有点困难。就在我们说话的时候,我正在编译最新的 QT 来看看 QTQuick。 (这需要很长时间,我有时间看看 stackoverflow :-) )所以,我对此还没有意见。

The point of declarative coding, i.e. WPF or QTQuick is to provide a separation between the developer and presumably the artist that is implementing the visual aspects of your application. With regards to WPF, I find that debugging gets to be a bit harder. As we speak, I am compiling the latest QT to look at QTQuick. (It takes a long time and I have time to look at stackoverflow :-) ) So, I don't have an opinion on that yet.

因为看清所以看轻 2024-09-05 23:58:32

QML/XAML 是:

  • 非常适合 MVVM 模式
  • 硬件加速(QML 使用适用于 Windows、MAC、Linux 和手机操作系统的 OpenGL...XAML 使用适用于 Windows 及其手机版本的 DirectX)
  • 更接近艺术家
  • 您可以创建出色且漂亮的 UI使用 XAML/QML
  • 更简单的 UI 实现
  • 漂亮的动画是可能的
  • 在 XAML 中,通常您只需稍加更改即可创建应用程序的 Silverlight 版本
  • 在 XAML 中,有一些很棒的功能,例如模板、触发器(DataTrigger、Trigger、EventTrigger)、绑定(在任何一侧,也可以在两侧一起),资源,命令,依赖属性和通知属性。

但请注意XAML:(我是XAML程序员,因此我没有QML积分)

  • XAML调试是不可能的
  • 对于XAML的任何更改,所有程序都必须重新编译
  • 要更加小心性能。例如,如果您在 XAML 中使用过多的 RoutedCommand,您的应用程序将无法使用!

  • 在 XAML 中,某些功能无法按预期工作。不幸的是,有一些技巧。 (应该很清楚...应该按预期工作...不是吗?)

  • 要小心一些类似的命名空间,例如 BitmapEffect 和 Effect。有不同的功能和成本。 (例如,BitmapEffect 对软件渲染有一些影响,Effect 对硬件渲染有一些影响)

  • 在现实世界中,艺术家无法将 WPF 用作 Flash(至少性能良好)。

  • 某些功能适用于特殊位置。例如,DataTrigger 仅在样式标签中工作,而不在资源部分中工作。

  • XAML 存在一些弱点。一些示例:没有任何顺序动画...您无法在 XAML 中进行任何计算(即使是一点点工作,您也必须用 C# 编写转换器!JavaSript 是 QML 中的一个很好的替代品)...某些属性是重复的。例如 x:Name 和 Name...从 ViewModel 控制视图尚不清楚。例如,从 ViewModel 关闭 View(您需要一些 CodeBehind)

  • 运行时错误太多。如果你在错误的地方使用了一些标签,它会注意到你的语法错误,但许多错误只发生在运行时。例如,如果我将 ColorAnimation 的背景属性(而不是 Background.Color)作为目标,它将成功编译,但在运行动画时... BUMP... 运行时错误!在这种情况下,在 Expression Blend 上,应用程序将崩溃!!!

QML/XAML are:

  • Great for MVVM pattern
  • Hardware accelerated (QML with using OpenGL for Windows, MAC, Linux and Phone OSes... XAML with using DirectX for Windows and its phone version)
  • Closer to artists
  • You can create a GREAT and NICE UI using XAML/QML
  • Easier UI implementation
  • Nice animation is possible
  • In XAML, usually you can create a Silverlight version of your application just with a little changes
  • In XAML there is some great features such as Template, Trigger (DataTrigger, Trigger, EventTrigger), Binding (in any side and also both side together), Resource, Commands, DependencyProperty and Notifiable Properties.

But please note in XAML: (I am a XAML programmer, therefore i have not points for QML)

  • XAML debugging is not possible
  • For any change in XAML, all program must be recompile
  • Be more careful for performance. For example if you use much many RoutedCommands in XAML, your application will be unusable!

  • In XAML, some feature not works as expected. There is unfortunately some tricks. (It should be clear... should works as expected... isn't it? )

  • Be careful for some similar namespaces like BitmapEffect and Effect. There is different features and costs. (e.g. BitmapEffect has some effects with software render and Effect has some effect with hardware render)

  • In real world, artists could not use WPF as Flash (at least with good performance).

  • Some features works on special places. For example DataTrigger works just in Style tag not in Resource section.

  • There is some weaknesses in XAML. Some examples: there is not any sequential animation... you cannot do any calculation in XAML (you must write a converter in C# even for a liiiittle work! JavaSript is a great replacement in QML)... some attributes are duplicate. e.g. x:Name and Name... Controlling View from ViewModel is not clear. e.g. closing View from ViewModel (you need some CodeBehind)

  • Tooooooo much run-time errors. If you use some tags in bad place it will notice you for syntax error, but many of errors occurs just in the run-time. e.g. if i target Background property (instead of Background.Color) for ColorAnimation, it will compile successfully, but in running animation... BUMP... runtime error!!! in such case on Expression Blend, application will crash!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文