理解XAML XAML(extensible application markup language)發音為“zammel”,是用於實例化.net對象的標記語言。 XAML扮演的角色 對於WPF應用XAML不是必須的,編程人員可以在後端直接編寫代碼構建界面 基於XAML可以單獨實現在前臺編寫UI的功 ...
理解XAML
XAML(extensible application markup language)發音為“zammel”,是用於實例化.net對象的標記語言。
XAML扮演的角色
- 對於WPF應用XAML不是必須的,編程人員可以在後端直接編寫代碼構建界面
- 基於XAML可以單獨實現在前臺編寫UI的功能,XAML的角色定位類似於html在Asp.net中一樣可以定義各種標簽,設置標簽屬性來改變樣式
- 在Visual Studio中可以通過可視化界面進行XAML的界面調試
WPF中XAML是如何運作的
- WPF應用在被編譯時所有XAML被轉換成BAML(binary application markup language)。例如在編譯window.xaml是會在obj/Debug文件夾下產生window.baml的臨時文件
- 編譯器會為window創建部分類window.g.i.cs,也在obj/Debug文件夾下
- 最終這些部分類被打包進單個程式集(*.exe),Baml最後以獨立資源嵌入到程式集
- 在視窗初始化調用被編譯的部分類的InitializeComponent()方法載入XAML
使用代碼載入未編譯的XAML
using System; using System.Collections.Generic; using System.IO; 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.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfXaml { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LoadXaml(); } private void LoadXaml() { using var fileReader = new FileStream("../../../Test.xaml", FileMode.Open); DependencyObject rootElement = (DependencyObject)XamlReader.Load(fileReader); this.Content = rootElement; var btn = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "A"); if (btn != null) btn.Click += Btn_Click; } private void Btn_Click(object sender, RoutedEventArgs e) { MessageBox.Show("showMe"); } } }MainWindow
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfXaml"> <Button Content="123" Width="100" Height="30" Name="A"></Button> <Button Content="456" Width="100" Height="30" Name="B"></Button> </StackPanel>Test.xmal
註意:這種直接載入xaml的方式並沒有使用打包成資源的baml速度快
XAML基礎
<Window x:Class="WpfXaml.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:WpfXaml" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> </Grid> </Window>
XAML名稱空間
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"是WPF核心名稱空間,包含WPF所以類,包括構建用戶界面的控制項
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"是XAML名稱空間,包含XAML的實用特效
代碼隱藏類與命名元素
x:Class="WpfXaml.MainWindow"
Class特性告訴XAML解釋器用指定名稱生成一個新類,該類是主視窗的部分類(partial,即MainWindow.g.i.cs)。該類包含了 InitializeComponent()方法。
<Grid x:Name="Grid"> </Grid>
Name特性告訴XAML解釋器為部分類添加名稱為Grid的欄位
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")] internal System.Windows.Controls.Grid Grid;