2000条你应知的WPF小姿势 基础篇<22-27 WPF生命周期, 基础类等>

  端午长假在家陪着女朋友, 幸福感满满,生活对于一只饱经忧患的程序猿来说也是非常重要的,也就暂时没有更新博客。休假结束,回归奋斗的日子了,开始继续更新WPF系列。

  

  在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师。最为出色的是他维护了两个博客: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<15-21>

 

  [小九的学堂,致力于以平凡的语言描述不平凡的技术。如要转载,请注明来源:小九的学堂cnblogs.com/xfuture]


  #22 Control Class

  Control Class 继承自FrameworkElement,是与用户交互的所有控件的基类。例如有:TextBox, Label, ListBox, ComboBox and Button。但有一些容器比如Panel和Grid并不是控件。

  Control Class 扩展了FrameworkElement,拥有如下延伸的功能:

  1. 通过使用Template来更改控件的外观。

  2. 支持更改字体,边框, 背景色等视觉元素。

 

  

  #23 WPF项目的五种形态

  使用WPF你可以开发如下五种形态的项目:

  1. 独立的应用程序 - 在Windows上运行的标准的窗口程序。

  2. XAML 浏览器应用程序(XBAP) - 在浏览器上运行的基于Page的应用程序

  3. 自定义控件库 - 继承自Control并对其改写,私人定制,封装成dll库。

  4. 用户控件库 - 从UserControl派生创建的自定义控件,封装成dll库。

  5. 基础类库 - 封装一般类为dll库。

 

  #24 Application Class

  Application class 呈现了一个在windows上运行的一个独立的wpf客户端应用程序。每个运行中的WPF应用程序都具有一个application类的单例。

  使用VS创建的WPF应用程序会在App.Xaml自动生成Application对象定义的应用程序:

  

<Application x:Class="WpfApplication9.App"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     StartupUri="MainWindow.xaml">
   <Application.Resources>

   </Application.Resources>
</Application>

  Application类负责:

  1. 管理应用程序的生命周期(如Start和Close)

  2. 管理窗口,资源以及各种项目的基础属性。

  3. 命令行处理

  4. 页面导航。

 

  #25 Window Class

  Window Class 在WPF中呈现了一个窗口。他派生自ContentControl, 也就是说它只能有单一的子元素。它直接继承自Control,间接继承了FrameworkElement, UIElement, Visual, DependencyObject, and DispatcherObject.

  一个窗口在视觉的角度上拥有一个客户端区域(窗口内)和非客户端区域(标题栏,框架,最小/最大/关闭 按钮).它是跟用户交互的WPF独立程序的外壳。可以通过定制来实现不同的外观效果。

  你可以通过启动,关闭,隐藏,显示等事件来管理一个window的生命周期。

  Xaml Code:

  

<Window x:Class="WpfApplication9.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="MainWindow" Height="200" Width="250">
    <Grid>
        <Label Content="I'm a WPF Window!" Height="28" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Window>

  效果:

  

  #26 WPF生命周期

  下图展现了WPF的生命周期以及其事件:

  

 

  Application相关触发的主要事件有:

  1. Startup - 应用程序启动

  2. Exit - 应用程序关闭。

  3. Activated - 应用程序获得焦点时触发,也就是成为前台程序。

  4. Deactivated - 应用程序失去焦点时,也就是不再是前台程序时。

  5. DispatcherUnhandledException - 当一个异常被抛出时,你可以选择处理或者是抛出该异常。

  6. SessionEnding - Windows正在关闭时,无论是注销还是关机,都会触发该事件。你可以选择取消关机。

  

  你可以通过重写On[EventName]方法来添加对事件的处理, 如  OnStartUp。

 

  

  #27 Window Events at Startup and Shutdown 窗口在创建和关闭时相继触发的事件

  Window StartUp时会触发的事件有:

  

  • Initialized - Main window is being created
  • IsVisibleChanged - IsVisible property set to true
  • SizeChanged - Size property set to size of window
  • LayoutUpdated - Window layout changes
  • SourceInitialized - Window is attached to Win32 window handle
  • Activated - Window becomes foreground window
  • PreviewGotKeyboardFocus - Window getting focus
  • IsKeyboardFocusWithinChanged - IsKeyboardFocusWithin property set to true
  • IsKeyboardFocusedChanged - IsKeyboardFocused property set to true
  • GotKeyboardFocus - Window now has keyboard focus
  • LayoutUpdated - Window layout changes
  • Loaded - Window is now laid out, fully rendered
  • ContentRendered - All window content has been rendered

  Window ShutDown时触发的事件有:

  

  • Closing - Window is going to close
  • IsVisibleChanged - IsVisible property set to false
  • Deactivated - Window becomes background window
  • IsKeyboardFocusWithinChanged - IsKeyboardFocusWithin property set to false
  • IsKeyboardFocusedChanged - IsKeyboardFocused property set to false
  • LostKeyboardFocus - Window no longer has keyboard focus
  • Closed - Window is closing

  

 

  后篇会有对这些事件以及更多WPF基础和呈现机制的tip,敬请关注!

  如果觉得有帮助,右下角赞一下吧~ (* *)

 

 

posted @ 2014-06-04 13:39  techlead_krischang  阅读(3637)  评论(15编辑  收藏  举报