A Quick Look at the Compact Framework

by Brian Jepson


Microsoft recently released the Smart Device Extensions for .NET (Beta 1) to MSDN subscribers. Although I am an MSDN subscriber, at the time the extensions came out, I didn't have a smart device :-(



Technically, you don't need a smart device to play with these extensions, since they include an emulator. However, I wanted the real thing (total coverage), so I went hunting for a decent Pocket PC that runs the latest Pocket PC 2002 operating system. I was upset to find that the leading handhelds cost upwards of $500, until I came across reviews for 5 handhelds. #1 on that list is the Audiovox Maestro PDA-1032, a rebranded Toshiba Pocket PC e570 with half the memory. I was able to find the Maestro for $299, so I went ahead and ordered it.



After it arrived, I naturally spent some time looking for games to run on it... the compact framework could wait. The next day, I downloaded the compact framework, and fired up Visual Studio .NET. I'm not much of a Visual Studio .NET user (I edit most of my C# using vim), but I decided I could make an exception, since Visual Studio .NET makes it very easy to work with compact framework applications.



Creating an Empty Application



To create a Pocket PC application, I did the following:


  1. At the Visual Studio Start Page, I chose "New Project".
  2. Next, I selected "Smart Device Application" from the Visual C# Projects.
  3. I chose Windows Application as the project type, and Pocket PC as the target platform. A blank form (Form1) and backing code (Form1.cs) appeared.
  4. I dropped a ComboBox on the form.
  5. Finally, I changed the name of the ComboBox to lstCommodities.


Connecting to a Web Service



At this point, I could have deployed the application to the smart device, but it would just be a form with an empty list box. So, I decided I'd plug it into a web service that I've been working on for a tutorial at the O'Reilly Emerging Tech Conference. The web service can return a list of energy commodities and price quotes.



To hook up my application to the web service, I did the following:


  1. Right-clicked on my project in the Solution Explorer and select "Add Web Reference"
  2. Pasted in the WSDL for my web service.
  3. Clicked "Add Reference"


This left me with a namespace, SmartDeviceApplication1.com.brinkster.www30 (it's a concatenation of my project namespace and the host name of the web service), that I could import with the using statement. So, I added this line to Form1.cs:


using SmartDeviceApplication1.com.brinkster.www30;


Next, I wanted to invoke the web service and stuff the results into the combo box. That namespace that I imported it full of all sorts of goodies, including a class (CommodityFun) that exposes the web methods, and classes (Commodity, Quote, etc) for all the return values of the web service. In this case, I'm interested in a list of commodities, so I need to deal with the Commodity class. Here is the code I added to my form's source code:


private void Form1_Load(object sender, System.EventArgs e)
{
CommodityFun wsfun = new CommodityFun(); // proxy class for remote web service
Commodity[] commodities = wsfun.ListAll(); // invoke a web service method
foreach (Commodity c in commodities)
{
lstCommodities.Items.Add(c.name); // add each commodity to the combo box
}
}


Not much to it, is there? The web reference makes sure that my application knows about the definition of the web service proxy class (CommodityFun) as well as classes for the return values (such as Commodity), so I can refer to them as though they were locally defined.

Deploying the Application



This was the easy part. I selected Start from the Debug menu, and Visual Studio .NET deployed everything (the compact framework and the application) to my Pocket PC. A few seconds later, the form appeared on my Pocket PC screen, and I was able to debug it from Visual Studio .NET. Figure 1 shows a screenshot (from the emulator, since I haven't figured out how to take a screenshot from my Pocket PC).




Figure 1. A simple web service client



Here's an article I found that walks you through a Compact Framework example with many more screenshots: Creating Pocket PC Apps with the Smart Device Extensions.

That's my trivial example. Have you built anything complex with the compact framework? Do you plan to?