Xamarin. 表單 XAML 基本概念Xamarin.Forms XAML Basics

下載範例 下載範例Download Sample Download the sample

可延伸應用程式標記語言(XAML)是由 Microsoft 建立的 XML 語言,做為程式碼的替代方案,用於將物件具現化及初始化,以及在父子式階層中組織這些物件。The eXtensible Application Markup Language (XAML) is an XML-based language created by Microsoft as an alternative to programming code for instantiating and initializing objects, and organizing those objects in parent-child hierarchies. XAML 已適應 .NET framework 中的數種技術,但它在定義 Windows Presentation Foundation (WPF)、Silverlight、Windows 執行階段和通用 Windows 中的使用者介面配置時,找到其最大的公用程式平臺(UWP)。XAML has been adapted to several technologies within the .NET framework, but it has found its greatest utility in defining the layout of user interfaces within the Windows Presentation Foundation (WPF), Silverlight, the Windows Runtime, and the Universal Windows Platform (UWP).

XAML 可讓開發人員使用標記而不是程式碼,在 Xamarin 中定義使用者介面。XAML allows developers to define user interfaces in Xamarin.Forms applications using markup rather than code. 在 Xamarin 的程式中,絕對不需要 XAML,但通常比對等程式碼更簡潔且更具視覺效果,而且可能會 toolable。XAML is never required in a Xamarin.Forms program, but it is often more succinct and more visually coherent than equivalent code, and potentially toolable. XAML 非常適合搭配熱門的 MVVM (模型-View-ViewModel)應用程式架構使用: XAML 會定義透過 XAML 型資料系結連結至 ViewModel 程式碼的視圖。XAML is well suited for use with the popular MVVM (Model-View-ViewModel) application architecture: XAML defines the View that is linked to ViewModel code through XAML-based data bindings.

在 XAML 檔案中,Xamarin Forms 開發人員可以使用所有的 Xamarin、版面配置和頁面,以及自訂類別來定義使用者介面。Within a XAML file, the Xamarin.Forms developer can define user interfaces using all the Xamarin.Forms views, layouts, and pages, as well as custom classes. XAML 檔案可以在可執行檔中編譯或內嵌。The XAML file can be either compiled or embedded in the executable. 不論是哪一種方式,XAML 資訊都會在組建時間進行剖析,以找出已命名的物件,並在執行時間再次具現化和初始化物件,以及在這些物件和程式設計程式碼之間建立連結。Either way, the XAML information is parsed at build time to locate named objects, and again at runtime to instantiate and initialize objects, and to establish links between these objects and programming code.

XAML 與對等程式碼有數個優點:XAML has several advantages over equivalent code:

  • XAML 通常會比對等程式碼更簡潔且更容易閱讀。XAML is often more succinct and readable than equivalent code.
  • XML 中固有的父子式階層可讓 XAML 以更清楚明瞭的方式模擬使用者介面物件的父子式階層。The parent-child hierarchy inherent in XML allows XAML to mimic with greater visual clarity the parent-child hierarchy of user-interface objects.
  • XAML 可以輕鬆地由程式設計人員撰寫,但也會讓它本身 toolable,並由視覺化設計工具產生。XAML can be easily hand-written by programmers, but also lends itself to be toolable and generated by visual design tools.

還有一些缺點,主要與標記語言內建的限制有關:There are also disadvantages, mostly related to limitations that are intrinsic to markup languages:

  • XAML 不能包含程式碼。XAML cannot contain code. 所有事件處理常式都必須在程式碼檔中定義。All event handlers must be defined in a code file.
  • XAML 不能包含重複處理的迴圈。XAML cannot contain loops for repetitive processing. (不過,有數個 Xamarin 視覺物件(最值得注意的ListView )可以根據其 ItemsSource 集合中的物件產生多個子系)。(However, several Xamarin.Forms visual objects—most notably ListView —can generate multiple children based on the objects in its ItemsSource collection.)
  • XAML 不能包含條件式處理(不過,資料系結可以參考以程式碼為基礎的系結轉換器,可有效地允許某些條件式處理)。XAML cannot contain conditional processing (However, a data-binding can reference a code-based binding converter that effectively allows some conditional processing.)
  • XAML 通常無法具現化未定義無參數的函式的類別。XAML generally cannot instantiate classes that do not define a parameterless constructor. (不過,有時候會有一種方法會圍繞此限制)。(However, there is sometimes a way around this restriction.)
  • XAML 通常無法呼叫方法。XAML generally cannot call methods. (同樣地,這種限制有時可以克服)。(Again, this restriction can sometimes be overcome.)

在 Xamarin. Forms 應用程式中,尚未提供可產生 XAML 的視覺化設計工具。There is not yet a visual designer for generating XAML in Xamarin.Forms applications. 所有 XAML 都必須手動撰寫,但有Xaml 預覽器。All XAML must be hand-written, but there is a XAML Previewer. XAML 的新程式設計人員可能會想要經常建立及執行其應用程式,特別是在任何可能不正確的專案之後。Programmers new to XAML might want to frequently build and run their applications, particularly after anything that might not be correct. 即使是在 XAML 中具有大量經驗的開發人員,也知道測試是有益的。Even developers with lots of experience in XAML know that experimentation is rewarding.

XAML 基本上是 XML,但 XAML 具有一些獨特的語法功能。XAML is basically XML, but XAML has some unique syntax features. 最重要的是:The most important are:

  • Property 元素Property elements
  • 附加屬性Attached properties
  • 標記延伸Markup extensions

這些功能是 XML 延伸模組。These features are not XML extensions. XAML 完全是合法的 XML。XAML is entirely legal XML. 但是,這些 XAML 語法功能以獨特的方式使用 XML。But these XAML syntax features use XML in unique ways. 下列文章將詳細討論它們,其中最後會介紹如何使用 XAML 來執行 MVVM。They are discussed in detail in the articles below, which conclude with an introduction to using XAML for implementing MVVM.

需求Requirements

本文假設您已熟悉 Xamarin. Forms。This article assumes a working familiarity with Xamarin.Forms. 本文也假設您已熟悉 XML,包括瞭解 XML 命名空間宣告的用法,以及詞彙元素標記屬性This article also assumes some familiarity with XML, including understanding the use of XML namespace declarations, and the terms element, tag, and attribute.

當您熟悉 Xamarin. Forms 和 XML 時,請開始閱讀第1部分。使用 XAML 消費者入門When you're familiar with Xamarin.Forms and XML, start reading Part 1. Getting Started with XAML.

Channel 9YouTube 上尋找更多 Xamarin 影片。Find more Xamarin videos on Channel 9 and YouTube.