可扩展应用程序标记语言(XAML)是一种声明性语言。概括来说,就是为应用程序构建UI。目前WPF,UWP应用程序都是使用XAML语言来构建UI.
1、XAML命名空间(xmlns)
如 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
这里的值是一个url,但其实这里并不指向这个链接。它只是用来标识在不同文档之间的唯一性。
这个命名空间实际上映射的是wpf的命名空间
System.Windows,System.Windows.Controls,System.Windows.Media.Media3D等等
它实际的定义如下
1 [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]
这段代码定义在AssemblyInfo.cs中
2、创建对象
1 public classStudent2 {3 public int ID { get; set; }4
5 public string StudentName { get; set; }6
7 public School School { get; set; }8 }9
10 public classSchool11 {12 public string SchoolName { get; set; }13 }
有上述类
在C#中创建
1 var student = new Student();
在xaml中创建
1
在调用这个类之前,我们还需要引入命名空间。
说明:如果是不同的程序集,还需要指定程序集,如(xmlns:CEF="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf")
1 xmlns:myclass="clr-namespace:MyNamespace"
这样,完整的调用如下
1
3、为属性赋值
1
属性是对象的情况
1
2
3
4
5
4、为对象命名(x:Name或Name)
1
1
使用Name命名只能用于.Net 3.5及以后的版本
x:Name是全版本通用
命名后可以在后台代码中直接操作
1 //Student
2 stu.ID = 2;3
4 //Label
5 mylable.Foreground = Brushes.Green;