Chapter 1 Introducing WPF Objectives - Intertech Motivation Behind WPF ... • Silverlight is a web-centric subset of .NET / WPF / XAML functionality. ... • Like any .NET

  • View

  • Download

Embed Size (px)

Text of Chapter 1 Introducing WPF Objectives - Intertech Motivation Behind WPF ... •...

  • Copyright Intertech, Inc 2014 Rev: 2 1-1

    Chapter 1

    Introducing WPF


    Understand the motivation behind WPF

    Examine the various flavors of WPF applications

    Overview the services provided by WPF

    Examine the core WPF assemblies and namespaces

    Work with the Window and Application class types

    Learn the syntax of XAML

    Understand the XAML / code relationship

  • Introducing WPF

    1-2 Copyright Intertech, Inc 2014 Rev: 2

    Chapter Overview

    Every few years or so, Microsoft introduces a new GUI toolkit. Since the release of .NET 3.0, the

    latest desktop GUI toolkit is Windows Presentation Foundation (WPF).

    In this introductory chapter, you will understand the motivation behind WPF, learn the syntax

    of XAML, examine the core programming model, and survey several WPF services. Also, you

    will overview the WPF support provided by Visual Studio and be introduced to the role of

    Expression Blend.

    As you would expect, this chapter provides a foundation for the remainder of the class.

  • Introducing WPF

    Copyright Intertech, Inc 2014 Rev: 2 1-3

    The Motivation Behind WPF

    Historically speaking, building a desktop UI under the Windows OS involved using a handful of key services:

    User32: Provided the code required to create windows, controls, and application infrastructure.

    GDI: Provided a framework for rendering 2D graphical data.

    DirectX: If an application required high-performance graphical rendering (multimedia applications, video games, rich interactive front ends, and so on), DirectX was the way to do so.

    Although Windows programmers could make use of these services using raw C/C++, many UI toolkits were introduced over the years:

    VB6, MFC, Windows Forms, GDI+, and so on.

    Each one of them was simply a wrapper around User32 / GDI.

    These toolkits still viewed DirectX as an external service.

    Windows Presentation Foundation (WPF) is a managed GUI toolkit that shipped beginning with .NET 3.0.

    The core WPF object model is similar, but not identical to, Windows Forms.

    The release of WPF does not imply that Windows Forms is obsolete.

    In fact, the .NET platform will support this API for years to come.

  • Introducing WPF

    1-4 Copyright Intertech, Inc 2014 Rev: 2

    Before the release of WPF, UI developers were forced to master a number of related, but ultimately independent, APIs.

    This required developers to switch gears when moving from one task (e.g., building a main window) to another (e.g., 3D graphical rendering).

    The end result was a very asymmetrical way to program.

    Consider the GUI development world before .NET 3.0:

    Desired Functionality Pre-.NET 3.0 Solution

    Forms, dialog boxes, controls Windows Forms, VB6, MFC, and so on

    2D graphics System.Drawing.dll (e.g., GDI+) or raw C-based GDI

    3D graphics DirectX

    Streaming video Windows Media Player API or third party APIs

    Fixed / Flow documents Third party APIs

    With the release of WPF, things have improved considerably.

    You now have a single symmetrical manner in which to interact with the necessary GUI infrastructure.

    Consider the GUI development world as of .NET 3.0 and higher:

    Desired Functionality .NET 3.0 and Higher Solution

    Forms, dialog boxes, controls Windows Presentation Foundation

    2D graphics Windows Presentation Foundation

    3D graphics Windows Presentation Foundation

    Streaming video Windows Presentation Foundation

    Fixed / Flow documents Windows Presentation Foundation

  • Introducing WPF

    Copyright Intertech, Inc 2014 Rev: 2 1-5

    Beyond offering a unified programming model, WPF also provides a clear separation of concerns.

    It is possible to separate the look and feel of a GUI application from the programming logic that drives it.

    This is achieved using an XML-based grammar termed XAML (zam-el).

    While most WPF applications will make use of XAML, doing so is entirely optional.

    This separation of concerns (via XAML) makes it much simpler for graphical designers to build very rich, professional UIs.

    Graphically minded individuals can use dedicated design tools such as Microsoft Expression Blend to generate the XAML.

    These XAML files can then be passed to the programming team to add logic to drive the UI (event handlers, method overrides, and so on).

    As you will see in the class, a Microsoft Blend project uses exactly the same format as a Visual Studio project.

    Beyond the introduction of XAML, WPF also provides a good number of integrated services, including (but not limited to) the following:

    A number of layout managers that provide full control over placement and repositioning of content.

    A built-in style engine, which allows you to define themes for a WPF application.

    Native use of vector graphics, which allows an image to be automatically resized to fit the size and resolution of the screen hosting the application.

    A rich typography API such as support for XPS (XML Paper Specification) documents, fixed documents (WYSIWYG), flow documents, and document annotations (e.g., a Sticky Notes API).

    Integrated 2D and 3D rendering services / animation services, which leverage DirectX for hardware acceleration. In fact, all rendering, even the rendering of UI elements (buttons, and so on) is preformed via DirectX.

    Interoperability with previous UI frameworks. For example, a WPF program can use Windows Forms controls or ActiveX controls. As well, a Windows Forms app can use WPF controls.

    Support for audio and video media.

    Support for touch screen development (as of .NET 4.0).

  • Introducing WPF

    1-6 Copyright Intertech, Inc 2014 Rev: 2

    Overall, WPF can be considered a supercharged UI framework.

    WPF is extremely useful when you need to build highly interactive, stylized front ends.

    Real-time rotation of 3D bar charts, spinning a portion of a UI to a 45-degree angle, and dynamic shadowing on a rendered image are all possible.

    Consider the following WPF application (an example project from Expression Blend):

    Clicking on any of the stylized buttons on the bottom of the application will rotate and animate a 3D motorcycle prototype.

    Each button supports a custom animation, performed when the cursor travels over the surface.

    Notice the drop shadows, angled text blocks, etc.

    While all of this could be done without WPF, doing so would require a considerable amount of complex code. Here, a majority of the UI is driven by XAML.

  • Introducing WPF

    Copyright Intertech, Inc 2014 Rev: 2 1-7

    Because WPF applications are so graphically intensive, a production-level WPF application may require the aid of a professional graphic designer.

    You may agree that most .NET programmers are not the best of artists.

    Likewise, most artists are not the best of .NET programmers.

    Using XAML and tools such as Visual Studio and Expression Blend, each part of the team can use dedicated tools, thus increasing efficiency in development.

    Remember that the same Visual Studio WPF application can be opened in Expression Blend (and vice versa).

    This is not to suggest that you cannot use WPF to build traditional GUIs (gray push buttons, a grid of data, a menu system, and so on).

    You can certainly use WPF to build traditional business applications.

    However, this API is strongly geared for next-generation GUI applications.

    WPF is closer to the Hollywood vision of what computer applications should look like (e.g., 3D spinning e-mail messages, glowing animated blocks of text, and the like).

    Even if you are interested in building a typical LOB application, WPF can still provide a number of key benefits.

    The WPF content model and template model make it very easy to customize controls.

    A LOB application can add a bit of eye candy (animations and visual effects) with just a few lines of XAML.

  • Introducing WPF

    1-8 Copyright Intertech, Inc 2014 Rev: 2

    The Flavors of WPF Applications

    Like Windows Forms, WPF can be used to build traditional desktop applications.

    Some possibilities include media viewers, thick client front ends to interact with remote data sources, and word processors.

    At minimum, this flavor of WPF application will make use of the Window and Application types, in addition to various UI elements (status bars, menu systems, dialogs, and so on).

    You will learn about these types in this chapter.

    Like other desktop applications, WPF desktop apps can be deployed via a standard setup program or ClickOnce deployment.

    Of course, the target machine must have the .NET 3.0 or higher runtime installed to support WPF applications.

    WPF desktop apps can make use of a page-based system, which allows an application to adopt a web-like navigational structure.

    Page navigation apps maintain Next and Previous buttons, a history list, and the ability to share data between pages.

    This type of WPF app makes use of various Page objects hosted within a NavigationWindow.

    If you are interested in this flavor of WPF program, consult Appendix C.

  • Introducing WPF

    Copyright Intertech, Inc 2014 Rev: 2 1-9

    WPF can also be used to build a new variety of smart clients where the app is embedded into a hosting browser.