U2U Article: "Let's go compact: the .NET Compact Framework" Copyright © 2002 by U2U nv/sa, Belgium. All rights reserved.
Please address any questions or suggestions to U2U.

Let's go compact: the .NET Compact Framework

"Developing embedded applications with het .NET Compact Framework 1.0 and Visual Studio .NET 2003"

Lees ook de Nederlandse versie van dit article

Author: Wim UYTTERSPROT, wim@u2u.be
Article was published in the Microsoft .NET Magazine, the Netherlands, March 2003

Microsoft .NET Magazine

Abstract  

For the development of embedded applications targeting the Pocket PC or Windows CE devices, we can use the Microsoft .NET Compact Framework 1.0. This framework is a subset of the standard .NET Framework and is integrated in the new Microsoft Visual Studio .NET 2003. Developers do not depend anymore on the Embedded Visual C++ tools when programming standard embedded business applications.  The programming languages C# and VB.NET are making the .NET Compact Framework highly accessible.

Figure 1. Applying the .NET Compact Framework: Ordering “Zaanse” cookies by means of ADO.NET and XML Web services on a Pocket PC

The .NET Compact Framework

The compact framework can be compared with the standard (and full) .NET Framework. It is a subset of it, and it offers us analogously an application platform consisting out of a series of runtime components. The main difference is that the underlying operating system is not Windows NT, but the Windows CE operating system (Figure 2). Therefore it is rather obvious that the runtime components of the .NET Compact Framework will be quite ‘compact’.

Figure 2. The .NET Compact Framework installs on top of the Windows CE operating system

But first, some facts: The .NET Compact Framework installs on top of the Windows CE operating system and consumes only one and a half MB on the hard disk. An embedded .NET application will be deployed on a Windows CE device in Intermediate Language format. For starting a minimal embedded .NET application and loading the CLR, there is only 1 MB of RAM memory needed. We can easily calculate that the .NET Compact Framework is approximately 20 times smaller in file size than the full version. For that reason, the memory usage that is assigned to the .NET functionality could be reduced 5 to 10 times. This kind of optimisation has of course consequences for the way we have to program our Smart Device applications in Visual Studio .NET, as lots of helper-function and helper properties have been removed in the compact framework. Let’s illustrate this with an example: for building an embedded Windows application we only have 28 of the 35 desktop controls to our disposal. Another example is the well-known Control class: in the compact framework this class only counts 27 instead of 76 properties, 38 instead of 182 methods and 17 instead of 58 events (see Figure 3).

 
Figure 3.
The .NET Compact Framework is compact. Take for example the well-known Control class: in the standard version of the .NET Framework this class counts 316 public members (left image); in the compact version, the Control class counts only 82 public members (right image)

 

Microsoft Visual Studio .NET 2003

Visual Studio .NET 2003 makes Web Services also extremely accessible for embedded applications. In a Visual Studio .NET solution, where a Web service project has been implemented, it is simple to add a project of type ‘Smart Device Application’ as client of that Web service. It is sufficient to choose the type ‘Smart Device Application’ as project template (see Figure 4). Visual Studio .NET will then create a project with an empty Windows Form, exactly sized to the screen of a Pocket PC respectively a Windows CE device. In the Solution Explorer we can find a list with references that refer to the assemblies needed by the applications. Although we see no difference at first sight, a closer look reveals that the references do not refer to the standard framework, but to the assemblies of the .NET compact framework. For example: the System.Windows.Forms-reference points to a path similar to ‘C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\System.Windows.Forms.dll’.

Figure 4. The  template for a .NET Compact Framework application

A programmer, who is familiar with the standard framework tools, will immediately recognize all operations and manipulations, when he or she wants to build a Smart Device application, wants to create a Windows Form and code the necessary event handling (see figure 6).

Visual Studio.NET is an ideal tool for building .NET Compact Framework applications: in the Solution Explorer we added two Window Forms, FormProducts and FormOneProduct. We defined a reference to a Web service with as namespace www.u2u.net and automatically we obtained a proxy class. This proxy class is implemented in the Reference.cs file that is linked to the web service by means of the Reference.map. Also in the compact world, the web services are described by a web service definition language (WSDL) file; here it is the NorthWindSevice.wsdl file.

In the toolbox we select the required compact CF controls; we drag and drop those on the Design Window and make use of the Properties Window; and we implemented the event handlers, using the same techniques when programming for the standard framework. Consider for example the sample code in figure 5 with in it the event handler of the GetProducts button. There is no difference with an event handler that would have been written for the standard .NET Framework.

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace U2USamples
{
    public class FormProducts : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button buttonGetProducts;

        public FormProducts()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.buttonGetProducts = new System.Windows.Forms.Button();
            this.buttonGetProducts.Location = new System.Drawing.Point(1, 1);
            this.buttonGetProducts.Size = new System.Drawing.Size(102, 24);
            this.buttonGetProducts.Text = "Get Products";
            this.buttonGetProducts.Click 
                       += new System.EventHandler(this.buttonGetProducts_Click);
            ...
        }

        private void buttonGetProducts_Click(object sender, System.EventArgs e)
        {
        ...
        }
    }
}

Figure 5. Code - The  event handling mechanism in the .NET Compact Framework does not differ from the one of the standard .NET Framework.

 
Figure 6. Visual Studio.NET is the ideal tool for building .NET Compact Framework applications. In the toolbox we find the compact CF controls. The Solution Explorer shows here a VS.NET solution consisting of two projects: a web service project and a second for our Smart Device client applications.

The strong points of the compact framework can be experienced in particular when we start to consume Web services in our embedded applications, and also when we have to perform intensive data – in special XML- manipulations. For adding a Web Reference to a Smart Device Application project, we can rely on the Solution Explorer wizards. Automatically Visual Studio .NET adds a proxy class (see the sample code in Figure 7), that we can call from our Smart Device applications.

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="NorthWindServiceSoap",
                                           namespace="http://www.u2u.net/")]
public class NorthWindService
                     : System.Web.Services.Protocols.SoapHttpClientProtocol {

    public NorthWindService() {
        this.Url = "http://www.u2u.net/Northwind/Northwindservice.asmx";
    }

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
     "http://www.u2u.net/GetProductsAndCategories",
     RequestNamespace=" http://www.u2u.net/",
     ResponseNamespace=" http://www.u2u.net/",
     Use=System.Web.Services.Description.SoapBindingUse.Literal,
     ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
     public System.Data.DataSet GetProductsAndCategories() {
    object[] results = this.Invoke("GetProductsAndCategories", new object[0]);
        return ((System.Data.DataSet)(results[0]));
    }
    ... 
}

Figure 7. Code - Also in a compact framework application, Web services are consumed by means of proxy classes. The code in this example was generated by Visual Studio .NET. NorthWindService is here a proxy class and GetProductsAndCategories a proxy method

The code that is generated for this proxy class is very general: it can be used both by standard and by compact .NET framework clients. In other words, the .NET Compact Framework supports the standaard proxy code. For our Smart Device application this means that the Windows Form easily can consume the web service: the form will send a request to an instance of the proxy class (see the example code in Figure 8) and the proxy object will invoke, in his turn, the web service.

www.u2u.net.NorthWindService webservice = new www.u2u.net.NorthWindService();
DataSet dataset = webservice.GetProductsAndCategories();

Figure 8. Code - The .NET Compact Framework ondersteunt de standaard proxy-code voor webservices. Dit betekent dat we webservices kunnen blijven “consumeren” met behulp van de dot-operator.

The code of figure 8 is remarkable because of its simplicity. After the scenes of the proxy class, the web service call makes use of SOAP serialization to obtain a DataSet object as return of the call. In our example, the web service sends a somewhat complex dataset with two tables, named ‘Products’ respectively ‘Categories’. As with the standard framework, the compact framework classes keep the complexity transparent. The compact dataset-object acts as a wrapping box that contains tables and relations and that is serializable by design.

In the code example of Figure 9, we manipulate the dataset of the compact version of ADO.NET. A DataTable and a DataView object are used. The last one, named ‘dataviewCategories’, is bound with the DataSource property of the ComboBox object ‘comboBoxCategories’. From our dataset we extract a second DataTable and create a second DataView object, named ‘dataviewProduct’. We formulate a RowFilter, using the static Format function of the String class. Once again, as an experienced .NET programmer, you feel at ease…

DataTable datatableCategories = dataset.Tables["Categories"];
DataView dataviewCategories = datatableCategories.DefaultView;
comboBoxCategories.DataSource = dataviewCategories;
comboBoxCategories.ValueMember = "CategoryID";
comboBoxCategories.DisplayMember = "CategoryName";

DataView dataviewProducts = dataset.Tables["Products"].DefaultView;
dataviewProducts.RowFilter = String.Format("CategoryID = '{0}'",
                             datatableCategories.Rows[0]["CategoryID"]);
dataGridProducts.DataSource= dataviewProducts;

Figure 9. Code - The .NET Compact Framework supports ADO.NET

Visual Studio.NET 2003 allows us to choose between C# and Visual Basic as standard language for accessing the .NET Compact Framework and its wizards, without imposing restrictions on other programming languages. As result of compiling our code, the Portable Executable is built in Intermediate Language (IL). This one is only usable on Windows CE-environments where the binary files of the .NET Compact Framework are installed. The IL instructions, which are the result of the compilation process, can be studied by means of the IL disassembler tool ILDASM.EXE (see Figure 10).

Figure 10. Smart Device applications are compiled in IL, what can be studied by means of the tool ILDASM.EXE

Deployment and debugging

It becomes even better when we want to deploy our Smart Device applications to a Pocket PC or any other Windows CE device. Directly from Visual Studio .NET it is possible to install applications in Release and even in Debug mode on a Windows CE-device that is connected to our development pc. We just need to click on the standard popup menu of the Solution Explorer in order to instruct Visual Studio to build the necessary CAB files. It is also possible to start up the compact applications from Visual Studio .NET; the studio takes care of everything: building the CAB files, deploying the assemblies to the de Pocket PC and starting up remotely the applications.

Together with the .NET Compact Framework, two emulators are installed on the developer’s pc, one for Pocket PC and another for Windows CE .NET. Both emulators offer us features to develop and debug compact .NET applications without the necessity to connect the developer’s pc to a real smart device. Debugging compact framework applications in Visual Studio .NET is done by setting the necessary breakpoints in the source code, starting the Smart Device applications in Debug mode, and choosing whether or not for emulation (see Figure 11).

 
Figure 11.Visual Studio .NET makes it possible to debug Smart Device applications by means of Pocket PC-emulator or Windows CE .NET emulator

Conclusion

The Microsoft .NET Compact Framework offers many new opportunities. By means of the Visual Studio .NET 2003, every .NET developer is now able to build Smart Device applications in a way they are familiar with

Sample code

1. Download the source code of the Northwind Web Service and install it as a IIS application, e.g. in the path c:\inetpub\wwwroot\northwind.

2. Download the source code of the U2U Smart Device Sample, install it in a local directory, e.g. in the path c:\u2u-samples

Contact me Contact


Contact me Receive U2U Newsletter.
Looking for a challenging job Download Brochure On Site Training Looking for a challenging job
Favorites Favorites

Copyright © 1999-2010 by U2U