Developing Smart Device Applications Using .NET Compact Framework


.NET Compact Framework is a set of technologies for writing applications using managed code on mobile smart devices. .NET Compact Framework is a subset implementation of the .NET Framework on a restricted device. It means that as a .NET developer, you can use your same skills and tools (Visual Studio .NET 2003) to build mobile applications. Building on the language-independence capability of .NET, .NET Compact Framework currently supports smart device development using Visual Basic .NET and Visual C#.

.NET COMPACT FRAMEWORK AVAILABILITY

The .NET Compact Framework (.NET CF) is an integral component of the 1.1 Release of .NET Framework and Visual Studio .NET 2003. (.NET Compact Framework was released as a beta technology add-on after the initial (1.0) release of .NET Framework.)


By definition, the CLR is platform independent, and applications developed using the .NET Compact Framework can execute on a variety of devices and platforms. Currently supported .NET Compact Framework devices include devices using operating systems in the Windows Mobile 2003 software. This includes Pocket PC, Pocket PC Phone Edition, and Smartphone operating system devices.

If you would like to better understand the exact subset of the class libraries and methods supported in the .NET Compact Framework, a good reference on smart device development is http://smartdevices.microsoftdev.com, which includes API documentation of the various APIs and class libraries that are included in the .NET Compact Framework. At the time of the writing of this book, the API documentation was available at http://smartdevices.microsoftdev.com/Downloads/Links_LinkRedirector.aspx?id=584.

Installation, Configuration, and Deployment

If you have the Visual Studio .NET 2003 environment, you are all set with smart device application development. Visual Studio .NET 2003 includes a Pocket PC 2002 emulator for developing, testing, and debugging applications in the same way it works with Web applications. If you want to use the newly released Microsoft Mobile Software 2003, which includes the Pocket PC 2003, Pocket PC Phone Edition 2003, you can download the Microsoft Pocket PC 2003 SDK from http://www.microsoft.com/mobile. Microsoft Smartphone 2003 SDK is also required for Smartphone application development.

After you have developed and tested your application using Visual Studio .NET and Compact Framework, you have a few options for deploying your application to your end users:

  • Copy the application executable and associated libraries to the device with .NET Compact Framework installed. If the Compact Framework is not already installed on the device, .NET CF Redistributable should first be used to install the Compact Framework on the device.

  • Build a cab file that can be used to install the application. The cab file includes the core .NET CF and can be installed with the application.

  • Create a custom installer using your favorite installation program.

Hello Smart Devices!

Listing 10.1 shows how the traditional Hello World application looks for .NET CF, a simple application to understand how you can write applications targeted for smart devices.

Listing 10.1 Hello World Smart Device Application
 using System; using Drawing; using Collections; using Windows.Forms; using Data; namespace HelloSmartDevice {    public class Form1 : Form    {       private Button Exit;       private Label label1;       private MainMenu mainMenu1;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          base.Dispose( disposing );       }       #region Windows Form Designer generated code       private void InitializeComponent()       {          this.mainMenu1 = new MainMenu();          this.Exit = new Button();          this.label1 = new Label();          //          // Exit          //          this.Exit.Location = new Point(56, 56);          this.Exit.Size = new Size(120, 24);          this.Exit.Text = "Exit";          this.Exit.Click += new EventHandler(this.Exit_Click);          //          // label1          //          this.label1.Location = new Point(16, 16);          this.label1.Size = new Size(208, 20);          this.label1.Text = "Hello Smart Devices";          this.label1.TextAlign = ContentAlignment.TopCenter;          //          // Form1          //          this.Controls.Add(this.label1);          this.Controls.Add(this.Exit);          this.Menu = this.mainMenu1;          this.Text = "Form1";       }       static void Main()       {          Application.Run(new Form1());       }       private void Exit_Click(object sender, EventArgs e)       {          Application.Exit();       }    } } 

As you can see from Listing 10.1, an application written for .NET CF is very similar to that for a Windows Forms application. Even the similar class libraries and namespaces are used. To get started with the development of a smart device, create a new project using Visual Studio .NET (using your favorite programming language ”either Visual Basic .NET or C#), this time using the Smart Device Application project type. The Smart Device Application Wizard will also query the target platform (Pocket PC, Windows CE, Smartphone) and the type of application (Windows Forms, class library, nongraphical application). After you select your appropriate platform, the IDE will generate a basic form (which looks very similar to a traditional Windows form, although you will notice that it is appropriately sized for your target platform). Similar to Windows Forms application development, you simply drag and drop form controls, this time from the Device Controls palette, and associate appropriate event handlers. For instance, the Hello World application shown in Figure 10.1 has two controls: a Label and a Button.

Figure 10.1. Hello World smart device application.

After you have developed and built the application, you are ready to test and debug it. This is where the integrated device development capability of Visual Studio .NET 2003 kicks in. You can choose where you would like to deploy and run the application, using a simple drop down and then run or debug the application (Figure 10.2). Whereas a typical Windows forms application runs on the desktop environment itself, a .NET CF application would run on a smart device. Visual Studio .NET 2003 provides integrated ActiveSync connectivity to download and deploy the application on the target device. If you don't have a device or would like to initially do some testing prior to actual device deployment, Visual Studio .NET 2003 also provides an emulator environment that runs a virtual device on the desktop and emulates a Pocket PC operating system.

Figure 10.2. Deploying and testing smart device applications.

Working with Data and XML

When you understand the process involved with smart device application development, the rest follows through. You can develop full-blown applications with rich user interfaces on the devices. One key thing to keep in mind is an understanding of the overall functional subset of the .NET Framework that is available for device application development.

For instance, consider a scenario in which a mobile application has been used to take orders for customers. The target device that has been selected for the application is a Pocket PC. However, unlike the desktop application scenario, the mobile application will not be always connected to a network. In fact, the model that has been selected for communication is to sync up data products and order data using an ActiveSync cradle with the computer. This is really different from a desktop environment where you can connect to a back-end database or enterprise applications using ADO.NET or Web services. In the case of .NET CF, you have to utilize the limited space available on the device to store product and customer data and the outgoing orders data. A couple of design approaches are possible. Existing customer and product data can be stored in the device as XML, which can be parsed by the application, and after the order is created, it can be stored as XML in an outgoing folder. When the device is synced, the outgoing orders can be transferred to the server and processed from there. The preceding approach totally decouples the communication between the device and the server and is quite suitable for any technology environment, whether the back-end is a database or an ERP application, such as SAP R/3.

For the purpose of this book, Listing 10.2 (shown in Figure 10.3) shows an implementation of the first approach. The parts for writing the XML into the file have been omitted but to illustrate the concept, the XML content is displayed as one of the GUI tabs. Note the use of tabs in the application. Because of the size of a Pocket PC environment, tabs can be effectively used to group and separate data-entry screens.

Listing 10.2 Order Entry Smart Device Application, Orders Are Stored in XML
 using System; using Drawing; using Collections; using Windows.Forms; using Data; using Xml; using IO; namespace OrderEntryApp {    public class Form1 : Form    {       private TabControl tabControl1;       private TabPage CustomerInfoTab;       private TabPage ProductInfoTab;       private Button SaveButton;       private Button ClearButton;       private Label CustomerLabel;       private TextBox CustomerBox;       private TextBox BillToCodeBox;       private Label BillToLabel;       private TextBox ShipToCodeBox;       private Label ShipToLabel;       private ComboBox ProductBox;       private ComboBox SizeBox;       private TabPage XMLTab;       private TextBox XMLBox;       private Label ProductLabel;       private Label SizeLabel;       private TextBox QuantityBox;       private Label QuantityLabel;       private MainMenu mainMenu1;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.mainMenu1 = new MainMenu();          this.tabControl1 = new TabControl();          this.CustomerInfoTab = new TabPage();          this.ProductInfoTab = new TabPage();          this.SaveButton = new Button();          this.ClearButton = new Button();          this.CustomerLabel = new Label();          this.CustomerBox = new TextBox();          this.BillToCodeBox = new TextBox();          this.BillToLabel = new Label();          this.ShipToCodeBox = new TextBox();          this.ShipToLabel = new Label();          this.ProductLabel = new Label();          this.ProductBox = new ComboBox();          this.SizeLabel = new Label();          this.SizeBox = new ComboBox();          this.XMLTab = new TabPage();          this.XMLBox = new TextBox();          this.QuantityBox = new TextBox();          this.QuantityLabel = new Label();          //          // tabControl1          //          this.tabControl1.Controls.Add(this.CustomerInfoTab);          this.tabControl1.Controls.Add(this.ProductInfoTab);          this.tabControl1.Controls.Add(this.XMLTab);          this.tabControl1.SelectedIndex = 0;          this.tabControl1.Size = new Size(240, 232);          //          // CustomerInfoTab          //          this.CustomerInfoTab.Controls.Add(this.ShipToCodeBox);          this.CustomerInfoTab.Controls.Add(this.ShipToLabel);          this.CustomerInfoTab.Controls.Add(this.BillToCodeBox);          this.CustomerInfoTab.Controls.Add(this.BillToLabel);          this.CustomerInfoTab.Controls.Add(this.CustomerBox);          this.CustomerInfoTab.Controls.Add(this.CustomerLabel);          this.CustomerInfoTab.Location = new Point(4, 4);          this.CustomerInfoTab.Size = new Size(232, 206);          this.CustomerInfoTab.Text = "Customer Info";          //          // ProductInfoTab          //          this.ProductInfoTab.Controls.Add(this.QuantityLabel);          this.ProductInfoTab.Controls.Add(this.QuantityBox);          this.ProductInfoTab.Controls.Add(this.SizeBox);          this.ProductInfoTab.Controls.Add(this.SizeLabel);          this.ProductInfoTab.Controls.Add(this.ProductBox);          this.ProductInfoTab.Controls.Add(this.ProductLabel);          this.ProductInfoTab.Location = new Point(4, 4);          this.ProductInfoTab.Size = new Size(232, 206);          this.ProductInfoTab.Text = "Product Info";          //          // SaveButton          //          this.SaveButton.Location = new Point(16, 240);          this.SaveButton.Text = "Save";          this.SaveButton.Click += new EventHandler(this.SaveButton_Click);          //          // ClearButton          //          this.ClearButton.Location = new Point(136, 240);          this.ClearButton.Text = "Clear";          this.ClearButton.Click += new EventHandler(this.ClearButton_Click);          //          // CustomerLabel          //          this.CustomerLabel.Location = new Point(8, 16);          this.CustomerLabel.Size = new Size(80, 16);          this.CustomerLabel.Text = "Customer";          this.CustomerLabel.TextAlign = ContentAlignment.TopCenter;          //          // CustomerBox          //          this.CustomerBox.Location = new Point(104, 16);          this.CustomerBox.Size = new Size(112, 22);          this.CustomerBox.Text = "ABC Company";          //          // BillToCodeBox          //          this.BillToCodeBox.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.BillToCodeBox.Location = new Point(104, 56);          this.BillToCodeBox.Size = new Size(112, 22);          this.BillToCodeBox.Text = "NYC101";          //          // BillToLabel          //          this.BillToLabel.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.BillToLabel.Location = new Point(8, 56);          this.BillToLabel.Size = new Size(80, 16);          this.BillToLabel.Text = "Bill To Code";          this.BillToLabel.TextAlign = ContentAlignment.TopCenter;          //          // ShipToCodeBox          //          this.ShipToCodeBox.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.ShipToCodeBox.Location = new Point(104, 96);          this.ShipToCodeBox.Size = new Size(112, 22);          this.ShipToCodeBox.Text = "NYC101";          //          // ShipToLabel          //          this.ShipToLabel.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.ShipToLabel.Location = new Point(8, 96);          this.ShipToLabel.Size = new Size(80, 16);          this.ShipToLabel.Text = "Ship To Code";          this.ShipToLabel.TextAlign = ContentAlignment.TopCenter;          //          // ProductLabel          //          this.ProductLabel.Location = new Point(8, 16);          this.ProductLabel.Size = new Size(80, 16);          this.ProductLabel.Text = "Product";          //          // ProductBox          //          this.ProductBox.Items.Add("Product 1");          this.ProductBox.Items.Add("Product 2");          this.ProductBox.Items.Add("Product 3");          this.ProductBox.Items.Add("Product 4");          this.ProductBox.Items.Add("Product 5");          this.ProductBox.Items.Add("Product 6");          this.ProductBox.Location = new Point(104, 16);          //          // SizeLabel          //          this.SizeLabel.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.SizeLabel.Location = new Point(8, 56);          this.SizeLabel.Size = new Size(80, 16);          this.SizeLabel.Text = "Size";          //          // SizeBox          //          this.SizeBox.Items.Add("Small");          this.SizeBox.Items.Add("Large");          this.SizeBox.Items.Add("Medium");          this.SizeBox.Location = new Point(104, 56);          //          // XMLTab          //          this.XMLTab.Controls.Add(this.XMLBox);          this.XMLTab.Location = new Point(4, 4);          this.XMLTab.Size = new Size(232, 206);          this.XMLTab.Text = "XML";          //          // XMLBox          //          this.XMLBox.Location = new Point(8, 16);          this.XMLBox.Multiline = true;          this.XMLBox.ReadOnly = true;          this.XMLBox.Size = new Size(216, 184);          this.XMLBox.Text = "";          //          // QuantityBox          //          this.QuantityBox.Location = new Point(104, 96);          this.QuantityBox.MaxLength = 5;          this.QuantityBox.Size = new Size(96, 22);          this.QuantityBox.Text = "";          //          // QuantityLabel          //          this.QuantityLabel.Font = new Font("Tahoma", 9F, FontStyle.Regular);          this.QuantityLabel.Location = new Point(8, 96);          this.QuantityLabel.Size = new Size(80, 16);          this.QuantityLabel.Text = "Quantity";          //          // Form1          //          this.Controls.Add(this.ClearButton);          this.Controls.Add(this.SaveButton);          this.Controls.Add(this.tabControl1);          this.Menu = this.mainMenu1;          this.Text = "Form1";       }       static void Main()       {          Application.Run(new Form1());       }       private void SaveButton_Click(object sender, EventArgs e)       {          StringWriter sw = new StringWriter();          XmlTextWriter xw = new XmlTextWriter( sw);          xw.WriteStartDocument();          xw.WriteStartElement("Order");          xw.WriteStartElement("Company");           xw.WriteStartElement("Name");           xw.WriteString(CustomerBox.Text);           xw.WriteEndElement();           xw.WriteStartElement("BillTo");           xw.WriteString(BillToCodeBox.Text);           xw.WriteEndElement();           xw.WriteStartElement("ShipTo");           xw.WriteString(ShipToCodeBox.Text);           xw.WriteEndElement();          xw.WriteEndElement();          xw.WriteStartElement("Product");           xw.WriteStartElement("Product");           xw.WriteString(ProductBox.Text);           xw.WriteEndElement();           xw.WriteStartElement("Size");           xw.WriteString(SizeBox.Text);           xw.WriteEndElement();           xw.WriteStartElement("Quantity");           xw.WriteString(QuantityBox.Text);           xw.WriteEndElement();          xw.WriteEndElement();          xw.WriteEndElement();          xw.WriteEndDocument();          XMLBox.Text = sw.ToString();       }       private void ClearButton_Click(object sender, EventArgs e)       {          CustomerBox.Text = "";          BillToCodeBox.Text = "";          ShipToCodeBox.Text = "";          ProductBox.Text = "";          SizeBox.Text = "";          QuantityBox.Text = "";       }    } } 
Figure 10.3. Order entry smart device application.

On the other hand, if the technology environment utilizes Microsoft SQL Server technology, database replication can be used to further simplify the architecture. SQL Server CE (http://www.microsoft.com/sql/ce) provides a compact database environment for mobile devices. For instance, the products and customer databases can be replicated on the device (not the entire database, of course, but only the current data for a particular sales representative, for example). DataGrid and other device controls can then be used to bind data with the embedded SQL Server, and orders can be stored on the device database. When the device is connected again, the order information can be replicated back to the corporate database.

A key capability included with the .NET CF subset functions is to connect with Web services. So if you are deploying your application in a connected environment (for instance within an office building using the 802.11b network), you can connect to enterprise databases and back-end applications using Web services from your device.

Developing Smartphone Applications

Devices developed for the Smartphone platform provide key benefits of smart device development for a new generation of mobile phones. To develop a Smartphone application similar to the one shown in Listing 10.3, open the Smart Device Application Project Wizard in Visual Studio .NET, but this time select Smartphone as the target platform. A blank form that looks much smaller than the one for Pocket PC will be created by the wizard. You can drag and drop controls and attach event handlers to this form. Notice that the Device Controls palette has some controls grayed out because they are not available for the Smartphone platform. This includes Button, RadioButton, ListBox, TabControl, and ToolBar. For instance, in the Smartphone platform if you would like to attach action-based event handlers, you would do that to the bottom-left menu bar instead of buttons. Buttons don't make sense on a Smartphone because no screen-sensitive stylus is available on the device; menus provide a much easier navigation using the joystick or thumb-based navigation controls available on a Smartphone. Figure 10.4 shows an example.

Listing 10.3 Hello World Smartphone Application
 using System; using Drawing; using Collections; using Windows.Forms; using Data; namespace SmartphoneApplication {    public class Form1 : Form    {       private Label label1;       private MenuItem ExitMenuItem;       private MainMenu mainMenu1;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.mainMenu1 = new MainMenu();          this.ExitMenuItem = new MenuItem();          this.label1 = new Label();          //          // mainMenu1          //          this.mainMenu1.MenuItems.Add(this.ExitMenuItem);          //          // ExitMenuItem          //          this.ExitMenuItem.Text = "Exit";          this.ExitMenuItem.Click += new EventHandler(this.ExitMenuItem_Click);          //          // label1          //          this.label1.Location = new Point(32, 24);          this.label1.Size = new Size(120, 48);          this.label1.Text = "Hello Smartphone";          this.label1.TextAlign = ContentAlignment.TopCenter;          //          // Form1          //          this.Controls.Add(this.label1);          this.Menu = this.mainMenu1;       }       static void Main()       {          Application.Run(new Form1());       }       private void ExitMenuItem_Click(object sender, EventArgs e)       {          Application.Exit();       }    } } 
Figure 10.4. Smartphone 2003 application.

SHOP TALK : REAL WORLD APPLICATIONS OF SMART DEVICE APPLICATIONS

One key area where mobile applications can be used effectively is in enterprises where a mobile or field sales process is involved. Using these devices, workers can work in a much more productive, efficient, and paper-free mechanism, whether they are taking orders from customers, delivering packages, writing prescriptions, diagnosing patients , or the like. With the available Smart Media extended memory, doctors can practically carry the entire PDR ( Physician's Desk Reference ) with them for easy lookup and then prescribe medicines to patients by clicking the name of the medicine. In my experience, a number of paper-based processes can be converted into error-free, paperless, device-based processes. Smart devices can be used in scenarios where taking a desktop or laptop computer is not feasible ”for instance, in assembling and repairing a large machine such as an airplane. Smart device applications are also used in embedded computing scenarios such as cars .




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net