RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Programming Windows Mobile 5.0 Applications Using the .NET Compact Framework : Page 5

Visual Studio 2005 is the premier development platform for Windows Mobile 5.0. This article explores its new capabilities in detail.

Monitoring Changes in System State
Windows Mobile 5.0 contains the SystemState (found in the Microsoft.WindowsMobile.Status assembly) class that provides the ability to get the current value of a system state as well as the ability to be notified when that state changes. For example, you might want to synchronize your Pocket PC with ActiveSync when the user connects the Pocket PC to the cradle (or synching cable). As such, your application needs to monitor if there is a change in the cradle state.

Figure 13 shows an application that displays the cradle state of the device as well as the IP address of itself and the host. Using such an application, you can write an application that synchronizes the content of the device with the desktop.

Figure 13: Monitoring changes in system states.
Let's look at the source code of the application. First, import the necessary namespaces. (This example requires you to add a reference to the Microsoft.WindowsMobile, Microsoft.WindowsCE.Forms, and Microsoft.WindowsMobile.Status assemblies.)

   Imports Microsoft.WindowsMobile
   Imports Microsoft.WindowsMobile.Status
   Imports System.Net
   Imports System.Text
Declare two SystemState variables—one to keep track of the cradle state, and one to keep track of the network connection state. Next, the code creates an instance of the Notification class to display notifications to the user.

   Private WithEvents cradleState As SystemState
   Private WithEvents networkConnectionState As SystemState
   Private WithEvents notification1 As New Microsoft.WindowsCE. _
Declare the DisplayGetOwnIPAddresses() subroutine to display the IP address(es) assigned to the Pocket PC.

   '---display own IP address
   Private Sub DisplayGetOwnIPAddresses()
      lblIPAddresses.Text = String.Empty
         Dim ownAddr() As IPAddress = Dns.GetHostEntry( _
         If ownAddr Is Nothing Then
            Exit Sub
         End If
         For i As Integer = 0 To _
            ownAddr.Length - 1
            lblIPAddresses.Text &= ownAddr(i).ToString & vbCrLf
      Catch ex As Exception
      End Try
   End Sub
Declare the DisplayHostIPAddresses() subroutine to display the IP address(es) of the host computer:

   '---display host IP address
   Private Sub DisplayHostIPAddresses()
      lblHostIPAddress.Text = String.Empty
         Dim hostAddr() As IPAddress = Dns.GetHostEntry( _
         If hostAddr Is Nothing Then
            Exit Sub
         End If
         For i As Integer = 0 To _
            hostAddr.Length - 1
            lblHostIPAddress.Text = hostAddr(i).ToString
      Catch ex As Exception
      End Try
   End Sub
When the form loads, you instantiate the cradleState and networkConnectionState objects (see Listing 2) so that any changes in the cradle state or network connection state will trigger the necessary events (you will service them next).

When the IP address of the device changes (such as when it is connected to a Wi-Fi network and assigned a new IP address), the Changed event of the networkConnectionState object will be fired. The new IP address of the device and the host PC will be updated.

   '---event handler for handling changes
   ' in network connection state
   Private Sub networkConnectionState_Changed( _
      ByVal sender As Object, _
      ByVal args As Microsoft.WindowsMobile.Status.ChangeEventArgs) _
      Handles networkConnectionState.Changed
      '---display own and host IP addresses
   End Sub
Likewise, when the cradle's state changes (when you connect or disconnect your Pocket PC from the host computer), the Changed event of the cradleState object will fire (see Listing 3). Here you will display a notification balloon to the user if the device is cradled to the computer. Notice that the content of the notification is coded in HTML. To dismiss a notification, you use the identifier cmd:2 as the name for an input element. This identifier has special meaning in Windows CE and is used to dismiss notifications. The content of the notification allows the user to select the update frequency through a drop-down list box.

The code uses the DisplayNotification() subroutine to display a notification balloon on the Pocket PC.

   '---display the notification
   Private Sub DisplayNotification( _
      ByVal caption As String, _
      ByVal text As String)
      With notification1
         .Caption = caption
         .Text = text
         .InitialDuration = 20
         .Visible = True
      End With
   End Sub
When the user submits the information in a notification balloon, it fires the ResponseSubmitted event via the notification1 object variable. Here you'll add the logic to do whatever you are supposed to do, such as communicate with the host application to synchronize the content on the Pocket PC with the desktop.

   Private Sub OnResponseSubmitted( _
      ByVal sender As Object, _
      ByVal args As Microsoft.WindowsCE.Forms. _
         ResponseSubmittedEventArgs) _
      Handles notification1.ResponseSubmitted
   '---A sample reply---
   ' notify?lstIntervals=2
      If (args.Response. Substring(0, 6) = "notify") Then
         Dim choice As Integer = Convert.ToInt32( _
            args.Response.Substring(20, 1))
         Select Case choice
            Case 0 '---do something
            Case 1 '---do something
            Case 2 '---do something
         End Select
      End If
      notification1.Visible = False
   End Sub
In this article, I've shown you some of the new features available on the Windows Mobile 5.0 platform. With Visual Studio 2005, developers using the .NET Compact Framework can now develop compelling applications with ease, as most of the important APIs are exposed as managed classes. If you have not started on mobile application development yet, now is a good time to get started!

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date