Tag Archives: C#

Introduction to WPF

Windows Presentation Foundation, or WPF for short, is a set of programming tools from Microsoft that allows the creation of graphical user interface (GUI) applications. It follows Windows Forms, which dominated the GUI application-building scene for .NET in the mid-2000s.

The main idea behind WPF is the separation of the graphical design aspects of an application from the coding logic behind the interface. For example, if we write an application that consists of a button which the user can press in order to get the program to do something such as display the current date and time, the graphical design of the program consists of determining what the button should look like, where the button should be placed within the window, and any other purely graphical aspects of the program. The code that runs when the button is pressed determines the date and time and sends this information back to the display so the user can see it. In WPF, the graphical aspects are separated more or less completely from the inner code that gets run when the button is pressed.

Other programming environments such as Windows Forms (which preceded WPF) or Java (nothing to do with .NET) use only one computer language to write all the code, so that if a separation between the design and the inner code is required, it must be done by writing bits of code in a certain way so that this separation is maintained. In WPF, the code for the design is actually written in a different language from the code that runs internally.

The WPF design code is written in a language called XAML, which is, in effect, just XML with a particular set of tags defined to match up with the requirements of WPF. XAML stands for Extensible Application Markup Language. The internal logic of a WPF program can be written in any standard .NET language, with the two most popular being C# and Visual Basic. In this introduction, we will use C#.

I’m assuming that the reader will know the basics of C#, including the ideas behind object oriented programming, so you will be familiar with the uses of classes and objects as well as the basic statements of C#.

To me, having to write XML (and hence XAML) by hand is excruciating. Although XAML code is more or less human-readable, it is to my eye unspeakably ugly, so I shy away from writing it from scratch. That is, I wouldn’t be able to stomach writing WPF applications if it weren’t for some Integrated Development Environments (IDEs) which allow you to build the graphical design of a program interactively, and then generate the XAML for you.

Fortunately, there are such IDEs around. In fact, there are two, and they are both written by Microsoft. One is the old standard, Visual Studio, which has been around in various incarnations for many years, and contains a reasonably competent GUI editor coupled with a very good code editor. The other, Expression Blend, is more recent. Although it contains a code editor, its strength is in graphical design.

Now I know that the main idea behind WPF is the separation of design from code, but it really does seem rather silly for two monolithic programs to be used for the creation of a single project. Surely a single IDE could be provided, and those interested in the graphical parts of a program could learn to use the graphical designer, while those working on the code could use the code editor. Of course, since both IDEs provide both sides of the project creation process, it is  possible, but unfortunately each of the two IDEs is better at one aspect of application writing than the other.

Anyway, having said that, what I’ll do in the next post is show how these two IDEs can be used to work together to create a simple WPF application.