Advertisment

.NET for Pocket Devices

author-image
PCQ Bureau
New Update

The .NET strategy is all about writing code once and executing it on any host that supports .NET, including compact devices, like smart phones, and PDA. And for this there exists a specialized version of .NET, called the .NET Compact Framework, which helps you write apps using the same design paradigm that you use for writing apps for desktop and distributed architectures using .NET. While .NET 1.0 supported the .NET Compact Framework as an add-on to the standard .NET Framework installation, starting with Visual Studio .NET 2003, there is a built in support for creating Smart Device Projects.

Advertisment

This means that there is inbuilt support for developing applications for compact devices.

Developing an app for compact devices is no different than selecting a project for any other type of app

We will look at Visual Studio .NET 2003 this time. The accompanying source code and the screenshots in the article will be specific to it.

Advertisment

New Project



Beginning to develop an application for compact devices is no different than from selecting a project for any other type of application you wish to create. 

We select the language of our choice, and the project being the Smart Device Application. For this article, we will be using the XML feed that MSDN website now makes available to us for various .NET technologies, like C#, and VB.NET. Using our application, the user can get the details of the latest happening for the technology of their choice by selecting it using a drop down box.

Once you click OK, the next screen that appears prompts for the target platform of the application .NET Compact Framework can build applications targeted for Pocket PC platform and for the Windows CE

platform.

Advertisment

Target Platform 



Visual Studio .NET 2003 installation also has installed support for the respective emulator for Pocket PC and Windows CE so that we may test our applications without requiring them physically. In addition to selecting the target platform, you also need to select the type of application, which you wish to develop. For our example, for creating a Windows Application for Windows CE. 

This helps you develop

an app to run on a smart device

Click on OK, and after some processing, you will see a form that is no different from a typical WinForm that you work with:

Advertisment

User Interface 



The user can select the technology of their choice, whose latest news they wish to read, by clicking on the appropriate tab and press the Refresh button. The titles of the topic are listed in the ListView control, which is in the Details mode. Clicking on the title shows the details in the Label controls below the ListView. For initialization, set the form title in the Load event of the form, as shown below:

private void frmMain_Load(object sender, System.EventArgs e)



{


// set the application title


this.Text = _strAppTitle;


}


_strAppTitle is an instance string constant that contains the application title. 

Advertisment

Reading MSDN Feeds



MSDN has made certain XML feeds available to developers that can be used to check what latest publications are available at MSDN website. This feed contains the article titles, URLs, description and publication date. These feeds are available for: 

  • MSDN
  • Visual Studio
  • .NET Framework
  • Visual C#
  • Visual C++
  • Visual Basic .NET
  • WebServices

Each of these feeds is available at a different URL. For instance, the Visual C# feed is available at

http://msdn.microsoft.com/vcsharp/rss.xml. Clicking the Refresh List button connects the application to the server and requests the relevant feed, as shown in the code excerpt below:

Advertisment

private void btnRefresh_Click(object sender, System.EventArgs e)



{


string strSource= null;


// read RSS feed for the right technology


switch(tabMain.SelectedIndex)


{


case 0: // MSDN


strSource = “http://”+_strFeedRoot+”/rss.xml”;


break


case 1: // Visual Studio


strSource = “http://”+_strFeedRoot+”/vstudio/rss.xml”;


break;


case 2: // .NET Framework


strSource = “http://”+_strFeedRoot+”/netframework/rss.xml”;


break;


case 3: // VC#


strSource = “http://”+_strFeedRoot+”/vcsharp/rss.xml”;


break;


case 4: // VC++ strSource = “http://”+_strFeedRoot+”/visualc/rss.xml”;


break;


case 5: // VB.NET


strSource = “http://”+_strFeedRoot+”/vbasic/rss.xml”;


break;


case 6: // WebServices


strSource = “http://”+_strFeedRoot+”/webservices/rss.xml”;


break;


}


ShowRSSFeed(strSource);


}


























__strFeedRoot is set to the root of the server domain name, which is msdn.microsoft.com. Once the correct source has been identified, the ShowRSSFeed method is invoked which uses the HttpWebRequest class to fetch the XML feed:

private void ShowRSSFeed(string strSource)



{


HttpWebRequest reqFeed = null;


try


{


reqFeed = (HttpWebRequest)WebRequest.Create(strSource);


}


catch


{


// there was an error


reqFeed =null;


}


if (reqFeed!=null)


{


// get the response..


HttpWebResponse respFeed = (HttpWebResponse)reqFeed.GetResponse();


// read the stream...


string strResponse = null;


StreamReader rdsStream = new StreamReader(respFeed.GetResponseStream(),Encoding.ASCII);


// ... till the end


strResponse = rdsStream.ReadToEnd();


// close all handles..


rdsStream.Close();


respFeed.Close();


reqFeed = null;


// show the details


ShowDetails(strResponse);


MessageBox.Show(“Feed successfully read!”,_strAppTitle,MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);


}


else


{


MessageBox.Show(“Unable to connect to the server!”,_strAppTitle,MessageBoxButtons.OK, MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);


}


}































Advertisment

If the WebRequest.Create call succeeds, we get the underlying HttpWebResponse object for the same and read the stream contents in ASCII encoding. The returned feed is pure XML that is parsed by the ShowDetails method. The important point to be noted is that the Compact Framework supports HttpWebRequest and HttpWebResponse classes. In addition, if you notice carefully, the MessageBox.Shpw method takes five arguments, the fifth argument specifies which of the displayed buttons of the dialog box will be the default button. Button1 indicates that the first button will be the default one. So, if we have Yes and No being displayed and we specify Button2, then No will be the default button.

Parsing the returned XML is not an issue, if we keep in mind its architecture. A typical block giving details of a new publication, for a technology, on MSDN will be like this:





Dr. GUI .NET 1.1 #0: Introduction to .NET, Hello World, and a Quick Look Inside the .NET Runtime 

Fri, 18 Apr 2003 07:00:00 GMT 



Join Dr. GUI under the hood of the .NET Framework, as he shows “Hello World!” programs in Visual Basic .NET, C#, and ASP.NET, and a lot more. 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnguinet/html/drguinet0_update.asp 



The tags contain the publication details, with the respective , <pubdate>, <description> and <link></link> tags containing publication specific details.</description></pubdate>

Action



The difference in development of compact devices, from the development of a typical WinForm application, lies in the way the application runs. Once it has been compiled and executed is requested, we get this dialog for our project.

When developing for compact framework, you have the option of deploying the application directly to the device, or deploy it to the device’s emulator. When you are developing and debugging the application, you would want to use the emulator, which is as good as real device!

Since our application is being designed for Windows CE, its emulator is selected by default. Clicking the Deploy button starts the emulator, if not active.

Next Visual Studio shall check if the deployment device has the Compact Framework installed or not. If not, it is good enough to install it for us. It proceeds to deploy the application and runs it. 

We have just developed and deployed an application for Windows CE and it works great. The emulator for Pocket PC and Windows CE 4.1 are installed as part of Visual Studio 2003 installation. 

The moral of the story is that if you know how to write applications for the desktop/distributed environments using Visual Studio, you can write them for compact devices too, like those running Pocket PC- and Windows CE-based OSs. You only need to select the right project type. The differences will come up as and by you start off to use .NET Framework Class Libraries, when you get to see the slight, and sometimes not so slight differences when developing for a compact device against a desktop development. 

In this article, we have seen how to write a simple application that works on a compact device. Next month, we take a look at some major class library differences when writing applications using the Compact Framework. The MSDN Feed Reader application is also available for download (binary only) from WinToolZone at

www.wintoolzone.com/dotnetapps.aspx.

Kumar Gaurav Khanna, 



Microsoft MVP, runs wintoolzone.com


Advertisment