
1 WPF Overview This chapter explains fundamental Windows Presentation Foundation (WPF) concepts. Normally, it’s the glaringly obvious chapter that you skip to get to the good stuff. If this were a cookbook, this would be where I explain food and tell you why it’s important (“so you don’t starve”). In this case, however, I encourage you to at least skim this chapter before plunging ahead. Many parts of WPF are confusing and seemingly inconsistent. This chapter gives some useful background on what WPF is (that question has caused more confusion than you might imag- ine), WPF’s goals, and the underlying architecture used by WPF. These tidbits of information will give you some useful perspective for understanding WPF’s quirks and idiosyncrasies. For example, this information will let you say, “Oh, WPF does it that way because Direct3D does it that way” or “I’ll bet this weird behavior was provided to save me a few keystrokes of typing.” In addition to this background, this chapter describes the basic types of WPF projects. Finally, this chapter can help you understand what’s contained in the later chapters. This chap- ter briefly defines resources, styles, control templates, and other terms that are described more completely in later chapters. A quick introduction to those terms now will help you know which chapters to read later. WPF IN A NUTSHELLCOPYRIGHTED MATERIAL WPF has been around for quite a while now, but there are still plenty of people out there who don’t really know what it is. I’ve heard people claim it’s everything from a set of controls to a “Vista thing” to XAML. 2 ❘ CHApTER 1 WPF OVERVIEw In fact, there’s a kernel of truth in each of these attitudes. WPF does include a new set of controls that largely replace the Windows Forms controls. The libraries you need to run WPF applications are installed by default in Vista and Windows 7, so it is sort of a Vista thing, although you can also run WPF applications in Windows XP and certainly in future versions of Windows (and perhaps even UNIX some day). WPF applications can use XAML to build interfaces, and XAML is all you really need to write loose web pages; but there’s a lot more to WPF than just XAML. As far as WPF’s importance and usefulness go, opinions range the gamut from “I don’t have time for jiggling buttons and spinning labels” to “It’s the wave of the future, and every new application will be written in WPF by the end of the year” (although that was last year, so perhaps this latter attitude isn’t quite correct). Again, the truth lies somewhere between these two extremes. You certainly can abuse WPF to build completely unusable interfaces full of bouncing buttons, skewed video, stretched labels, garish col- ors, and rotating three-dimensional (3D) graphics. You can add animation to the controls until the interface behaves more like a video game than a business application. Figure 1-1 shows the Clutter example program displaying a (faked) series of rotated images as an invoice spins into view. This program demonstrates some interesting techniques but goes way over- board with gratuitous animation, displaying a spinning invoice area, animated buttons, and sound effects. If you think it’s ugly in this book, you should see how annoying it is when you run it! FOcUS ON WHAT, NOT HOw In this overview chapter, don’t worry about how the examples work. For now, focus on the cool and amazing things they can do. You’ll see how they work in later chapters. FIGURE 1-1 WPF in a Nutshell ❘ 3 AMPLE EXAMPLES All of the example programs that are available for download on the book’s web site have titles that match their names. For example, Figure 1-1 shows the Clutter pro- gram and its title is “Clutter.” If you see a picture in the book and you want to find the corresponding example program, download the programs for that chapter and look for a program with a name matching the title shown in the picture. For information about downloading the examples, see the section “Source Code” in the Introduction. If you use restraint and good design principles, you can use WPF to make user interfaces that are more visually appealing and inviting. You can use animation to hide and display information to reduce clutter while giving the user hints about where the data has gone so it’s easy to find later. ANIMATION OVERLOAD Before you go overboard with animation, ask yourself, “Does this animation serve a purpose?” If the purpose is to hide a picture while showing where it is going so the user can find it again later — great. If the purpose is to be cool and make an invoice fly out of a file cabinet icon while growing, spinning, and changing opacity — think again. The first time, the user might think this is cool; but by the second or third time, the user will find it annoying; and by the end of the day, the user will be seasick. It may not be true that all new applications will use WPF by the end of the year, but you should con- sider using WPF for new development. While getting the most out of WPF takes a lot of study and practice, it’s easy enough to use WPF controls instead of the corresponding Windows Forms controls in most cases. You may not stretch WPF to its limits, but you can take advantage of some of WPF’s new features without a lot of work. Of course, some applications will probably never need WPF. Some programs run most naturally as automatic services or from the command line and don’t need graphical user interfaces at all. What Is WPF? So, what exactly is WPF? I’ve heard it described as a library, framework, subsystem, set of controls, language, and programming model. Probably the easiest way to understand WPF is to think of it as an assortment of objects that make it easier to build cool user interfaces. Those objects include a new set of controls, some replacing your favorite Windows Forms controls (such as Label, TextBox, Button, Slider) and others providing new features (such as Expander, FlowDocument, and ViewBox). 4 ❘ CHAPTER 1 WPF OVERVIEW WPF also includes an abundance of new objects to manage animation, resources, events, styles, tem- plates, and other new WPF features. Your application uses some combination of these objects to build a user interface. What Is XAML? XAML (pronounced zammel) stands for “eXtensible Application Markup Language.” It is an exten- sion of XML (eXtensible Markup Language). Microsoft invented XAML to represent WPF user interfaces in a static language much as HTML represents the contents of a web page. It defines spe- cial tokens to represent windows, controls, resources, styles, and other WPF objects. A program can use a file containing XAML code to load a user interface. For example, a web browser can load a file containing XAML code and display the user interface (UI) it defines. If you use Expression Blend or Visual Studio to create a WPF application, the application automatically loads the project’s XAML for you so you don’t need to add code to do that yourself. NO XAML REQUIRED Note that XAML is not required to make a WPF application. A program could build all of the objects that it needs by using code. For example, instead of placing a Label object in a XAML file, the program could create an instance of theLabel class and use that instead. XAML is just there for your convenience. It makes it easier to build and store interfaces. All of the usual XML rules apply to XAML files. In particular, XAML files must have a single root element that contains all of the other elements in the file. What element you use as the root element depends on the type of project you are building. For example, in a compiled application, the root element is a Window that represents the window displayed on the desktop. In contrast, a loose XAML page is displayed in a web browser, so the browser plays the role of the window. In that case, the root element is typically some container control such as a Grid or StackPanel that can hold all of the other elements. CONTROL SNEAK PEEK Later chapters describe these controls in detail, but for now, know that a Grid arranges controls in rows and columns, and a StackPanel arranges controls in a single row either vertically or horizontally. WPF in a Nutshell ❘ 5 Each opening element must have a corresponding closing element with the same name but beginning with a slash. For example, the following code snippet defines a StackPanel: <StackPanel> </StackPanel> If an element doesn’t need to contain any other elements, you can use a special shorthand and end the opening element with a slash instead of a separate closing element. The following snippet shows an Image object. It doesn’t contain any other items, so it uses the shorthand notation. <Image Margin=”10” Width=”75” Height=”75” Source=”Volleyball.jpg”/> The preceding snippet also demonstrates attributes. A XAML attribute is a value contained inside an item’s opening tag. In this snippet, the Image object has attributes Margin, Width, Height, and Source with values 10, 75, 75, and Volleyball.jpg. XAML elements must be properly nested to show which WPF objects contain other objects. The fol- lowing XAML code shows a Window that contains a horizontal StackPanel that holds several other vertical StackPanel objects, each holding an Image and a Label.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages20 Page
-
File Size-