How to: Share Source Code Across Platforms (Devices)

You can share source code across platforms by using compiler constants to differentiate those sections of code that depend on the target. Allowable constants are PocketPC, Smartphone, and WindowsCE. The platforms must target the same version of the .NET Compact Framework.

The following steps provide a simple example of the technique. You create a Visual Basic Pocket PC application, add compiler directives, run the application, close it, and change to a Smartphone application. Then you run the Smartphone application to see that the title bar text is changed.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create and run the Pocket PC version

  1. On the Visual Studio File menu, point to New, and then click Project.

  2. In the Project types pane, expand Visual Basic, expand Smart Device, and then click Pocket PC 2003.

  3. In the Templates pane, click Device Application (1.0), and then click OK.

    The appended (1.0) indicates that this is a .NET Compact Framework version 1.0 project.

  4. In the designer, right-click the form, and then on the shortcut menu, click Properties.

  5. Clear the Text property value of the form—that is, make it blank.

  6. In Solution Explorer, right-click Form1.vb, and then on the shortcut menu, click View Code.

  7. Expand the Windows Form Designer generate code region.

  8. After InitializeComponent() in Public Sub New(), insert the following code:

    #If PocketPC Then
       Me.Text = "PPC2003"
    #Else
       Me.Text = "Smartphone"
    #Endif
    
  9. On the Debug menu, click Start Debugging.

  10. In the Deploy <Projectname> dialog box, click Pocket PC 2003 SE Emulator, and then click Deploy.

    The Pocket PC application runs in the emulator with PPC2003 in the title bar of the form.

To create and run the Smartphone version

  1. Close the emulator without saving state.

    If a message appears indicating that the connection has been lost, click OK.

  2. On the Project menu, click Change Target Platform.

  3. In the Change to box in the Change Target Platform dialog box, select Smartphone2003, and then click OK.

  4. In the message box advising that the project will be closed and re-opened, click Yes.

    Note that the Target Device box on the toolbar now displays Smartphone 2003 SE Emulator.

  5. On the Debug menu, click Start Debugging.

  6. In the Deploy <Projectname> dialog box, click Smartphone 2003 SE Emulator, and then click Deploy.

    The Smartphone application runs in the emulator with Smartphone in the title bar of the form.

See Also

Tasks

How to: Change Platforms in Device Projects

Concepts

Creating and Developing Managed Device Projects