在正文開始之前需要介紹一個人:Sean Sexton. 來自明尼蘇達雙城的軟件工程師,對C#和WPF有着極深的熱情。最為出色的是他維護了兩個博客:2,000Things You Should Know About C# 和 2,000 Things You Should Know About WPF 。他以類似微博式的150字簡短語言來每天更新一條WPF和C#重要又容易被遺忘的知識。Follow他的博客也有一段日子了,很希望能夠分享給大家。
本系列我不僅會翻譯他的每一個tip,也會加入自己開發之中的看法和見解,希望能夠以更貼近的語言來展示大師的風采。本系列我希望自己也能和他一樣堅持下來,每天的進步才能促成偉大。
如果大家更想了解這個偉大的程序員,這個是訪談鏈接,他在訪談中有談到自己生平和對編程相關學習的看法,我覺得對大家會幫助很大,有時間后面會出一篇翻譯這個訪談的文章供大家閱讀,希望大家能夠關注。
在這里鄭重說明.該系列是基於Sean Sexton先生的英文博客, Sean Sexton擁有全部版權和撤銷權利。
前文:<1-7>WPF, 渲染, 矢量, 布局, 樣式, 模板, 命令
前文:<8-14>xaml, page, 3d, version, wpf, silverlight,asp.net
[小九的學堂,致力於以平凡的語言描述不平凡的技術。如要轉載,請注明來源:小九的學堂。cnblogs.com/xfuture]
#15 system.windows 命名空間
大多數wpf用戶界面相關的類的命名空間都是system.windows或者其子命名空間。
下面列出一些在system.window命名空間下的類:
1 Application – Encapsulated a WPF application 2 Clipboard – Transfer data to/from clipboard 3 ContentElement – Base class for contents elements, for flow-style presentation 4 DataObject – Transfering data 5 DataTemplate – Visual structure of a data object 6 DependencyObject – Object that participates in dependency property system 7 DependencyProperty – Properties that support data binding, inheritance, etc. 8 EventManager – Event-related utility methods 9 FrameworkElement – Extends UIElement & provides support for logical tree, et al 10 MessageBox - Displays a message box 11 ResourceDictionary – Dictionary that contains WPF resources 12 RoutedEvent – Routed event 13 Style – Sharing of properties/resources between instances 14 UIElement – Base class for graphical elements 15 UIElement3D – Base class for 3D elements 16 Window – Window in user interface
#16 獨立和瀏覽器承載的應用程序
wpf可以創建獨立的windows應用程序在windows環境下運行或者創建瀏覽器承載的應用程序,在瀏覽器中運行。瀏覽器承載的應用程序也被稱之為xbaps(xaml瀏覽器應用程序)。
發布的XBAP托管在一個web的服務器上,訪問者機器首先下載和安裝應用程序所需要的silverlight插件,當用戶第一次瀏覽xbap頁面時,用戶界面會顯示在瀏覽器的窗口中。
XBAPS可以在ie和firefox中完美運行。
#17 WPF單位
在windows forms開發中,控件的大小是根據 像素生成的。而在wpf中使用的是wpf單位。
1 wpf unit =1/96 inch。 這個說明了在96dpi下,一個wpf單位為一個像素(pixel)。這就意味着在120dpi下,一個wpf單位是1.25pixel。
由於WPF所有的元素都是使用wpf單位來進行大小繪制的,所有的控件都是會基於設備系統的DPI。這樣就可以保證在不同的設備上有着相同的物理尺寸。換句話說就是一個96WPF單位的元素無論是在96dpi下還是120dpi下都是1英寸大小(96pixel=1英寸)。
在Windows Form下如果想做到設備無關性,需要設置AutoScaleMode,但在WPF中是自動的。
計算公式:# pixels = (# WPF Units) * (DPI / 96)
#18 WPF結構
下面結構圖展示了WPF主要的一些dll之間的依賴關系:
下面列出在上圖中的dll:
System.Windows.Presentation.dll – Contains some types related to add-ins
PresentationFramework.Aero.dll – Implements Aero theme for WPF controls
- PresentationFramework.Classic.dll – Implements Classic theme for WPF controls
- PresentationFramework.dll – Contains most of the main WPF controls, as well as support for data binding
- PresentationUI.dll – Contains some resources related to themes & some support classes for System.Windows.Documents
- ReachFramework.dll – Contains types related to printing and XPS documents
- System.Printing.dll – Contains main types related to printing
- PresentationCore.dll - Lower level types for visual rendering like UIElement. Also DragDrop support.
- System.Windows.Input.Manipulations.dll - Implementation of input manipulators
- UIAutomationProvider.dll - Support for UI Automation (for accessibility)
- WindowsBase.dll - Lower-level types like DependencyObject and DependencyProperty
- UIAutomationTypes.dll – Basic types related to UI Automation
#19 WPF可視化對象(Visual Class)
Visual類是渲染WPF窗口和頁面對象的基類。它可以提供hittest(與一個點、矩形、區域或其它對象之間執行像素級的點擊檢測,比如判斷鼠標是否在一個對象內等),剪切和坐標變換以及將自身渲染呈現出現。它提供了wpf視覺樹的模型。
以下是Visual,UIElement,FrameworkElement的構造示意: public abstract class Visual : DependencyObject public class UIElement : Visual, IAnimatable, IInputElement public class FrameworkElement : UIElement, IFrameworkInputElement, IInputElement, ISupportInitialize
從Visual的構造看,它是繼承於DependencyObject的抽像類。而UIElement繼承自Visual,FrameworkElement又繼承自UIElement。Visual是所有FrameworkElement的抽象基類。它為在WPF中寫新的控件提供了入口點,在許多方面,你可以把它想像成為相當於Win32應用程序模式下的window句柄(HWND)。Visual對象是WPF的核心對象,其主要角色是提供呈現支持。用戶控件,如按鈕Button和文本框TextBox,都從Visual類繼承,並使用Visual定義的屬性來維持它們的呈現數據。
#20 WPF UIElement Class
UIElement繼承Visual並增加了對基本用戶交互行為的支持。
包括:布局行為,父子關系,繪制和排列,響應用戶的輸入,從鍵盤和鼠標輸入事件,命令綁定,Focus的管理,發出和接受路由事件,冒泡/隧道樹。
#21 FrameworkElement
FrameworkElement繼承自UIElement,是所有控件的父類。
FrameworkElement在UIElement的基礎上增加了:
1. 布局。實現了一些布局的屬性:HorizontalAlignment, MinWidth and Margin
2. 通過BeginStoryboard方法來產生動畫
3. 數據綁定
4. 模板綁定
5. 樣式
FrameworkElement類是我們在邏輯樹(也包括用戶界面的可視化對象的層次)上最主要的要素。
附帶:邏輯樹和視覺樹的區別。其實邏輯樹是以控件最低元素,而視覺則是最深層次的剖析。邏輯樹是與XAML布局相同的。大家可以通過下圖來進行判斷:
左圖為邏輯樹,右圖為視覺樹。
敬請期待后續基礎篇22-28