標籤:
WPF(Windows Presentation Foundation)
WPF是使用者介面架構,最大的特點是設計與編程分離,使各專業人才更加專註,不用分心。
我們可以通過XML的變形語言--XAML語言來操作。
XAML由一些規則(告訴解析器和編譯器如何處理XML)和一些關鍵字組成,但它自己沒有任何有意義的元素。
看下列代碼
<Window x:Class="HelloWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Title>Window1</Window.Title>
<Window.Height>300</Window.Height>
<Window.Width>300</Window.Width>
<Grid>
</Grid>
</Window>
此代碼將會輸出HelloWPF.Window1
其中 "http://schemas.microsoft.com/winfx/2006/xaml/presentation為命名空間,用於驗證自己和子項目。我們可以(在根項目或子項目上)聲明額外的XML命名空間,但每一個命名空間下的標識符都必須有一個唯一的首碼。
在C#中 我們可以用 system.console.writeline("HelloWPF.Window1")來實現同樣的效果,這時你一定會奇怪,既然C#看起來更簡單,我們為什麼要用XAML呢?
事實是,使用XAML語句,我們可以很快地在IE瀏覽器中查看XAML,還會看到一個活生生的按鈕放在瀏覽器視窗中,而C#代碼則必須要額外的代碼編譯方可使用。
產生和事件處理大順序:在運行時(run-time)模式下,為任何一個XAML聲明的對象設定屬性之前,總要添加一些事件處理常式,這樣就可以讓某個事件在屬性被設定時被觸發,而不用擔心XAML使用特性的順序。
屬性元素:
XAML語言:
<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Button.Content>
<Rectangle Height="40" Width="40" Fill="Black"/>
</Button.Content>
</Button>
C#中:
System.Windows.Controls.Button b = new System.Windows.Controls.Button();
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
r.Width = 40;
r.Height = 40;
r.Fill = System.Windows.Media.Brushes.Black;
b.Content = r;
我們可以使用屬性元素來替代設定屬性。
C#課後小試7