Creating a Smart Device Solution / Project

In Microsoft Visual Studio 2008, choose File – New – Project to open the New Project window. Under the Visual C#, you can see a Smart Device Project Type. This Project Type includes a Smart Device Project template.

Figure 1. Smart Device Project Template

The Smart Device project creation is split into two parts. In the New Project dialog box, you will choose the Smart Device project template. One of unique features of developing for a ‘smart device’ (or PDA or Cell Phone) is that there are so many different choices.

Figure 2. Various options for the creation of a smart device project

As shown in Figure 2 above, you can see the number of options available just within the Microsoft world.

You have the option of developing for the following target platforms:

Windows Mobile 5.0 Pocket PC SDK

Windows Mobile 5.0 Smartphone SDK

Windows CE

Pocket PC 2003

You can also use different .NET COMPACT Framework versions:

.NET Compact Framework Version 3.5

.NET Compact Framework Version 2.0

And then, you have various templates available:

Device Application

Class Library

Console Application

Control Library

Empty Project

For this article, we will be choosing:

the ‘Device Application’ template

for the Target platform ‘Windows Mobile 5.0 Pocket PC SDK’

with ‘.NET Compact Framework Version 3.5’.

This is the latest available on Visual Studio 2008 at its release.

Generated Files

When you click OK in the ‘Add New Smart Device Project’ dialog box, Visual Studio will create the FirstSmartDevice solution and project with the basic files.

The files created (and the directory structure) are remarkably similar to the Windows Forms application discussed in the following article:

Figure 3. Solution Explorer for the FirstSmartDevice application

The similarity between Windows Forms applications and the Device Application on Windows Mobile makes sense, because they both are thick clients (or smart clients); the only differences being, the Windows Mobile applications have smaller screen and fewer resources (less memory, less disk, etc.).

FirstSmartDevice.sln and FirstSmartDevice.suo

In the root directory of the FirstSmartDevice application, you will find a solution file (.sln) and a .suo file. You will these solution files for all the Visual Studio solution types.

The .sln file is text-based, and contains the information about the projects that particular solution includes. And the .suo file is binary and contains the user-specific preferences.

FirstSmartDevice.csproj and FirstSmartDevice.csproj.user

The file with .csproj is the Project file. This file contains an exhaustive list of project related information. This file is discussed in other articles like Windows Forms application article mentioned above.

A more unique file is the .csproj.user file. The default contents of this file are shown in Code Listing 1.

Code Listing 1. The FirstSmartDevice.csproj.user file

Obviously, this file contains information about the device where this program would be run.

Form1.cs, Form1.Designer.cs, and Program.cs

These are the files that make up the first form and the code to start the program. These files are very similar to the files generated for the Windows Forms application.

The code in these files is discussed in a section below.

Miscellaneous Files

In addition to the above files, several other files are also generated as a part of the solution.

AssemblyInfo.cs

This file contains information about the generated assembly. These attributes for the assembly includes AssemblyTitle, AssemblyVersion, AssemblyCompany, etc. These are the same attributes that you will find in other projects that compile to a dll or exe as well.

Resources.resx and Resources.Designer.cs

The resources, like strings, are manipulated via these files.

Code Analysis

Now let’s look at the code in the three important C# files in this project.

Program.cs

Code Listing 2. Program.cs

using System; using System.Linq; using System.Collections.Generic; using System.Windows.Forms;

As you can see in Code Listing 2, this class has the Main() method where the C# program starts. This class looks just like that of the Program.cs generated for the Windows Forms application, except that EnableVisualStyles() and SetCompatibleTextRenderingDefault() methods are not called.

Just like in the Windows Forms application, the Run() method starts the message loop and displays the form Form1. This file includes the same namespaces as the Windows Forms program.

However, it must noted that these namespaces are not coming from the same assemblies as they are for the Windows Forms programs. If you look at the Properties window for the System.Windows.Forms reference in the Solution Explorer, you will see the following path for the dll:

Figure 4. The methods available in the .NET Compact Framework.

Only a subset of functionality that is available in .NET Framework is available in .NET Compact Framework. As you can see in the figure above, in the MSDN documentation, the methods available in the compact framework are shown with the icon of a PDA.

Form1.cs and Form1.Designer.cs

Code Listing 3. Form1.cs

using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;

The code shown for Form1.cs and Form1.Designer.cs are very similar to the ones you see in the Windows Forms projects. The difference, of course, is you will see some methods missing (as the .NET Compact Framework is smaller than .NET Framework). All the included namespaces are in the assemblies in the compact framework directory.

The constructor of the class Form1 calls the method InitializeComponent(), which is in the Form1.Designer.cs. As you add more controls to the form (like labels, text boxes, etc.), this method will grow. They all will be initialized when the Form1 instance is created.

Running the Smart Device Program

Now let’s add a label to the form and run it.

Adding a Label

Figure 5. Adding a label to a Smart Device form.

In order to add a label to Form1, drag it from the tool box and drop on the design surface of Form1. Form1.cs has a view that shows code and a Design view that renders the UI in a PDA.

Code Listing 5. The InitializeComponent() method after adding the label

After you add the label to the design window, Visual Studio Designer will automatically generate the appropriate label creation code to InitializeComponent() method, as shown in Code Listing 5.

You can see that a new label has been initialized and various properties (location, size, text, etc.) have been set.

Running the Smart Device Program

When you press F5 (or choose one of the other ways to run an application from Visual Studio), Visual Studio first needs to deploy this application to a device that has the appropriate OS (in this case Windows Mobile 5.0).

So, you will be provided with the list of emulators and devices on your machine. Choosing the USA Windows Mobile 5.0 Pocket PC R2 Emulator will display the following (after it took a little bit of time to deploy the program):

Figure 6. Running the Windows Mobile program in an emulator

Take a Quick Quiz on this Article

1. Which of the following projects (files and structure) is most likely to resemble a Smart Device Application project created using .NET Framework, C#, and Visual Studio?