UWP基础教程 - XAML开篇
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项目空白页面的代码
<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中的特殊属性,专门用来声明命名空间。一旦声明了命名空间,在该文档任何地方都可以使用。
xmlns:local="using:UWPXaml"
表示在该XAML里可以通过local标识符来使用UWPXaml控件下的控件或者其他类
大家可能会问那么Grid的命名空间是什么呢?为什么没有标识符前缀?这里我们来看下两个特别的命名空间
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对象元素和属性。
- 黄荣奎:如何快速、便捷开发小程序
- 熊普江: BGP网络架构助力开发者快速构建、优化业务
- 唐良:云端架构给电商行业带来创新力
- 邹方明:看腾讯云如何架构海量存储系统
- 王之捷:AI智能云端架构大幅提升智能语音识别能力
- 精彩大放送 | 腾讯云:容器架构向无服务架构的演进
- Python 数据分析学习笔记
- 如何用Fiddler抓取https
- 人工智能与大数据结合,帮助降低自杀率
- “多态”的数据库连接池实现
- ASP.NET MVC基于标注特性的Model验证:DataAnnotationsModelValidatorProvider
- 用js代码理解区块链,最简版本
- 谈谈IE针对Ajax请求结果的缓存
- Ajax请求过程中显示“进度”的简单实现
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
- Python 基于jwt实现认证机制流程解析
- Keras – GPU ID 和显存占用设定步骤
- PHP中如何使用Redis接管文件存储Session详解
- 看我一波,Android获取进程名函数,代码优化到极致的操作!
- laravel5使用freetds连接sql server的方法
- opencv 图像滤波(均值,方框,高斯,中值)
- opencv 阈值分割的具体使用
- 浅谈keras 的抽象后端(from keras import backend as K)
- 在Keras中利用np.random.shuffle()打乱数据集实例
- 浅谈matplotlib中FigureCanvasXAgg的用法
- Keras自定义实现带masking的meanpooling层方式
- 利用keras使用神经网络预测销量操作
- 获取python运行输出的数据并解析存为dataFrame实例
- 如何使用Cython对python代码进行加密