Writing an Application for SmartPhoneXMLDataSetViewer


We'll use a tutorial approach to build a simple XML DataSet viewer. The XML DataSet viewer shows the first table in a DataSet . The tutorial provides developers with an existing SmartPhone project to experiment with. It also demonstrates how the .NET Compact Framework development experience is nearly unchanged when working with SmartPhone.

MANAGING FILES ON THE SMARTPHONE

Managing files on the SmartPhone OS can be cumbersome because there is no touch screen or File Explorer program. With physical SmartPhone devices, you at least have the option of accessing the SmartPhone's file system via ActiveSync. But this problem gets especially annoying when working with the emulator, because by default, you cannot access the emulator's file system through ActiveSync. Keep a lookout at http://www.gotdotnet.com in case Microsoft releases any utilities to work around this limitation.


Before starting the tutorial, we assume that the SmartPhone add-on has been installed. The steps that follow demonstrate how to create the XMLDataSetViewer utility from scratch. Finished versions of the XMLDataSetViewer are available in the directory \SampleApplications\Chapter17 . There are C# and Visual Basic versions of the XMLDataSetViewer.

Building the DataSetViewer from ScratchTutorial Steps

  1. Launch Visual Studio .NET and create a new project. You can choose a Smart Device Application project for either C# or Visual Basic.

  2. Follow the same steps as you normally would for creating a new Smart Device Application, except when asked to choose a platform to target, choose SmartPhone instead of Pocket PC or Windows CE. This step is shown in Figure 17.1.

    Figure 17.1. When creating a new project, choose the SmartPhone platform.

    graphics/17fig01.jpg

  3. When you have finished setting up the new application, you will see the form editor and the Toolbox, as shown in Figure 17.2. This screen looks almost identical to that for a Pocket PC project, except that the form is smaller to reflect the smaller screen size on SmartPhones. Also, some of the controls in the Toolbox are ghosted out.

    Figure 17.2. The form editor and Toolbox for a SmartPhone project contain some ghosted-out items.

    graphics/17fig02.jpg

  4. Drag a DataGrid and a TextBox onto the control. Name the DataGrid dgDataSet and the TextBox txtXmlToLoad . For convenience, use this text as the default for the TextBox: \Storage\Program Files\XMLDataSetViewer_CS\SampleDataSet.xml .

  5. Add a reference to the DataGrid to your project. To do this, right-click the solution name (for example, XmlDataSetView_CS ) in the Solution Explorer. Then, select Add Reference. You will see a dialog window in which you can choose from a variety of DLLs. Choose the Browse button and move to the directory in which you installed Visual Studio. This is typically C:\Program Files\Microsoft Visual Studio .NET 2003 . In this directory select the file CompactFrameworkSDK\v1.0.5000\Windows CE\ System.Windows.Forms.DataGrid.dll .

  6. Add menu items by clicking the MainMenu1 icon that appears below the form editor in the IDE. You can add menu items by clicking new menu slots and typing the text for the menu item. For this project we chose two items: Exit and Load XML .

  7. Add code to the Exit menu item by double-clicking it. The IDE brings up the method that gets called when the Exit menu item is clicked. Add this code:

     
     C# Application.Exit(); VB Application.Exit() 
  8. Add a declaration for a DataSet , called m_DataSet , at the top of the Form1 class. For example, the class member declarations for the project would look like this:

     
     C# public class Form1 : System.Windows.Forms.Form {    private System.Windows.Forms.DataGrid dgDataSet;    private System.Windows.Forms.MenuItem menuItem1;    private System.Windows.Forms.MenuItem menuItem2;    private System.Windows.Forms.TextBox txtXmlToLoad;    private System.Windows.Forms.MainMenu mainMenu1;    private DataSet m_DataSet; // Rest of class Form1 not shown here... VB Public Class Form1    Inherits System.Windows.Forms.Form    Friend WithEvents dgDataSet As System.Windows.Forms.DataGrid    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu    Private m_DataSet As DataSet    'Rest of class Form1 not shown here... 
  9. Add this code to the Load XML menu item by double-clicking the menu item and inserting the code in the code editor:

     
     C# if (this.m_DataSet == null) {    this.m_DataSet = new DataSet(); } this.m_DataSet.Clear(); try {    m_DataSet.ReadXml(this.txtXmlToLoad.Text);    // Set up a DataView    DataView l_DataView = new DataView(m_DataSet.Tables[0]);    this.dgDataSet.DataSource = l_DataView; } catch (Exception ex) {    MessageBox.Show(ex.ToString()); } VB If (Me.m_DataSet Is Nothing) Then    Me.m_DataSet = New DataSet    End If Me.m_DataSet.Clear() Try    m_DataSet.ReadXml(Me.txtXmlToLoad.Text)    ' Set up a DataView    Dim l_DataView As DataView = New DataView(m_DataSet.Tables(0))    Me.dgDataSet.DataSource = l_DataView Catch ex As Exception    MessageBox.Show(ex.ToString()) End Try 
  10. Add the default XML file, SampleDataSet.xml , to your application. To do this, view the Solution Explorer by pressing Ctrl+Alt+L and hover the mouse over the solution name (for example, XMLDataSetViewer_CS ). Right-click and select Add , Add Existing Item , and then select the file called SampleDataSet.xml . You can find this file included with the completed XMLDataSetViewer project, either the C# or Visual Basic version.

  11. Build and deploy your application! If you don't have any SmartPhone hardware, deploy to the emulator with the Virtual Radio.

Using the XMLDataSetViewer

Using SmartPhone applications is different from using Pocket PC applications because there is no touch screen. Instead, there are two menu buttons , a directional button, and a keypad for navigating an application.

To quit the application, click the left menu button, which triggers the Exit menu item.

To select the XML file to load in this simple application, you must insert the full text path of the XML file to load into the textbox. To do this, first make the textbox the active control in the application. Once the textbox is the active control, you can move the cursor with the cursor keys and insert text by using the numeric keypad.

To load the selected XML file, click the right menu button, which triggers the Load XML menu item.

The DataGrid is very small on a SmartPhone screen. To navigate through the data, first select the DataGrid as the active control by pressing the down button on the directional keypad. Once the DataGrid is the active control, you can scroll up and down by pressing the up and down buttons on the directional keypad. If the data table being displayed is too wide to fit in the DataGrid, you can navigate left and right by using the left and right buttons on the directional keypad.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

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