前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UWP基础教程 - XAML开篇

UWP基础教程 - XAML开篇

作者头像
陈仁松
发布2018-03-20 16:30:33
1.8K0
发布2018-03-20 16:30:33
举报
文章被收录于专栏:陈仁松博客陈仁松博客

XAML是英文Extensible Application Markup Language的缩写,中文可以称为“可扩展应用程序标记语言”,是基于Extensive Markup Language(XML)可扩展标记语言,在Windows 10 UWP、Windows 8、Windows Phone、Silverlight以及WPF技术框架下都可以使用XAML的语法作为应用UI界面的开发。

XAML简化了创建UI的过程,使UI编程更加简单明了,在使用XMAL的项目中,以".xaml"作为文件扩展名。每个XAML页面都具有一个后台代码文件xaml.cs文件来控制页面逻辑处理,这就是微软典型的Code-Behind模式的编程方式,这一机制将用户界面设计和后台代码设计分割,这意味着项目组的UI同学可以使用Blend for Visual Studio进行XAML用户界面设计,同时后台代码开发人员可在Visual Studio中共享该XAML文件,并同时设计其后台代码。

根据微软Microsoft Domain-Specific Languages描述

Xaml, the eXtensible Application Markup Language, is a system for representing structured information. This specification defines three aspects of Xaml: ·The Xaml Schema Information Set - a model for defining a particular Xaml vocabulary. ·The Xaml Information Set - a model for describing the information in a Xaml instance. ·The process for converting an XML [XML] document into the corresponding Xaml Information Set, as directed by one or more Xaml Schema Information Sets. This specification does not define any particular Xaml vocabulary.

Xaml, the eXtensible Application Markup Language, is a system for representing structured information. This specification defines two abstract information models: the Xaml Schema Information Set model, and the Xaml Information Set model. The Xaml Information Set (‘Xaml Infoset’ for short) defines the structure of information that a Xaml instance can represent. The Xaml Schema Information Set allows specific Xaml vocabularies to be defined. This specification also defines a set of rules for transforming an XML document into a Xaml Information Set. XML is a common format for Xaml. (The term “Xaml Document” refers to an XML document that represents a Xaml Information Set.) But while this specification does not define any other representations, any physical representation may be used as long as it can represent the information in the Xaml Information Set. This first section of the specification describes the roles of the information sets, how they relate to applications that use Xaml, and how the transformation rules come into play.

XAML基于XML格式,同时也继承了大量XML的概念,其中最基础的就是Element(元素)的定义和Property(属性)的使用。下图演示了一个XAML基础元素的使用以及结构,是不是和XML很相似。所以,如果你熟悉XML,其实已经具备XAML语法基础,那么接下来学习XAML的过程将会事半功倍。

首先我们来看一段UWP项目空白页面的代码

代码语言:javascript
复制
<Page
    x:Class="UWPXaml.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPXaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    </Grid>
</Page>

上面代码声明了若干个命令空间,xmlns特性是XML中的特殊属性,专门用来声明命名空间。一旦声明了命名空间,在该文档任何地方都可以使用。

代码语言:javascript
复制
    xmlns:local="using:UWPXaml"

表示在该XAML里可以通过local标识符来使用UWPXaml控件下的控件或者其他类

大家可能会问那么Grid的命名空间是什么呢?为什么没有标识符前缀?这里我们来看下两个特别的命名空间

代码语言:javascript
复制
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

这两个命名控件在创建所有Win10的UWP应用都会使用到, http://schemas.microsoft.com/winfx/2006/xaml/presentation是UWP的核心命名空间。它包含了大部分用来构建UI的控件,主要包含Windows.UI.Xaml的控件,该命名空间的声明没有前缀,所以它也成为该文档的默认命名空间,所以没有前缀的元素都是自动位于该命名空间下,Grid元素就处于该命名空间下。 http://schemas.microsoft.com/winfx/2006/xaml是XAML的命名空间,包含各种XAML的实用特性,声明以x作为前缀,常用的x.Name就是使用该命名空间来实现的。

定义

描述

x:Key

为 XAML ResourceDictionary 中的每个资源设置一个唯一的用户定义密钥。该密钥的令牌字符串是 StaticResource 标记扩展的参数,你可以在以后使用此密钥在应用 XAML 的其他位置检索其他 XAML 用法中的 XAML 资源。

x:Class

为 XAML 页面提供代码隐藏的类指定代码命名空间和代码类名称。这可为构建你的应用时通过构建操作创建或加入的类命名。这些构建操作支持 XAML 标记编译器,并在编译应用时,将你的标记和代码隐藏文件组合到一起。你必须具有此类,才能支持对 XAML 页面实现代码隐藏。还需要 x:Class,才能使你的 XAML 内容在默认 Windows 运行时激活模型中初始化为 Window.Content。

x:Name

在处理 XAML 中定义的对象元素后,为运行时代码中存在的实例指定一个运行时对象名。你可以将在 XAML 中设置 x:Name 看作是在代码中声明命名变量。稍后你会了解,这是将 XAML 加载为 Windows 运行时应用的一个组件时发生的实际情况。注意  FrameworkElement.Name 是框架中的一个类似属性,并非所有元素都支持它。因此,当 FrameworkElement.Name 在该元素类型上不受支持时,你可以将 x:Name 用于元素标识。

x:Uid

标识某些元素,应将本地化后的资源用于该元素的一些属性值。有关如何使用 x:Uid 的详细信息,请参阅快速入门:翻译 UI 资源。

XAML 固有类型

当属性或资源需要时,这些类型可以为简单的值类型指定值。这些固有类型与通常定义为每个编程语言固有定义的一部分的简单值类型相对应。例如,你可能需要一个表示 true 布尔值的对象,以便在 ObjectAnimationUsingKeyFrames 情节提要视觉状态中使用。对于 XAML 中的该值,可将 x:Boolean 固有类型用作对象元素,例如 <x:Boolean>True</x:Boolean>

XAML命名空间的概念其实和C#代码中的Using类似,XAML的命名空间使得XAML解析器能够准确找到指定的类进行渲染。

关于XAML命名空间就介绍到这里,下一篇将会介绍XAML对象元素和属性。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档