{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
First Smart Device Program in C# Explained
Summary
This article discusses and analyzes various aspects in creating the first smart device program using .NET Framework and C#.
 
Table of Contents

Creating a Smart Device Solution / Project

Figure 1. Smart Device Project Template

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

Generated Files

Figure 3. Solution Explorer for the FirstSmartDevice application

FirstSmartDevice.sln and FirstSmartDevice.suo

FirstSmartDevice.csproj and FirstSmartDevice.csproj.user

Code Listing 1. The FirstSmartDevice.csproj.user file

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

Miscellaneous Files

Code Analysis

Program.cs

Code Listing 2. Program.cs

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

Form1.cs and Form1.Designer.cs

Code Listing 3. Form1.cs

Code Listing 4. Form1.Designer.cs

Running the Smart Device Program

Adding a Label

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

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

Running the Smart Device Program

Figure 6. Running the Windows Mobile program in an emulator

 
Article Series
Previous Article: This is the First Article of the Series
This article is part of the Series:
Article Series: Advanced First Programs in C# Explained
Next Article:
Test Your Knowledge: First Smart Device Program in C# Explained

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

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

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:

First Windows Forms Program in C# Explained

Figure 3. Solution Explorer for the FirstSmartDevice application

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

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <DeployDeviceID>25D984D9-0DFE-4DB1-A5A0-9A4F660BF2CE</DeployDeviceID>
  </PropertyGroup>
</Project>

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;

namespace FirstSmartDevice
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}

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:

C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.Windows.Forms.dll

As you can see, while the name of the namespace and assembly are same (as Windows Forms), these assemblies are coming from the .NET COMPACT Framework.

Similarly, the assemblies for all the other namespaces System, System.Linq, and System.Collections.Generic are also coming from the .NET COMPACT Framework SDK:

C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.dll

C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.Xml.Linq.dll

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

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;

namespace FirstSmartDevice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Code Listing 4. Form1.Designer.cs

namespace FirstSmartDevice
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.MainMenu mainMenu1;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.Menu = this.mainMenu1;
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.Text = "Form1";
            this.AutoScroll = true;
        }

        #endregion
    }
}

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.

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

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(4, 4);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(174, 20);
            this.label1.Text = "Hello From FirstSmartDevice!";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.label1);
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

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

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?



: One difference would be smaller screen vs. larger screen
Question 1 of 5
Article Series
Previous Article: This is the First Article of the Series
This article is part of the Series:
Article Series: Advanced First Programs in C# Explained
Next Article:
Test Your Knowledge: First Smart Device Program in C# Explained
Bookmark and Share This

More Articles With Similar Tags
This test has questions on developing basic web services with C# programming language.
This test has questions on the basics of developing an ASP.NET server control program with C# programming language.
Test your knowledge on the basics involved in creating the first ASP.NET Server Control program using C#.
How would you start with developing a Windows Mobile program using C# programming language?
icon-first-aspnet-server-control-solution-explorer.jpg
This article analyzes and explains the first ASP.NET Server Control. Server Controls have functionality and user interface that can be plugged into web application projects easily.
About  Contact  Privacy Policy  Site Map