Xamarin.Forms 中 XAML 相对于 C#(反之亦然)的优势? [英] Benefits of XAML over C# (or vice versa) in Xamarin.Forms?

查看:44
本文介绍了Xamarin.Forms 中 XAML 相对于 C#(反之亦然)的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xamarin.Forms 中,有两种方法可以构建应用程序的布局:在 C# 中,或混合使用 XAML 和 C#.一个比另一个有任何明确的好处吗?

Xamarin 应该以相同的方式编译到各自的平台应用程序(对于 droid、iOS、usw)中,所以我能看到的唯一区别是 XAML 将设计和代码拆分为单独的文件(.xaml 和 .xaml.cs),而仅使用 C# 的方式可避免 Visual Studio 代码抱怨 .xaml.cs 文件中的编译时生成的函数.有什么我错过的吗?当 C# 代码对我来说已经很直观时,有一个很好的理由可以流利地阅读 XAML 标记?

解决方案

XAML 提供了更简洁优雅的用户定义界面,并具有更好地模仿树的视觉结构页面上视觉元素的组织.XAML 也是通常比等效代码更易于维护和修改.

<小时><块引用>

声明式 UI (XAML) 是一种比通过代码制作 UI 更好的方法,出于以下原因:

  • 恰到好处,没有人情味除了少数事情,XAML 对每个人来说看起来都一样.你不能对代码隐藏 C# 编码说同样的话

  • 更易于维护

    比代码更容易阅读和修改

  • 它有助于在 UI 和逻辑之间明确分离关注点

  • 工具友好;不幸的是,今天的 Xamarin Forms 仍然没有设计师,但这应该会改变

我并不是说 XAML 是完美的.C#代码也不错.

<小时>

阅读一些类似的讨论:

In Xamarin.Forms there's 2 ways to build an app's layout: in C#, or using a mix of XAML and C#. Is there any definite benefit of one over the other?

Xamarin should compile either into the respective platform apps (for droid, iOS, usw) the same, so the only differences I can see are that XAML splits the design and the code into separate files (.xaml and .xaml.cs), while the C#-only way avoids visual studio code complaining about the compiletime-generated functions in the .xaml.cs files. Is there anything I missed? A good reason to become fluent in reading the XAML markup when the C#-code is already intuitive for me?

解决方案

XAML provides a more succinct and elegant definition of the user interface, and has a visual structure that better mimics the tree organisation of the visual elements on the page. XAML is also generally easier to maintain and modify than equivalent code.


A declarative UI(XAML) is a better approach than doing the UI by code, for the following reasons:

  • it's coincise, it's impersonal except few things, XAML looks the same for everyone. you can't say the same thing about code-behind C# coding

  • it's more maintainable

    much more easier to read and modify than code

  • it helps with a clear separation of concerns, between the UI and logic

  • tooling friendly; unfortunately Xamarin Forms today still doesn't have a designer, but this is supposed to change

I am NOT saying XAML is perfect. C# code is also good.


Read some similar discussions :

这篇关于Xamarin.Forms 中 XAML 相对于 C#(反之亦然)的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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