The Application Class

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.

Creating an Application Instance

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; 

image from book
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.

Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

Similar book on Amazon
Getting Started with the Internet of Things: Connecting Sensors and Microcontrollers to the Cloud (Make: Projects)
Getting Started with the Internet of Things: Connecting Sensors and Microcontrollers to the Cloud (Make: Projects)
Expert .NET Micro Framework (Expert's Voice in .NET)
Expert .NET Micro Framework (Expert's Voice in .NET)
Netduino Plus
Netduino Plus
Getting Started with Netduino
Getting Started with Netduino © 2008-2017.
If you may any questions please contact us: