This book shows you how to use the Microsoft Windows Presentation Foundation (WPF) to write programs that run under Microsoft Windows. These programs can be either regular stand-alone Windows applications (which are now often called client applications) or front ends for distributed applications. The WPF is considered to be the primary application programming interface (API) for Microsoft Windows Vista, but you can also run WPF applications under Microsoft Windows XP with Service Pack 2 or Windows Server 2003 after you have installed Microsoft .NET Framework 3.0. Although you use the WPF for writing what are sometimes called "regular type Windows apps," these are definitely not your parents' Windows programs. The WPF includes a new look, a new philosophy concerning control customization, new graphics facilities (including animation and 3D), and a new programming interface. The WPF actually has two interrelated programming interfaces. You can write WPF programs entirely using C# or any other programming language that complies with the .NET Common Language Specification (CLS). In addition, the WPF includes an exciting new XML-based markup language called the Extensible Application Markup Language (or XAML, pronounced "zammel"), and in some cases you can write entire programs in XAML. Generally, however, you will build your applications from both code and markup (as the title of this book implies). You'll use XAML for defining the user interface and visuals of your applicationincluding graphics and animationand you'll write code for handling user input events. Your BackgroundIn writing this book, I have assumed that you already have experience with the C# programming language and previous versions of the .NET Framework. If that is not the case, please refer to my short book titled .NET Book Zero: What the C or C++ Programmer Needs to Know about C# and the .NET Framework. This book is free and is available for reading or downloading from the following page of my Web site: http://www.charlespetzold.com/dotnet If you are a beginning programmer, I recommend that you learn C# first by writing console programs, which are character-mode programs that run in a Command Prompt window. My book Programming in the Key of C#: A Primer for Aspiring Programmers (Microsoft Press, 2003) takes this approach. This BookI have been writing programs for Windows since 1985, and the WPF is the most exciting development in Windows programming that I've experienced. But because it supports two very different programming interfaces, the WPF has also presented great challenges for me in writing this book. After giving the matter much thought, I decided that every WPF programmer should have a solid foundation in writing WPF applications entirely in code. For that reason, Part I of this book shows you how to write complete WPF programs using C#. Several features of the WPF required enhancements to .NET properties and events, and it's important to understand these enhancements, particularly when you're working with XAML. For this reason, I have devoted chapters in Part I specifically to the new concepts of dependency properties and routed input events. Part II of this book focuses on XAML. I show how to create small XAML-only applications and also how to combine XAML with C# code in creating larger, more sophisticated applications. One of the first jobs I take on in Part II is to create a programming tool called XAML Cruncher that has helped me a lot in learning XAML, and which I hope will help you as well. Because XAML is used primarily to create the visuals of an application, most of the graphics coverage in this book is found in Part II. In the long run, most of the XAML that gets written in this world will probably be generated by interactive designers and other programming tools. I'm sure that you will eventually use these designers and tools yourself to facilitate the development of your applications. However, I think it's vital for every WPF programmer to be able to write XAML "manually," and that's what I show you how to do in this book. If you're like me, you'll discover that programming in XAMLparticularly when you're using a tool such as XAML Crunchercan be great fun as an end in itself. My books are tutorials more than references. Readers of my previous books have found the books most useful when they begin reading Chapter 1 and continue sequentially. Getting your fingers accustomed to typing C# and XAML is essential. Like learning to juggle or playing the oboe, programming is not something you can learn just from reading a book. Generally people learn a programming interface more quickly and deeply when they actually type in the code themselves. Particularly in the early chapters, try doing this, and don't hesitate to experiment with variations in the code. In the early chapters I have suggested many such experiments. Windows and ProgrammingMicrosoft released the first version of Windows in the fall of 1985. Since then, Windows has been progressively updated and enhanced, most dramatically in Microsoft Windows NT (1993) and Windows 95 (1995), when Windows moved from a 16-bit architecture to a 32-bit architecture. When Windows was first released, there was really only one way to write a Windows application, and that was by using the C programming language to access the Windows API. Over the years, many other languages have been adapted for doing Windows programming, including Microsoft Visual Basic and C++. For C-based languages, Microsoft currently offers four approaches to writing Windows applications, as detailed in the following table.
It's not my job to tell you what language or interface you should use to write Windows applications. That's a decision only you can make based on your particular programming job and the resources available to you. If you want to learn more about the Windows API, many people have found my book Programming Windows (fifth edition, Microsoft Press, 1998) to be valuable. Although I never cared for MFC, historically it has been one of the most popular approaches to Windows programming. A good place to learn about MFC is the book Programming Windows with MFC (second edition, Microsoft Press, 1999) by Jeff Prosise and, for advanced programmers, Programming Applications for Microsoft Windows (Microsoft Press, 1999) by Jeffrey Richter. If you'd rather engage in more modern Windows programming (this is, after all, the twenty-first century), .NET is really the way to go. Programs written for .NET are generally compiled into an intermediate code (the Microsoft Intermediate Language, or MSIL), which is then compiled into native code when the application is run. This managed code helps protect the operating system from errant programs that might damage the user's system, and it also lets .NET programs be potentially platform independent. It might seem odd that Microsoft has released two distinct .NET platforms for writing Windows client applications. I believe that Windows Forms still has a strong role to play in Windows application development. Particularly with the enhancements in the .NET Framework 2.0, Windows Forms has a greater array of powerful controls and standard dialog boxes than the WPF, and in many ways it's the easier programming platform. I've written two books on Windows Forms. Programming Windows with C# (Microsoft Press, 2001) is large and comprehensive, and it covers the first version of Windows Forms in detail. Programming Microsoft Windows Forms (Microsoft Press, 2005) is short and streamlined. It supplements the earlier book with coverage of Windows Forms 2.0 features, and by itself it can help a beginning Windows Forms programmer get quickly up to speed. Whereas the WPF may not yet have the range of controls or standard dialog boxes supported in Windows Forms, it lays a strong foundation for an extensive future. In particular, if you need to do a lot of control customization or graphics, the WPF is the way to go. System RequirementsTo use this book, you'll need:
All versions of the .NET Framework and software development kits are available from the Microsoft Web site: http://msdn.microsoft.com/netframework/downloads/updates In theory, you don't need Visual Studio to compile and run WPF applications. The SDK includes a command-line program named MSBuild that can build WPF applications from C# project (.csproj) files. However, Visual Studio certainly makes WPF development easier, and some would argue that in this day and age, it's really essential. Prerelease SoftwareThis book was reviewed and tested against the June 2006 Community Technology Preview (CTP) of the .NET Framework 3.0. I expect this book to be fully compatible with the final release of the .NET Framework 3.0 and SDK, but if there are any changes or corrections, they will be collected in the Microsoft Knowledge Base article cited in the "Support for This Book" section of this introduction. Code SamplesAll the code samples discussed in this book can be downloaded from the book's companion content page at the following Web address: http://www.microsoft.com/mspress/companion/0-7356-1957-3 Support for This BookEvery effort has been made to ensure the accuracy of this book and the companion content. As corrections or changes are collected, they will be added to a Microsoft Knowledge Base article. Microsoft Press provides support for books and companion content at the following Web site: http://www.microsoft.com/learning/support/books Questions and CommentsIf you have comments, questions, or ideas regarding this book or the companion content, or if you have questions that are not answered by visiting the sites previously mentioned, please send them to Microsoft Press via e-mail at: mspinput@microsoft.com or via postal mail at: Microsoft Press Please note that Microsoft software product support is not offered through these addresses. Author's Web SiteInformation specific to this book can also be found on this page of my Web site: http://www.charlespetzold.com/wpf Information about my other books, as well as a blog and miscellaneous articles, can be accessed from the home page of my Web site. Special ThanksFor helping put this book together, I want to thank my agent, Claudette Moore of the Moore Literary Agency, and my editors at Microsoft Press: Valerie Woolley (project editor), Kenn Scribner (technical editor), and the other folks listed on the copyright page of this book who have tried their best to polish my prose and correct my misconceptions. I've also benefited greatly from a community of bloggersmany but not all of whom work at Microsoftwho have written about WPF programming and indirectly contributed to this book, and people who have emailed me with pointers. The most prominent are (in alphabetical order) Chris Anderson, Pete Blois, Aaron Cornelius at www.wiredprairie.us, Pablo Fernicola, Jessica Fosler, Henry Hahn, Karsten Januszewski, Chuck Jazdzewski, Nick Kramer, Rahul Patil, Rob Relyea, Greg Schechter, Tim Sneath, David Teitlebaum, and Shawn Van Ness. Of course, even with this lineup of impeccable guidance, I am perfectly capable of blundering, and any errors or bugs that might still remain in this book are entirely my responsibility. Most of all, I want to thank Deirdre, who offers words of wisdom, sentences of sympathy, paragraphs of patience, chapters of charity, and a big long book of love. Charles Petzold |