The Compact .NET Framework is a powerful tool that allows Windows .NET developers to easily leverage their existing skills to build mobile applications. As a result, projects are completed within a shorter development cycle. Taking the smaller memory footprint into consideration, code transfer from one platform to the other becomes simpler. In fact, the abstraction layer built into the Common Language Runtime (CLR) allows .NET CE applications to work on multiple devices and processors. In this article, I'll show you how to get up and running with the Compact .NET Framework.
Obtaining the tools
The .NET Compact Framework (also known as NETCF) is officially available on all editions of Visual Studio.NET 2003. During the installation process, you have the choice of adding in an option called Smart Device Programmability. A beta of the Compact Framework was available to Visual Studio.NET 2002 users as a Smart Device Extensions (SDE) add-on. This add-on was available only to MSDN subscribers for a limited time.
If you are not new to mobile development, note that Microsoft will no longer be supporting the eMbedded Visual Tools 3.0. If you need to create unmanaged native code on the Windows CE platform, you can use eMbedded Visual C++ Version 4.0 (with Service Pack 2).
The eMbedded Visual Basic product is in the process of being phased out as a Windows CE development language. Microsoft recommends that VB developers switch to Visual Basic .NET. For backward compatibility, eMbedded Visual Basic applications are supported on Windows Mobile 2003.
If you don't actually own a copy of Visual Studio .NET, you can create Compact Framework projects using a free tool available on the Web. Using #develop (pronounced SharpDevelop), you can create C# and VB.NET projects using the .NET platform. The full-featured IDE is available for free under a GPL license.
This article focuses on developing software with the .NET Compact Framework using Visual Studio .NET 2003. To use the framework, you need: a development machine with a minimum of a Pentium II 450-MHz processor, at least 128 Mb of RAM, and 5 GB of hard drive space (to install Visual Studio). The .NET Compact Framework will run only on Windows 2000, Windows XP, or Windows .NET Server 2003.
What does it include?
The Smart Device Programmability option in Visual Studio .NET 2003 contains all necessary tools for building mobile .NET applications. It also includes a fantastic set of emulators to run and test Windows CE and PocketPC applications in the proper environments, so you don't actually need the device in hand to test the code you are deploying.
Developing a simple application
The .NET Compact Framework contains two elements: a unique class library and the CLR. The class library has been pared down to accommodate the limitations of most mobile devices. It also incorporates classes that take advantage of the extra functionality afforded by the devices. You can download a supplementary help file documenting the entire .NET Compact Framework API.
The CLR has multiple functions: garbage collecting, memory management, and Just-In-Time (JIT) compilation during program execution. Also, it performs essential functions such as security and error checking.
You have a choice of two languages to create your Compact .NET applications: C# or Visual Basic .NET. You can create projects specific to certain devices by selecting the appropriate profile. Visual Studio also supports Device Kits (also known as Device SDKs), which allows you to add in other devices as they appear on the market.
To begin developing applications, open up Visual Studio.NET 2003. Select a new project, name it, and select the Smart Device Application icon. This will launch the Smart Device Application Wizard. At this point, you can decide whether your application will target the Pocket PC or Windows CE device.
You can create three main types of applications:
- Windows Application—This is the most common option for building a Windows CE application. Selecting this application type presents a generic form and controls.
- Class or Windows Control Library—Select this option to build reusable code modules and class libraries that can be ported to multiple applications.
- Console Application—These apps are natively supported on Windows CE. Choosing this option allows you to build applications that run in the background without user intervention.
We're going to build a simple mobile app called Hello Builder to demonstrate how easy it is to develop an application that runs on the Pocket PC or any other mobile device.
First, create a new Visual Basic .NET project using the Smart Device Extension Wizard. Next, select the Pocket PC platform. Once you've worked through the wizard, Visual Studio generates an empty form. From the left panel, drag a button control onto the form and double-click it. Visual Studio .NET will generate most of the event coding necessary to control the button. Simply add in the following line of code:
MsgBox("Hello Builder.com", MsgBoxStyle.Information, "CNET")
To build the solution, click on Build | Build Solution in the top menu. Now, you need to transfer the application to a Pocket PC emulator or an actual Pocket PC for the testing and deployment phase.
To deploy the application, click on Debug | Start. You are presented with several viewing options, including the emulator and an actual device, if one is properly attached to the computer. To successfully push the application to a Pocket PC, you need to preinstall ActiveSync version 3.7 and attach the device to your computer using either a USB cable or serial cradle. The end result looks like Figure A.
If you want more information on developing Compact .NET applications, I would strongly recommend that you look up the official documentation on the Microsoft MSDN Library and the QuickStarts.