Title: What is WPF and why should you care?
This blog entry explains what WPF is, what some of its major features are, and why you might (or might not) care. Some of this material is adapted from my book WPF Programmer's Reference.
Here's a list of the sections:
What Is WPF?
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).
WPF also includes an abundance of new objects to manage animation, resources, events, styles, templates, and other new WPF features, some of which I'll describe a bit more later.
Your application uses some combination of these objects to build a user interface.
A WPF application can run in several environments. It can run on the desktop much as a normal Windows Forms application does. It can also run in a web browser as a XAML Browser Application (XBAP). That lets it run locally on the user's system, but within the browser so it can take advantage of the browser's navigation tools.
A WPF program can also run as a frame application inside a Frame control that acts sort of like a simple browser. You can restrict the Frame to only browse to the WPF pages you want, or you can allow it to browse more generally to web pages.
Finally you can make a Silverlight application. Silverlight is a restricted version of WPF that runs completely inside a browser. A Silverlight application acts as a normal web site, but with many of the extra features provided by WPF. For an example of a Silverlight web site, look at Microsoft's Silverlight home page.
What Is XAML?
XAML (pronounced "zammel") stands for "eXtensible Application Markup Language." It is an extension 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 special 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 or WPF desktop application can load a file containing XAML code and display the user interface (UI) it defines. Code in normal C# or Visual Basic files called "code-behind" sits behind the user interface to handle its events and do all of the application's serious work.
The point of using XAML files is to more definitely separate the user interface from the code-behind. According to Microsoft's vision, a user interface designer (who has design experience) would build the user interface and then a programmer would attach the code-behind. In practice, the separation isn't as great as you might like and you cannot really test either the interface or the code-behind without the other being present. (Also many companies cannot afford to have separate user interface designers and programmers, so it may be a moot point.)
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. Both Expression Blend and Visual Studio let you manually edit XAML code if you like, and sometimes that's easier than trying to make the tools build XAML code for you. (In some cases, Expression Blend and particularly Visual Studio cannot generate XAML code to use certain features. In those cases you must edit the XAML code manually to use those features.)
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. Typically in a XAML file that element is a Window (the WPF equivalent of a Form) or a frame. The Window control also can contain only a single child, which is usually some sort of container control such as a Grid, StackPanel, or Canvas.
What Tools Do You Need?
All non-desktop versions of Visual Studio can create WPF applications. The WPF Window Desginer is similar in many ways to the Windows Forms Desginer. You can use its Toolbox to drop WPF controls on a window and then use the Properties window to set the controls' properties.
Adding code-behind is also similar to the way you add code to handle events for a Windows Forms application. You can double-click on a control to open the code editor for the control's default event, or you can use the Properties window to create other events.
You should be able to build simple interfaces fairly quickly if you have experience with Windows Forms development.
Expression Blend is a tool built specifically for creating WPF interfaces. It works much as Visual Studio's WPF Window Designer. Although many of the tools have been moved around and modified, you should be able to use it fairly easily of you have experience using Visual Studio. Expression Blend also provides some useful features that are missing in Visual Studio, such as the ability to capture property animations.
Expression Blend does not provide all of the support for editing code that Visual Studio does so you'll probably still want Visual Studio for writing code-behind. Expression Blend is also a fairly expensive product (there is a trial version), whereas Visual Studio has a free express edition.
Probably the best working environment for writing WPF applications includes both Visual Studio (for code editing and some layout work) and Expression Blend (for property animation and some layout work).
No matter which tool you use, it is often easier to simply modify XAML code by hand rather than trying to figure out how to make the tools do it for you. For Windows Forms applications, you can do pretty much all of your user interface design work with the Windows Forms Designer. For WPF applications, there are times when you'll want to just modify the XAML code directly.
What are WPF's Advantages and Disadvantages?
WPF has several important goals and benefits including:
- Better use of graphics hardware
- WPF is layered on DirectX, which provides better access to modern graphics hardware than the older GDI and GDI+ graphics routines do. That makes it easier to transform objects (for example, rotate buttons), display multimedia, and produce 3-D graphics. It also gives you easy graphic effects such as embossing and making drop shadows.
- Retained mode graphics
- This is really a consequence of the preceding advantage, but it deserves a bit more description. To draw something (a line, button, image, or whatever) in WPF, you create an object. That object redraws itself as needed. That means you don't need to do any drawing in Paint event handlers. It also means that you can transform the object without losing any resolution. In particular, you can zoom in on a line, curve, or piece of text and it remains at high resolution without showing any graininess.
- Property binding to provide animation
- This lets the program animate a property (such as a location or size) over time. The objects that perform the animation handle timing issues so you don't need to.
- Property inheritance
- Most controls inherit the property values of their containers. For example, you can easily change the fonts or colors of a whole bunch of controls by changing the properties of their container.
- Styles let you bundle property values in a package that you can then easily apply to individual controls or to a type of control. For example, you can define all of the size, font, color, and other properties for all Buttons on a window.
- Templates let you change the pieces that are used to build other controls. For example, a normal Button is drawn as a rectangle but you could make a Button that is based on an ellipse or some other shape.
- Consistent control containment
- Windows Forms has some restrictions on what kinds of objects can be contained in other objects. For example, a Label or Button can contain text and images but not other controls. In WPF a Label or Button can hold most things including other controls. For example, you could put a Grid inside a Button and then put other controls inside the Grid. This model is still not completely general (some controls can hold only a single child while others can hold many), but it does let you build more powerful combinations than Windows Forms does.
- Separate user interface and code-behind
- The idea is that a user interface designer can build the interface while a programmer writes the code-behind. In practice, the separation isn't as clean as you might like. For example, the user interface designer cannot test the interface if it includes definitions for the events in the code-behind. That means you either need to keep these two people fairly closely synchronized, or you need to attach all of the event handlers at run time with code. All in all, the separation isn't much greater than it is in a Windows Forms application, although there are some new tricks you can use such as loading a new interface at run time.
- New controls
- WPF comes with a whole new set of controls. Some, such as Label, Button, and TextBox, fill the roles of Windows Forms controls. Some, such as Expander and FlowDocumentScrollViewer, are new.
- Declarative programming
- Some tout this is a big new benefit. In declarative programming, you use static code to define the user interface and its behavior instead of writing code that must respond and execute to take action. This makes sense if the user interface just sits there but it's less clear that it's a big benefit when you need to perform complex actions in response to events. Trying to make static code handle dynamic events can be confusing.
WPF also has some disadvantages:
- Steep learning curve
- You don't need to throw away everything you've learned about .NET programming, particularly for code-behind, but there's a ton of new stuff to figure out.
- Missing controls
- WPF is missing some controls that you've grown used to such as DateTimePicker, MonthCalendar, PropertyGrid, and BackgroundWorker.
- While WPF has a more consistent object model than Windows Forms in some ways, it is inconsistent in others. For example, property inheritance doesn't always work as you might expect.
- Mediocre editors
- The WPF Window Designer and Expression Blend's designer aren't nearly as good as the Windows Forms Designer for some tasks. (It really seems like the programmers who wrote these hadn't used Visual Studio before.) However, they are getting better with each new release.
- Expression Blend features missing in Visual Studio
- Expression Blend has some tools (such as recording property animations) that are missing in Visual Studio, obviously to give you a reason to buy Expression Blend.
A clean separation between the user interface design and the code-behind isn't there, and many companies can't (or won't) spend the money to have separate user interface designers and programmers, so Microsoft may as well give up on that idea.
Summary (Why Should You Care?)
WPF includes tools that let you build some amazingly engaging applications. They can interact with the user in new ways, provide simple animations, and use a new look and feel without requiring a lot of code-behind. They can also run in the browser in several ways, including as an XBAP or a Silverlight application.
Features such as styles and templates promote consistency and let you change a user interface's appearance and behavior relatively easily.
The biggest disadvantage to WPF is the steep learning curve. You won't need to start from scratch, particularly when it comes to writing code-behind, but to get the most out of WPF will take some experience. Even after a fair amount of practice, building a WPF application will probably take longer than building a comparable Windows Forms application. Probably not twice as long, but long enough to be worth considering.
I've heard some developers say you should use WPF for all new development. Others are more than happy to stick with plain old Windows Forms programming. Personally I enjoy WPF and the results it produces. If you want to hire me to help write WPF applications for you, please let me know. (Of course, please let me know if you want help with Windows Forms applications, too.)
In any case, you should at least learn a little about WPF. For a solid introduction to WPF programming, see my book WPF Programmer's Reference. If you just want to try it out briefly, you might want to dig through Microsoft's WPF web pages. That should at least give you a taste so you'll know whether you want to go into WPF further and add it to your resume.
For some more examples pictures showing things you can do with WPF, take a look at the web pages for my book WPF Programmer's Reference. All of these images were produced relatively easily with WPF. Many would have been much harder to produce with Windows Forms.