WPF學習筆記1——xaml基礎
什麼是xaml?
xaml是擴展應用標記語言的簡寫。它是用於實例化.Net對象的標記語言,主要用於構建wpf應用的界面。
在xaml中的所有元素都映射爲.Net類的實例,元素的名字也完全對應於類的名字。
使用vs2019創建默認wpf應用程序,會自動生成App.xaml和MainWindow.xaml兩個文件,以及對應的.cs文件,App.xaml.cs和MainWindow.xaml.cs
MainWindow.xaml
程序的主窗體,MainWindow.xaml內容如下:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
Window 元素是一個頂級元素,在xaml文檔裏面只能有一個window元素。Page和Application元素也可以作爲頂級元素。
Title,height,width爲Window元素的屬性。
Grid是一個子元素。
xaml淺析
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
在XAML中對象之間的層級關係,要麼是並列要麼是包含,全部體現在標籤的關係上。那在後臺類裏,我們可以通過using引用其它名稱空間,而在XAML代碼裏,我們通過xmlns特徵來定義名稱空間,格式如下:
xmlns:[可選的映射前綴]=“名稱空間”。
x:它包含的類都是與解析XAML語言相關。
x:Class="MyFirstWpfApplication.MainWindow"表示將當前這個Window這個標籤解析成C#類的類名。這也和我們開始驗證的其爲後臺分部類一樣。它是x:下的Attribute,只能用於根節點,且根節點的類型要與x:Class所指示的類型(且爲分部類)一致。
補充 x名稱空間下的其它Attribute:
x:Name:告訴編譯器爲這個標籤生成對應的實例外還要爲這個實例聲明一個引用變量,變量名就是x:Name的值。還有將XAML標籤所對應的對象的Name屬性也設爲x:Name的值,並註冊到UI樹上,方便查找。
x:FieldModeifer 設置元素可訪問級別
x:key 採用鍵值對等
mc:Ignorable="d"
1、首先,命名空間d是我們在寫xaml文件時看到控件的大小,比如d:DesignWidth="480"表示在編輯器裏面我們看到的大小480,但是運行調試大小並不一定是480,它可能隨着屏幕的大小而發生改變
2、而命名空間mc是跟兼容性有關的,mc:Ignorable="d"的意思就是告訴編輯器(vs2017)在項目運行時忽略命名空間d設置的大小
頂級元素都對應一個類,還有相應的命名空間,而在類的內部則是部署在頂級元素的其他元素。類的聲明在相應的.cs文件中,比如MainWindow.xaml對應的MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();//加載各種組件
}
}
}
App.xaml
程序的主體,在Windows系統環境下,一個程序就是一個進程(Process)。一個GUI進程需要一個窗體作爲主窗體。App.xaml文件的作用就是聲明瞭程序的進程和主窗體會是誰。
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
XAML語法
xaml文檔是一個樹形結構
xaml中爲對象屬性賦值的語法:首先xaml代碼不能編寫程序的運行邏輯,當我們創建標籤對象的時候對其屬性進行必要的初始化纔有使用意義。爲對象屬性賦值有兩種方法
1.使用字符串進行簡單賦值。即簡單的Attribute=Value語法賦值,由於xaml語法限制,Value只能是字符串的值。
2.使用屬性元素進行復雜賦值。屬性元素指的是某個標籤的一個元素對應這個標籤的一個屬性,即以元素的形式來表達一個實例的屬性。
XAML對名稱空間引用的語法
xmlns:[映射名]=“clr-namespace:類庫中名稱空間的名字;assembly=類庫文件名”