You are previewing Programming .NET Compact Framework 3.5 Second Edition.
O'Reilly logo
Programming .NET Compact Framework 3.5 Second Edition

Book Description

"If you're interested in developing for this burgeoning platform, there is no one better able to get you up-to-speed.”

–From the Foreword by Rob Tiffany, mobility architect, Microsoft

Completely revised and updated for .NET Compact Framework 2.0 and 3.5, Visual Studio 2008, and Windows Mobile Smart Phones, Programming .NET Compact Framework 3.5, Second Edition, teaches you how to write highly effective applications for handheld wireless devices with small screens, limited memory, and finite battery life.

This book is the definitive tutorial and reference for the .NET Compact Framework and Windows Mobile. If you’re interested in developing for this burgeoning platform, there is no one better able to get you up to speed than industry veterans, master programmers, and teachers Paul Yao and David Durant.

With this book you will learn how to

  • Use each of the ten API sets available for Windows Mobile; when to use each; and, especially, when to use Win32 and the .NET Compact Framework 2.0 and 3.5

  • Write programs that make the most of a Windows Mobile device’s limited battery life

  • Efficiently invoke Win32 APIs from the .NET Compact Framework

  • Write exceptional, data-driven applications using data binding and .NET controls

  • Manage device data with the object store, file I/O, and the registry

  • Work with databases using ADO.NET and LINQ

  • Synchronize mobile data with remote databases and the remote API

  • Make a mobile device work with the Windows Communication Foundation (WCF)

  • Create graphical output on Windows Mobile devices 

  • If you want to learn Windows Mobile development but only have limited experience with the .NET Framework, this is the only book you need.

    Table of Contents

    1. Copyright
      1. Dedication
    2. Praise for Programming .NET Compact Framework 3.5, Second Edition
    3. Figures
    4. Tables
    5. Foreword
    6. Preface
      1. What You Need to Use This Book
        1. Hardware
        2. Software
      2. .NET Compact Framework Versions
      3. The Target Audience for This Book
    7. Acknowledgments
    8. About the Authors
    9. 1. Mobile Phone Programming
      1. 1.1. Selecting an Application Programming Interface
        1. 1.1.1. The Win32 API
          1. 1.1.1.1. Win32 Strengths
          2. 1.1.1.2. Win32 Weaknesses
        2. 1.1.2. The .NET Compact Framework
          1. 1.1.2.1. Compact Framework Strengths
          2. 1.1.2.2. Compact Framework Weaknesses
        3. 1.1.3. The Web Browser
        4. 1.1.4. Rich Internet Applications (RIAs)
      2. 1.2. Memory Management
        1. 1.2.1. Metadata Maps
        2. 1.2.2. The JITted Code Pool
        3. 1.2.3. Garbage Collector Pools
        4. 1.2.4. Garbage Collection and Data
        5. 1.2.5. Automatic Garbage Collection
          1. 1.2.5.1. Phase 1: Mark and Sweep
          2. 1.2.5.2. Phase 2: Compact the Heap
          3. 1.2.5.3. Phase 3: Flush the JITted Code Pool
        6. 1.2.6. Special Handling of Managed Data
          1. 1.2.6.1. Calling the Garbage Collector
          2. 1.2.6.2. Manual Cleanup Using the Dispose and Finalize Methods
          3. 1.2.6.3. Finalize: The Last Bastion of Defense
          4. 1.2.6.4. Weak References
        7. 1.2.7. Manual Memory Management of Native Data
      3. 1.3. Conclusion
    10. 2. Extending Battery Life
      1. 2.1. What Is the Problem?
        1. 2.1.1. Basic Questions to Ask
        2. 2.1.2. Factors Influencing Battery Life
      2. 2.2. Measuring Battery Usage
        1. 2.2.1. Talk Time and Standby Time
        2. 2.2.2. Power Measurement Method 1: Software-Only Approach
        3. 2.2.3. Power Measurement Method 2: Dedicated Hardware
      3. 2.3. Device Power Study
        1. 2.3.1. Standby Power Usage
        2. 2.3.2. Backlight Power Usage
        3. 2.3.3. Power Usage for Communicating
        4. 2.3.4. Multimedia Power Usage
      4. 2.4. Conclusion
    11. 3. Platform Invoke
      1. 3.1. Overview of P/Invoke
        1. 3.1.1. When to Use P/Invoke
        2. 3.1.2. Why We Prefer .NET Compact Framework Classes over Win32 Functions
        3. 3.1.3. Porting Native Code to Managed Code
        4. 3.1.4. Component Object Model (COM) Support
      2. 3.2. Creating P/Invoke Declarations
        1. 3.2.1. A Simple Function: MessageBox
        2. 3.2.2. Native Function Details
        3. 3.2.3. Function Return Values
        4. 3.2.4. Getting Started: C-Style Function Declarations
      3. 3.3. Supported P/Invoke Function Parameters
        1. 3.3.1. The Limits of Parameter Passing
        2. 3.3.2. Simple Data Types
          1. 3.3.2.1. Boolean
          2. 3.3.2.2. Signed versus Unsigned Integers
          3. 3.3.2.3. The C# long Type
        3. 3.3.3. Passing Parameters by Value versus by Reference
        4. 3.3.4. Passing String Parameters by Value
        5. 3.3.5. Structures
          1. 3.3.5.1. Example: MemoryStatus
        6. 3.3.6. Type of Types versus Type of Parameters
          1. 3.3.6.1. Value Types as Parameters
          2. 3.3.6.2. Reference Types as Parameters
        7. 3.3.7. Arrays
          1. 3.3.7.1. Example: Passing an Array of Integers
      4. 3.4. A Sample Program: CallWin32
      5. 3.5. Writing Win32 Dynamic Link Libraries
        1. 3.5.1. Declaring C++ Functions in a DLL
      6. 3.6. Manual P/Invoke Parameter Passing
        1. 3.6.1. The Marshal Class
        2. 3.6.2. Copying to Native Memory
        3. 3.6.3. Fine-Tuning Structures with the MarshalAs Attribute
          1. 3.6.3.1. Example: FindMemoryCard
      7. 3.7. Communicating between Native and Managed Code
        1. 3.7.1. The MessageWindow Class
        2. 3.7.2. Other Ways to Communicate between Native and Managed Code
      8. 3.8. Comparing P/Invoke Support
        1. 3.8.1. Windows CE–Specific Differences
        2. 3.8.2. .NET Compact Framework Implementation Details
      9. 3.9. Conclusion
    12. 4. Data Binding to Controls
      1. 4.1. Data Binding
        1. 4.1.1. Data-Bindable Controls
        2. 4.1.2. Data-Bindable Objects
      2. 4.2. Complex Data Binding
        1. 4.2.1. Using Complex Data Binding with ComboBox Controls
      3. 4.3. Simple Data Binding
        1. 4.3.1. The BindingsCollection Property
        2. 4.3.2. Formatting and Parsing
      4. 4.4. The DataGrid Control
        1. 4.4.1. Using Complex Data Binding with the DataGrid Control
        2. 4.4.2. Styling the Display of Data in a DataGrid Control
        3. 4.4.3. Creating Table and Column Styles
        4. 4.4.4. Creating Styles at Runtime
        5. 4.4.5. Responding to User Input
        6. 4.4.6. Using Simple Data Binding with the DataGrid Control
        7. 4.4.7. Accessing DataGrid Information
        8. 4.4.8. Providing Drill-Down Capability
        9. 4.4.9. Providing In-Place Editing Capability
        10. 4.4.10. Providing Automated In-Place Editing Capability
      5. 4.5. Conclusion
    13. 5. Storage
      1. 5.1. Smart-Device Data Storage
        1. 5.1.1. Installable File Systems
        2. 5.1.2. The Windows CE File System
        3. 5.1.3. ROM-Based Files
      2. 5.2. File I/O
        1. 5.2.1. The File and Directory Classes
        2. 5.2.2. Byte-Level I/O
        3. 5.2.3. Higher-Level I/O
        4. 5.2.4. Encoding and Decoding Data
        5. 5.2.5. Using the I/O Classes
        6. 5.2.6. Text File I/O
        7. 5.2.7. Binary File I/O
        8. 5.2.8. Writing Binary Data
        9. 5.2.9. Reading Binary Data
        10. 5.2.10. Binary I/O and Structures
        11. 5.2.11. XML File I/O
        12. 5.2.12. Easier XML Serialization
          1. 5.2.12.1. Binary Formatting and XML Serialization
          2. 5.2.12.2. XML Serialization
      3. 5.3. Registry Access
        1. 5.3.1. Opening and Creating Registry Keys
        2. 5.3.2. Reading and Writing Registry Values
        3. 5.3.3. Updating the Storage Sample Application to Use the Registry
      4. 5.4. Conclusion
    14. 6. ADO.NET Programming
      1. 6.1. Examining ADO.NET
        1. 6.1.1. A Layered Approach
        2. 6.1.2. The ADO.NET Classes
          1. 6.1.2.1. Namespace References and Assembly References
          2. 6.1.2.2. Functionality: Supersets and Subsets
        3. 6.1.3. ADO.NET Error Handling
      2. 6.2. Working with Data Sets
        1. 6.2.1. Creating and Accessing DataSet, DataTable, and DataView Objects
          1. 6.2.1.1. Understanding Data Tables
          2. 6.2.1.2. Working with Data Rows
          3. 6.2.1.3. Introducing Data Views
        2. 6.2.2. Data Binding
          1. 6.2.2.1. Binding to Multi-Item Controls
          2. 6.2.2.2. Binding to Single-Item Controls
          3. 6.2.2.3. Designating the Row to Be Displayed
            1. Matching Binding
            2. Indexing
            3. Search Key
          4. 6.2.2.4. Assigning the Controls to a Row
          5. 6.2.2.5. Updating the Bound Data Table
        3. 6.2.3. Reading and Writing a Data Set as XML
      3. 6.3. Microsoft SQL Server CE
        1. 6.3.1. SQL Server CE Files
        2. 6.3.2. SQL Server CE Syntax
        3. 6.3.3. SQL Server CE Query Analyzer
        4. 6.3.4. Creating a SQL Server CE Database
        5. 6.3.5. Populating a SQL Server CE Database
          1. 6.3.5.1. The SqlCeConnection and SqlCeCommand Classes
        6. 6.3.6. Retrieving and Displaying Data
          1. 6.3.6.1. The SqlCeDataReader Class
        7. 6.3.7. Updating a SQL Server CE Database
        8. 6.3.8. The SqlCeDataAdapter Class
          1. 6.3.8.1. Using a SqlCeDataAdapter Object to Retrieve Data
          2. 6.3.8.2. Using a SqlCeDataAdapter Object to Update a Database
        9. 6.3.9. Querying Schema Information
      4. 6.4. Microsoft SQL Server
        1. 6.4.1. Connecting to SQL Server
        2. 6.4.2. Creating Command Objects
        3. 6.4.3. Using SQL Server Stored Procedures
        4. 6.4.4. Using Stored Procedures with DataSet Objects
        5. 6.4.5. DataSet Objects and Concurrency
        6. 6.4.6. Using Multi SELECT Stored Procedures
        7. 6.4.7. Working with Typed Data Sets
      5. 6.5. Web Services
        1. 6.5.1. XML, XSD, and SOAP
        2. 6.5.2. A Web Services Application
        3. 6.5.3. A Web Services Client Application
      6. 6.6. Conclusion
    15. 7. LINQ
      1. 7.1. Overview
        1. 7.1.1. Set Classes in .NET
        2. 7.1.2. LINQ in the Compact Framework
        3. 7.1.3. Deferred Execution
      2. 7.2. The Sample Application
        1. 7.2.1. LINQ to Datasets
        2. 7.2.2. LINQ to Objects
          1. 7.2.2.1. Business Objects
            1. Support for Disconnected Operations
            2. One Row, One Object
          2. 7.2.2.2. The Business Objects
        3. 7.2.3. Business Object Properties
          1. 7.2.3.1. Read-Write Properties
          2. 7.2.3.2. Read-Only Properties
        4. 7.2.4. The Hybrid Version
      3. 7.3. LINQ to XML
      4. 7.4. Conclusion
    16. 8. Synchronizing Mobile Data
      1. 8.1. Understanding SQL Server CE Synchronization
        1. 8.1.1. Three Synchronization Mechanisms
        2. 8.1.2. IIS Connectivity
        3. 8.1.3. Database Connectivity
      2. 8.2. Installing Remote Data Connectivity
        1. 8.2.1. Creating the Virtual Directory
        2. 8.2.2. Configuring Additional Components
      3. 8.3. Using RDA
        1. 8.3.1. RDA Capabilities and Overhead
        2. 8.3.2. Programming for RDA
          1. 8.3.2.1. Pulling Data
          2. 8.3.2.2. Fixing the IDENTITY Property
          3. 8.3.2.3. Viewing the Pulled Schema
          4. 8.3.2.4. Modifying Pulled Data Locally
          5. 8.3.2.5. Pushing Data
          6. 8.3.2.6. The SubmitSQL Method
      4. 8.4. Using Merge Replication
        1. 8.4.1. Using Good Design to Avoid Synchronization Failures
        2. 8.4.2. Configuring Merge Replication
        3. 8.4.3. Programming for Merge Replication
          1. 8.4.3.1. Subscribing to a Publication
          2. 8.4.3.2. Synchronizing the Data
          3. 8.4.3.3. Modifying the Data at the Device
      5. 8.5. Choosing between Merge Replication and RDA
      6. 8.6. Using Data Synchronization Services
        1. 8.6.1. Understanding Data Synchronization Service Requirements
        2. 8.6.2. Building a Data Synchronization Service
          1. 8.6.2.1. Beginning Development of Both Projects
          2. 8.6.2.2. Completing the Service
          3. 8.6.2.3. Completing the Client
          4. 8.6.2.4. Handling Concurrency Errors
          5. 8.6.2.5. Some Closing Notes on Data Synchronization Services
      7. 8.7. Conclusion
    17. 9. The Remote API
      1. 9.1. RAPI Fundamentals
        1. 9.1.1. Available RAPI Functions
        2. 9.1.2. Building .NET ActiveSync Applications
        3. 9.1.3. RAPI Startup and Shutdown
          1. 9.1.3.1. CeRapiInit versus CeRapiInitEx
          2. 9.1.3.2. Shutdown with CeRapiUninit
          3. 9.1.3.3. Two Approaches to Startup
            1. Approach 1: Simple, Single-Threaded RAPI Startup
            2. Approach 2: Multithreaded RAPI Startup
          4. 9.1.3.4. Defensive Coding
      2. 9.2. Accessing the Object Store
        1. 9.2.1. Using RAPI to Access Device Files
          1. 9.2.1.1. FindPrograms: Using RAPI to Find Files in the Device File System
          2. 9.2.1.2. FindProgramsFaster: Speeding Up a File Search
        2. 9.2.2. Remote Access to Device Registry Entries
        3. 9.2.3. Remote Access to Device Property Databases
      3. 9.3. Detecting Changes in Device Connection State
        1. 9.3.1. The Auto-Start Approach
        2. 9.3.2. The Callback Approach
      4. 9.4. Loading Programs and DLLs
        1. 9.4.1. Running Device-Side Programs
        2. 9.4.2. Loading Device-Side DLLs
          1. 9.4.2.1. Calling a DLL Function with CeRapiInvoke
      5. 9.5. Conclusion
    18. 10. Windows Communication Foundation
      1. 10.1. What Is WCF?
        1. 10.1.1. WCF Terminology
        2. 10.1.2. WCF in the .NET Compact Framework
      2. 10.2. Creating a WCF Service
        1. 10.2.1. Generating the Code
        2. 10.2.2. Making a Windows Mobile–Compatible WCF Service
        3. 10.2.3. Setting the Host Address
        4. 10.2.4. SmartMeter: A Sample WCF Service
      3. 10.3. Creating a WCF Client in Windows Mobile
        1. 10.3.1. WCF Client Namespaces and Assemblies
        2. 10.3.2. Generating the WCF Client Proxy
        3. 10.3.3. Instantiating a WCF Client
        4. 10.3.4. Accessing the WCF Service
        5. 10.3.5. WCF Client Sample: ReadSmartMeter
      4. 10.4. Conclusion
    19. 11. Creating Graphical Output
      1. 11.1. An Introduction to .NET Compact Framework Graphics
        1. 11.1.1. Drawing Surfaces
          1. 11.1.1.1. Display Screens
          2. 11.1.1.2. Printers
          3. 11.1.1.3. Bitmaps
          4. 11.1.1.4. Metafiles
          5. 11.1.1.5. Supported Drawing Surfaces
        2. 11.1.2. Drawing Function Families
          1. 11.1.2.1. Text Output
          2. 11.1.2.2. Raster Output
          3. 11.1.2.3. Vector Output
        3. 11.1.3. .NET Compact Framework Graphics
          1. 11.1.3.1. The Role of the Graphics Class
          2. 11.1.3.2. Drawing Support for Text Output
          3. 11.1.3.3. Drawing Support for Raster Output
          4. 11.1.3.4. Drawing Support for Vector Output
      2. 11.2. Drawing on the Display Screen
        1. 11.2.1. Accessing a Graphics Object
        2. 11.2.2. Drawing in Controls
        3. 11.2.3. The Paint Event
        4. 11.2.4. Non-Paint Event Drawing
      3. 11.3. Raster Graphics
        1. 11.3.1. Specifying Colors
          1. 11.3.1.1. System Colors
          2. 11.3.1.2. Named Colors
          3. 11.3.1.3. Colors from RGB Values
        2. 11.3.2. Creating Brushes
          1. 11.3.2.1. Creating Brushes with System Colors
          2. 11.3.2.2. Creating Brushes with Named Colors
          3. 11.3.2.3. Creating Brushes with RGB Values
        3. 11.3.3. Creating Bitmaps
          1. 11.3.3.1. Bitmaps: Drawing Surface or Drawing Object?
          2. 11.3.3.2. The Bitmap Class
          3. 11.3.3.3. Creating an Empty Bitmap
          4. 11.3.3.4. Creating a Bitmap from an External File
          5. 11.3.3.5. Creating a Bitmap from a Resource
            1. Typed Resources
            2. Untyped Resources
          6. 11.3.3.6. Image File Sizes
        4. 11.3.4. Drawing Bitmaps
          1. 11.3.4.1. Drawing the Entire Bitmap at the Original Image Size
          2. 11.3.4.2. Drawing Part of a Bitmap at the Original Image Size
          3. 11.3.4.3. Drawing Part of a Bitmap with a Change to the Image Size
          4. 11.3.4.4. Drawing Part of a Bitmap with a Change to the Image Size and with Transparency
        5. 11.3.5. A Sample Program: ShowBitmap
      4. 11.4. Vector Graphics
        1. 11.4.1. Creating Pens
        2. 11.4.2. A Game: JaspersDots
      5. 11.5. Conclusion
    20. 12. Text and Fonts
      1. 12.1. Drawing Text
        1. 12.1.1. Text-Drawing Support in the .NET Compact Framework
        2. 12.1.2. The DrawString Method
        3. 12.1.3. A Sample Program: SimpleDrawString
      2. 12.2. Font Selection
        1. 12.2.1. The Font Property of Controls
        2. 12.2.2. Enumerating Fonts
        3. 12.2.3. A Sample Program: FontPicker
        4. 12.2.4. A Sample Program: RotateText
      3. 12.3. Placing Text
        1. 12.3.1. Text Size and the MeasureString Method
        2. 12.3.2. A Sample Program: MeasureString
        3. 12.3.3. Text Alignment
        4. 12.3.4. A Sample Program: TextAlign
      4. 12.4. Conclusion
    21. A. Hungarian Notation for .NET Programs
      1. A.1. Goals and Objectives
      2. A.2. Guidelines
      3. A.3. .NET Naming Guidelines
      4. A.4. Hungarian Notation
        1. A.4.1. Our Use of Hungarian Notation
        2. A.4.2. The m_ Prefix for Private Data
        3. A.4.3. Hungarian Prefixes for CTS Value Types
    22. B. Windows API Allocation and Cleanup Functions