Using the .NET compact Framework


Microsoft created the .NET Compact Framework for one single reason. To develop applications!!The .NET Compact Framework is used to develop applications for smart devices. By smart devices we mean devices that run the Pocket PC 2000, Pocket PC 2002 and Windows CE.NET operating systems. These operating systems are targetted for mobile devices (like PDA, Pocket PC) that have small display areas, small storage areas and run on batteries.

Developing applications for devices has never been easier! Earlier, people had to grapple with the eMbedded Visual Tools kit from Microsoft. This toolkit essentially consists of an operating system and development tools (like embedded Visual Basic and embedded Visual C++). Application also had to deal with different programming models and understand and work with the API of the operating system. With the advent of the .NET Compact Framework, Microsoft has unified the programming models between developing applications for PC's and applications for devices. The
.NET Compact Framework provides a managed execution environment for applications on devices and makes writing applications extremenly easy.

In this article, we will see how to write a simple .NET Compact Framework application and deploy it onto a device. This article provides a step-by-step instruction on how to write the application.

The .NET Compact Framework

The .NET Compact Framework is a subset of the functionality found in the .NET Framework. Major portions of the .NET Framework were removed, since these did not make sense in the devices world. When we say removed, it does not mean that the code was simply cut-off and the remaining code got renamed as Compact Framework :-) Microsoft re-wrote large chunks of the code to take into account the form factor of small devices.

Common Language Runtime

The CLR is the most important part of the .NET Compact Framework. It is responsible for taking a .NET assembly and then setting up an application domain for it to run. A native just-in-time (JIT) compiler is used to compile the MSIL code to the actual machine bits. The CLR provides the services of memory management, garbage collection and class loading. The CLR also manages the security structure in which the application executes.

The CLR consists of two parts. One is the direct execution engine which is responsible for execution of the code and the other is the base class libraries which is a set of reusable classes that contain a basic set of building blocks that applications can use. The following figure shows a typical contruction of the .NET Compact Framework.



The diagram is pretty straight forward.

  1. Applications, device libraries and base class libraries occupy the managed space. The CLR provides the execution environment for these applications along with a set of base services.
  2. The execution engine itself is a native executable, as is the Platform Adaptation Layer which is an abstration between the execution engine and the underlying operating system.

    These two layers are packaged as a single executable called MSCOREE.

In the .NET Compact Framework, there is one implementation of the MSCOREE for each of the supported processors. If future platforms become available with more capabilities, it is only the PAL that needs to be changed, since it contains platform specific features that will change between each hardware.

Features Missing From .NET Compact Framework
As mentioned earlier, the .NET Compact Framework is a subset of the full .NET Framework and major sections were ignored because they implemented features that were specific to the PC platform, which did not make sense in the device world. The following is a brief summary of the features that are excluded from the .NET Compact Framework.

  1. No application configuration files.
  2. No support for COM Interop.
  3. No support for remoting.
  4. No support for printing.
  5. No support for the SoapFormatter or the BinaryFormatter classes.
  6. No support for XPath and XSLT.
  7. No support for the System.Web namespace.

Ok, enough of basics!! Entire books have been written on the Compact Framework, but we don't have space for it. So let's start writing a simple application.

Availability of .NET Compact Framework

Visual Studio .NET includes all the capabilities that allow you to write and debug a device application (called Smart Device Extensions). You use all the tools and techniques that are available for developing normal application when writing device applications too. Instead of using the .NET Framework class libraries, you use the Compact Framework specific libraries.

Note that as of now, the .NET Compact Framework supports only the Visual Basic.NET and the C# language.

Developing a Sample Application

Let us now see the steps in developing a simple application using the .NET Compact Framework. We will write an application that displays a simple message to the user. Since you might not have access to an actual device, we will see how to write and deploy an application to the emulator that is supplied with Visual Studio .NET.

Step - 1: Creating the Project
The first step in creating a device application is to select the appropriate project type. A device project in Visual Studio .NET is called a Smart Device Extensions project. You need to select this project and provide a name for it, as shown in the following figure.

After deciding the application name, click on the [Ok] button.

Step - 2: Deciding the Profile
After deciding the application name, Visual Studio provides a set of device profiles. A device profile
is a set of project types targetted for a specific platform. The profile dialog box allows you to select the profile that you want to use, as shown in the following figure.

Here, select the Pocket PC platform and the Windows Application project. This means that we are targetting the Pocket PC platform and we are going to write a windows application. After making the selection, click on the [Ok] button.

Step - 3: Design the Application
After deciding the target environment and the type of application, all that is left is to design the application. Note that based on your selection, Visual Studio .NET might display different design environments. For our selection of Pocket PC/Windows application, the following interface is shown:

Note that the design surface is very similar to that of a desktop application. You simply drag and drop the appropriate controls onto the design surface. The final interface will be as shown:

Note that you use very similar controls to that of the desktop application. It is recommended thatyou do not resize the form, since Visual Studio .NET has decided the interface size based on the device profile that you selected. Now let's write some code to:

  1. Populate the combo-box with some values.
  2. Display some text when the button is clicked.

Here is the source code for these actions.

Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
ComboBox1.Items.Add("Robin Cook")
ComboBox1.Items.Add("Wilbur Smith")
ComboBox1.Items.Add("J.K. Rowling")
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim strResult As String = Nothing
strResult = "You Entered:" & System.Environment.NewLine
strResult = strResult & TextBox1.Text & System.Environment.NewLine & System.Environment.NewLine
strResult = strResult & "You Selected:" & System.Environment.NewLine
strResult = strResult & ComboBox1.SelectedItem
MessageBox.Show(strResult, "Message", MessageBoxButtons.OK)
End Sub

You can see that the code is very similar to that of a desktop application. That's that commanality you have between desktop applications and device applications. Amazing!!

Step - 4: Executing the Application
Having written the application, the final step is to execute the application. Choose the execute button. You are now presented with the following dialog box:

Here, you choose the deployment options. You can decide to deploy to either an emulator or to the actual device. We will choose the emulator. Now begins the interesting part!! The emulator is launched and the application deployment starts. Since the application uses the .NET Compact Framework libraries, the .NET Compact Framework is deployed first, as shown in the following figure.

Where does Visual Studio .NET get the files for the .NET Compact Framework?? You can see all the relevant files at the following location: C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE. Below this folder you will see the various version of the Windows CE operating system and within these folders, the relevant files for the various processors are present. After the relevant dependent files are installed, the actual application files are copied over and the application is started as shown in the following figure.

Note that once we click the button, we get a meesage box informing us about our selection. One immediate thing that you would have noticed while working with the application is that there is no tab key to move between fields. This is one of the challenges of developing for a device. We need to keep in mind that the only entry method for users is a stylus pen, thus they have to tap through the various fields of the interface.

OK, we are done!! When you close the emulator, you are given the option to either Save the emulator state or to Turn off the emulator. Each option has its own advantages.

  1. When you choose Save the emulator state, any installation that Visual Studio .NET did (for example the .NET Compact Framework) is retained. Thus, when you re-execute a project, these files are not deployed again.
  2. When you choose Turn off the emulator, we are essentially resetting the machine. Thus, all files will be re-deployed everytime to execute it.

Note that everytime you Save the emulator state and you close your application (by clicking on the (x) icon), the application is actually not closed, but rather pushed into the background. Thus you need to stop this application from running, otherwise, you will not be able to deploy the application again (you will get a sharing violation error). To stop the application from running, choose Start / Settings / System / Memory / Running Programs and then click the application that you want to stop and click the [Stop] button. This is shown in the following figure:

Ok, that was an interesting journey :-) One other thing to keep in mind is to definitely test your application with an actual device before deploying it into production. This is important because, the emulator is not the actual representation of how an application might execute in real time. You need to remember that an emulator actually executes in your desktop PC, which is more powerful than the actual device. Also, when working with the emulator, you have access to the keyboard, but in the real-world the user only has a stylus. Thus you should not develop interfaces that have too many input fields.

Conclusion

In this article we have seen how to use the .NET Compact Framework to develop a simple application and also how to deploy the application onto the device. The Visual Studio .NET development environment provides a very productive IDE for developing smart-device applications and also provides excellent emulation and debugging support. There are many different types of applications that you can write using the .NET Compact Framework and this hopefully helped you make a start. Have fun!!


Similar Articles