2000条你应知的WPF小姿势 基础篇<15-21>

  在正文开始之前需要介绍一个人: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

  

posted @ 2014-05-26 11:35  techlead_krischang  阅读(2878)  评论(27编辑  收藏  举报