Introducing Windows Presentation Foundation and XAML
Total Page:16
File Type:pdf, Size:1020Kb
C H A P T E R 2 7 ■ ■ ■ Introducing Windows Presentation Foundation and XAML When version 1.0 of the .NET platform was released, programmers who needed to build graphical desktop applications made use of two APIs named Windows Forms and GDI+, packaged up primarily in the System.Windows.Forms.dll and System.Drawing.dll assemblies. While Windows Forms/GDI+ are excellent APIs for building traditional desktop GUIs, Microsoft shipped an alternative GUI desktop API named Windows Presentation Foundation (WPF) beginning with the release of .NET 3.0. This initial WPF chapter begins by examining the motivation behind this new GUI framework, which will help you see the differences between the Windows Forms/GDI+ and WPF programming models. Next, we will examine the different types of WPF applications supported by the API, and come to know the role of the several important classes including Application, Window, ContentControl, Control, UIElement, and FrameworkElement. During this time, you will learn to intercept keyboard and mouse activities, define application wide data, and other common WPF tasks using nothing but VB 2010 code. This chapter will then introduce you to an XML-based grammar named Extensible Application Markup Language (XAML). Here, you will learn the syntax and semantics of XAML, including attached property syntax, the role of type converters, markup extensions, and understanding how to generate, load and parse XAML at runtime. As well, you will learn how to integrate XAML data into a VB 2010 WFP- code base (and the benefits of doing so). This chapter wraps up by the integrated WPF designers of Visual Studio 2010. Here, you will build your own custom XAML editor/parser, which illustrates how XAML can be manipulated at runtime to build dynamic user interfaces. ■ Note The remaining WPF chapters of this book will introduce you to Microsoft Expression Blend, which is a WPF-centric tool dedicated to the generation of XAML on your behalf. The Motivation Behind WPF Over the years, Microsoft has created numerous graphical user interface toolkits (VB6, raw C/C++/Windows API development, MFC, etc.) to build desktop executables. Each of these APIs provided a code base to represent the basic aspects of a GUI application, including main windows, dialog boxes, 1165 CHAPTER 27 ■ INTRODUCING WINDOWS PRESENTATION FOUNDATION AND XAML controls, menu systems, and other basic necessities. With the initial release of the .NET platform, the Windows Forms API quickly became the preferred model for UI development, given its simple yet very powerful object model. While many full-featured desktop applications have been successfully created using Windows Forms, the fact of the matter is that this programming model is rather asymmetrical. Simply put, System.Windows.Forms.dll and System.Drawing.dll do not provide direct support for many additional technologies required to build a feature-rich desktop application. To illustrate this point, consider the ad hoc nature of GUI development before the release of WPF (e.g., .NET 2.0; see Table 27-1). Table 27-1. .NET 2.0 Solutions to Desired Functionalities Desired Functionality .NET 2.0 Solution Building windows with controls Windows Forms 2D graphics support GDI+ (System.Drawing.dll) 3D graphics support DirectX APIs Support for streaming video Windows Media Player APIs Support for flow-style documents Programmatic manipulation of PDF files As you can see, a Windows Forms developer must pull in types from a number of unrelated APIs and object models. While it is true that making use of these diverse APIs may look similar syntactically (it is just VB 2010 code, after all), you may also agree that each technology requires a radically different mind- set. For example, the skills required to create a 3D rendered animation using DirectX are completely different from those used to bind data to a grid. To be sure, it is very difficult for a Windows Forms programmer to master the diverse nature of each API. ■ Note Appendix A provides an introduction to Windows Forms and GDI+ application development. Unifying Diverse APIs WPF (introduced with .NET 3.0) was purposely created to merge these previously unrelated programming tasks into a single unified object model. Thus, if you need to author a 3D animation, you have no need to manually program against the DirectX API (although you could), as 3D functionality is baked directly into WPF. To see how well things have cleaned up, consider Table 27-2, which illustrates the desktop development model ushered in as of .NET 3.0. 1166 CHAPTER 27 ■ INTRODUCING WINDOWS PRESENTATION FOUNDATION AND XAML Table 27-2. .NET 3.0 Solutions to Desired Functionalities Desired Functionality .NET 3.0 and Higher Solution Building forms with controls WPF 2D graphics support WPF 3D graphics support WPF Support for streaming video WPF Support for flow-style documents WPF The obvious benefit here is that .NET programmers now have a single, symmetrical API for all common GUI programming needs. Once you become comfortable with the functionality of the key WPF assemblies and the grammar of XAML, you'll be amazed how quickly you can create very sophisticated UIs. Providing a Separation of Concerns via XAML Perhaps one of the most compelling benefits is that WPF provides a way to cleanly separate the look and feel of a Windows application from the programming logic that drives it. Using XAML, it is possible to define the UI of an application via XML markup. This markup (ideally generated using tools such as Microsoft Expression Blend) can then be connected to a managed code base to provide the guts of the program’s functionality. ■ Note XAML is not limited to WPF applications! Any application can use XAML to describe a tree of .NET objects, even if they have nothing to do with a visible user interface. For example, the Windows Workflow Foundation API uses a XAML-based grammar to define business processes and custom activities. As you dig into WPF, you may be surprised how much flexibility “desktop markup” provides. XAML allows you to define not only simple UI elements (buttons, grids, list boxes, etc.) in markup, but also 2D and 3D graphical data, animations, data binding logic, and multimedia functionality (such as video playback). For example, defining a circular button control that animates a company logo requires just a few lines of markup. As shown in Chapter 31, WPF controls can be modified through styles and templates, which allow you to change the overall look and feel of an application with minimum fuss and bother. Unlike Windows Forms development, the only compelling reason to build a custom WPF control library is if you need to change the behaviors of a control (e.g., add custom methods, properties, or events; subclass an existing control to override Overridable members). If you simply need to change the look and feel of a control (again, such as a circular animated button), you can do so entirely through markup. 1167 CHAPTER 27 ■ INTRODUCING WINDOWS PRESENTATION FOUNDATION AND XAML Providing an Optimized Rendering Model GUI toolkits such as Windows Forms, VB6, or MFC preformed all graphical rendering requests (including the rendering of UI elements such as buttons and list boxes) using a low level, API (GDI), which has been part of the Windows OS for years. GDI provides adequate performance for typical business applications or simple graphical programs; however, if a UI application needed to tap into high performance graphics, DirectX was required. The WPF programming model is quite different in that GDI is not used when rendering graphical data. All rendering operations (e.g., 2D graphics, 3D graphics, animations, UI widget renderings buttons, list boxes) now make use of the DirectX API. The first obvious benefit is that your WPF applications will automatically take advantage of hardware and software optimizations. As well, WPF applications can tap into very rich graphical services (blur effects, anti-aliasing, transparency, etc.) without the complexity of programming directly against the DirectX API. ■ Note Although WPF does push all rendering requests to the DirectX layer, I don't want to suggest that a WPF application will perform as fast as building an application using unmanaged C++ and DirectX directly. If you are build a desktop application that requires the fastest possible execution speed (such as a 3D video game), unmanaged C++ and DirectX are still the best approach. Simplifying Complex UI Programming To recap the story thus far, Windows Presentation Foundation (WPF) is an API to build desktop applications that integrates various desktop APIs into a single object model and provides a clean separation of concerns via XAML. In addition to these major points, WPF applications also benefit from a very simple way to integrate services into your programs, which historically were quite complex to account for. Here is a quick rundown of the core WPF features: • A number of layout managers (far more than Windows Forms) to provide extremely flexible control over placement and reposition of content. • Use of an enhanced data-binding engine to bind content to UI elements in a variety of ways. • A built-in style engine, which allows you to define “themes” for a WPF application. • Use of vector graphics, which allows content to be automatically resized to fit the size and resolution of the screen hosting the application. • Support for 2D and 3D graphics, animations, and video and audio playback. • A rich typography API, such as support for XML Paper Specification (XPS) documents, fixed documents (WYSIWYG), flow documents, and document annotations (e.g., a Sticky Notes API). • Support for interoperating with legacy GUI models (e.g., Windows Forms, ActiveX, and Win32 HWNDs). For example, you can incorporate custom Windows Forms controls into a WPF application, and vice versa.