Before we can begin to display items using the Windows Presentation Foundation, we have to consider how Windows applications work within this environment. Up until now, we have written very simple programs. They have created and used resources directly. Our programs have produced output by interacting directly with the required devices and obtained input by binding to events generated by the hardware itself. We have noticed that when our program reaches the end of the Main method, it stops running. In other words, there is nothing managing the execution of our application; our programs have sole control of the platform. This is perfectly acceptable for very simple applications, such as our first flashlight program.
However, when programs start to use resources like windows for output, and need use managed input from keypads, we need something to look after these devices and manage the flow of events to and from them. The WPF, upon which the .NET Micro Framework Presentation classes are based, provides an Application class for this purpose.
The Application class does exactly what the name implies. It looks after an application. It serves as a container for services that are shared across a single application domain. In the .NET Micro Framework, you will create a single Application instance when your program starts, and use this to manage the behavior of your program and the resources it uses.
The Application class and the Presentation classes are provided in the form of a number of resource libraries that are added to your project.
Figure 7-13 shows the resources that should be present in your project before you can successfully build a WPF application. To simplify the code, you may also want to include these namespaces:
using System; using Microsoft.SPOT; using Microsoft.SPOT.Input; using Microsoft.SPOT.Presentation; using Microsoft.SPOT.Presentation.Controls; using Microsoft.SPOT.Presentation.Media; using Microsoft.SPOT.Presentation.Shapes;
Figure 7-13: Adding presentation resources to a project.
You can create an instance of an Application class just like you would any other.
Application app = new Application();
This line of C# creates a new instance of the Application class that is referred to by app. Note that, at the moment, if you run this program, you will not see anything happen, because you need to start the Application by calling its Run method and giving it a window instance to display.
Application app = new Application(); Window win = new Window(); app.Run(win);
The call of the Run method causes the Application to start and begin providing services for the window specified by the parameter. You can then create the display by adding display controls to the window and changing their properties.
Once a thread of execution enters the Run method of the Application class, it will not return until the application exits. In the case of a Windows program running on a personal computer, this would occur when a user closes the program. In a .NET Micro Framework application, it is unlikely this will never happen because a device will contain only a single program, which must never end. This means that all the configuration of your device (for example, starting timers and attaching interrupt handlers to events) must be done before the Run method is called.
In addition to managing the window display, the Application class has to route events from the hardware to the window's controls, for example, to allow an item on a list to be selected. We will deal with this aspect in the section titled "Binding to Hardware Events" later in this chapter; at this point, we are considering how displays are generated.