Thank you for picking up WPF 4.5 Unleashed! Windows Presentation Foundation (WPF) is Microsoft’s premier technology for creating Windows desktop apps, whether they consist of plain forms, document-centric windows, animated cartoons, videos, immersive 3D environments, or all of the above. WPF is a technology that makes it easier than ever to create a broad range of applications. It’s also the basis for XAML-based Windows Store apps.
Ever since WPF was publicly announced ten years ago (with the code name “Avalon”), it has gotten considerable attention for the ways in which it revolutionizes the process of creating software—especially for Windows programmers used to Windows Forms and GDI. It’s relatively easy to create fun, useful, and shareable WPF samples that demonstrate all kinds of techniques that are difficult to accomplish in other technologies. WPF 4.5, released in August 2012, continues to improve on previous versions of WPF in many different dimensions.
WPF is quite a departure from previous technologies in terms of its programming model, underlying concepts, and basic terminology. Even viewing the source code for WPF (by cracking open its components with a tool such as .NET Reflector) is a confusing experience because the code you’re looking for often doesn’t reside where you’d expect to find it. When you combine all this with the fact that there are often several ways to accomplish any task in WPF, you arrive at a conclusion shared by many: WPF has a very steep learning curve.
That’s where this book comes in. As WPF was developed, it was obvious that there would be no shortage of WPF books in the marketplace. But it wasn’t clear to me that the books would have the right balance to guide people through the technology and its unique concepts while showing practical ways to exploit it. Therefore, I wrote the first edition of this book, Windows Presentation Foundation Unleashed, with the following goals in mind:
To provide a solid grounding in the underlying concepts, in a practical and approachable fashion
To answer the questions most people have when learning the technology and to show how commonly desired tasks are accomplished
To be an authoritative source, thanks to input from members of the WPF team who designed, implemented, and tested the technology
To be clear about where the technology falls short rather than selling the technology as the answer to all problems
To be an easily navigated reference that you can constantly come back to
The first two editions of this book were far more successful than I ever imagined they would be. Now, more than six years after the first edition, I believe that this book accomplishes all the same goals but with even more depth and in the context of the modern Windows experience that includes Windows Store apps and a different design aesthetic. Whether you’re new to WPF or a long-time WPF developer, I hope you find this book to exhibit all these attributes.
This book is for software developers who are interested in creating user interfaces for the Windows desktop. Regardless of whether you’re creating line-of-business applications, consumer-facing applications, or reusable controls, this book contains a lot of content that helps you get the most out of the platform. It’s designed to be understandable even for folks who are new to the .NET Framework. And if you are already well versed in WPF, I’m confident that this book still has information for you. At the very least, it should be an invaluable reference for your bookshelf.
Because the technology and concepts behind WPF are the same ones behind Silverlight and XAML-based Windows Store apps, reading this book can also make you a better developer for Windows Phone and the Windows Store.
Although this book’s content is not optimized for graphic designers, reading this book can be a great way to understand more of the “guts” behind a product like Blend for Visual Studio.
To summarize, this book does the following:
Covers everything you need to know about Extensible Application Markup Language (XAML), the XML-based language for creating declarative user interfaces that can be easily restyled
Examines the WPF feature areas in incredible depth: controls, layout, resources, data binding, styling, graphics, animation, and more
Delves into topics that aren’t covered by most books: 3D, speech, audio/video, documents, effects, and more
Shows how to create popular user interface elements and leverage built-in controls such as the new Office-style Ribbon
Demonstrates how to create sophisticated user interface mechanisms, such as Visual Studio–like collapsible/dockable panes
Explains how to develop and deploy all types of applications, including navigation-based applications, applications hosted in a web browser, and applications with great-looking nonrectangular windows
Explains how to create first-class custom controls for WPF
Demonstrates how to create hybrid WPF software that leverages Windows Forms, DirectX, ActiveX, or other non-WPF technologies
Explains how to exploit desktop features in WPF applications, such as Jump Lists and taskbar customizations, and the same toast notifications used by Windows Store apps
This book doesn’t cover every last bit of WPF. (In particular, XML Paper Specification [XPS] documents, which never really took off, are given only a small bit of attention.) WPF’s surface area is so large that I don’t believe any single book can. But I think you’ll be pleased with the breadth and depth achieved by this book.
Examples in this book appear in XAML and C#, plus C++/CLI for interoperability discussions. XAML is used heavily for a number of reasons: It’s often the most concise way to express source code, it can often be pasted into lightweight tools to see instant results without any compilation, WPF-based tools generate XAML rather than procedural code, and XAML is applicable no matter what .NET language you use, such as Visual Basic instead of C#. Whenever the mapping between XAML and a language such as C# is not obvious, examples are shown in both representations.
This book targets version 4.5 of Windows Presentation Foundation, the corresponding Windows SDK, and Visual Studio 2012.
The following software is required:
A version of Windows that supports the .NET Framework 4.5.
The .NET Framework 4.5, which is installed by default starting with Windows 8. For earlier versions of Windows, you can download the .NET Framework 4.5 for free from http://msdn.com.
In addition, the following software is recommended:
The Windows Software Development Kit (SDK), specifically the .NET tools it includes. This is also a free download from http://msdn.com.
Visual Studio 2012 or later, which can be a free Express edition downloaded from http://msdn.com.
If you want additional tool support for WPF-based graphic design, Blend for Visual Studio can be extremely helpful.
A few examples are specific to features introduced in Windows Vista, Windows 7, and Windows 8. Some examples require a touchscreen (or an equivalent touch digitizer). The rest of the book applies equally to all relevant versions of Windows.
The source code for examples in this book can be downloaded from http://informit.com/title/9780672336973 or http://adamnathan.net/wpf.
This book is arranged into six main parts, representing the progression of feature areas that you typically need to understand to use WPF effectively. But if you’re dying to jump ahead and learn about a topic such as 3D or animation, the book is set up to allow for nonlinear journeys as well. The following sections provide a summary of each part.
This part includes the following chapters:
Chapter 1 introduces WPF by comparing it to alternative technologies and helping you make decisions about when WPF is appropriate for your needs. Chapter 2 explores XAML in great depth, giving you the foundation to understand the XAML you’ll encounter in the rest of the book and in real life. Chapter 3 highlights the most unique pieces of WPF’s programming model above and beyond what .NET programmers already understand.
This part includes the following chapters:
Chapter 4: Sizing, Positioning, and Transforming Elements
Chapter 6: Input Events: Keyboard, Mouse, Stylus, and Touch
Chapter 7: Structuring and Deploying an Application
Chapter 8: Exploiting Windows Desktop Features
Part II equips you with the knowledge to assemble and deploy a traditional-looking application (although some fancier effects, such as transforms and nonrectangular windows, are also covered). Chapters 4 and 5 discuss arranging controls (and other elements) in a user interface. Chapter 6 covers input events, including support for engaging touch user interfaces. Chapter 7 examines several different ways to package and deploy WPF-based user interfaces to make complete applications. Chapter 8 ends this part by showing slick ways to exploit features in the Windows desktop that can help make your application look modern.
This part includes the following chapters:
Chapter 11: Images, Text, and Other Controls
Part III provides a tour of controls built into WPF. There are many that you’d expect to have available, plus several that you might not expect. Two categories of controls—content controls (Chapter 9) and items controls (Chapter 10)—are important and deep enough topics to merit their own chapters. The rest of the controls are examined in Chapter 11.
This part includes the following chapters:
Chapter 14: Styles, Templates, Skins, and Themes
The features covered in Part IV are not always necessary to use in WPF applications, but they can greatly enhance the development process. Therefore, they are indispensable for professional developers who are serious about creating maintainable and robust applications or components. These topics are less about the results visible to end users than they are about the best practices for accomplishing these results.
This part includes the following chapters:
Chapter 18: Audio, Video, and Speech
This part of the book covers the features in WPF that typically get the most attention. The support for 2D and 3D graphics, animation, video, and more enable you to create a stunning experience. These features—and the way they are exposed—set WPF apart from previous systems. WPF lowers the barrier to incorporating such content in your software, so you might try some of these features that you never would have dared to try in the past!
This part includes the following chapters:
Chapter 19: Interoperability with Non-WPF Technologies
Chapter 20: User Controls and Custom Controls
Chapter 21: Layout with Custom Panels
Chapter 22: Toast Notifications
The topics covered in Part VI are relevant for advanced application developers, or developers of WPF-based controls. The fact that existing WPF controls can be radically restyled greatly reduces the need for creating custom controls. The final chapter is especially interesting because it enables your WPF apps to use a feature designed for Windows Store apps.
Various typefaces in this book identify new terms and other special items. These typefaces include the following:
Throughout this book, you’ll find a number of sidebar elements:
Tip
A tip is a bit of information that can help you in a real-world situation. Tips often offer shortcuts or alternative approaches to produce better results or to make a task easier or quicker.
Warning
A warning alerts you to an action or a condition that can lead to an unexpected or unpredictable result—and then tells you how to avoid it.