To start a mobile project, follow the same procedure as when you are developing standard desktop application. In Visual Studio, create a new project, select the language, and then select the Smart Device project template. Also select other project properties, such as name and location.
The next step is specific to Windows Mobile solutions. You have to define the target platform and version of the .NET Compact Framework. As mentioned earlier, in Visual Studio 2005, you can target .NET CF 1.0 and 2.0, and in Visual Studio 2008, you can target .NET CF 2.0 and 3.5. As a target platform, you can select any platform available on the developer workstation: Windows Mobile 5.0 or 6, or Windows CE, or even Pocket PC 2003.
.png)
Now you have a regular Visual Studio solution with a basic application skeleton. Also notice that Form1 is enclosed in the device skin. It simulates a real application environment. The skin can be disabled in Visual Studio IDE on the form itself. Visual Studio IDE looks familiar, with all standard elements, such as the toolbar, Solution Explorer, Properties, Form Designer, Code Editor, and more. There is also a toolbar specific to Windows Mobile solutions, where you can select the target device and emulator, connect to the device and emulator, set various device options, and change rotation in Form Designer from Portrait to Landscape or vice versa.
.NET Compact Framework is a subset of the .NET Framework and so is the set of supported controls and their properties. Core development and user interface design is the same as Windows desktop application development and is not covered by this article.
Creating the User Interface
When you design the user interface for mobile devices, you have to be aware of the limitations of the small form factor, and design the user interface accordingly. It is never good practice to copy the user interface from a desktop application to a mobile device. One of the approaches that is very common in desktop applications, scrolling, is bad practice on mobile devices. It is very user unfriendly to scroll up and down on a mobile device and should be avoided when it is possible. Instead, use different approaches for that, such as Tab controls, panels, or similar approaches.
Menus on Windows Mobile Devices are located on the bottom of the screen. They are created in the same manner as menus in desktop applications. However, it is good practice not to nest them more than three levels down, because they become unreadable after that. Mobile forms allow for two menu items that correspond to a left and a right soft key, Soft Key 1 and Soft Key 2. They can have nested menus as necessary. It is common practice to put the default action on the left soft key, and all actions and nested menus on the right soft key.
Windows Mobile 6 devices support orientation change. Therefore, applications that are written for a certain orientation become user unfriendly when the orientation is changed. Automatic scroll bars are supported, but as mentioned earlier, scrolling should be avoided when possible. Using docking and anchoring (supported from .NET CF 2.0) provides a solution in most cases and should be used when applicable.
Controls available in .NET CF are all standard controls (text box, label, common dialog boxes, tab, grid, and more) plus many advanced controls (Web browser) from the .NET Framework.
In Windows Mobile forms, the X in the upper-right corner means minimize the window, instead of closing it. To close it, you have to set the MinimizeBox property of the form to False. Then ok appears that means close the window.
Handling Input
Compared to standard desktop input methods, keyboard and mouse, mobile devices handle input somewhat differently. Keyboard became a standard option in many mobile devices so that users can use it as an input device for mobile devices, but more common options are Taps, Software Input Panel (SIP), and Hardware keys.
Tap is to a mobile device, what the left mouse double-click is to the desktop. It is a common input method on touch-screen devices. Tap-and-hold corresponds to right-click. It opens the shortcut menu for selected items. When tap is used on a control in a Windows form, it starts all corresponding events for that control: MouseDown, MouseUp, MouseMove, Click, DoubleClick. From a programmatic perspective, it is the equivalent to a mouse click, and you can handle all these events in your application just as you handle them in desktop applications.
The soft input panel lets users accept keyboard input without a physical keyboard. SIP in combination with Tap provides a real keyboarding experience for users, invoking events associated with pressing keys on the keyboard: KeyUp, KeyDown, and KeyPress.
The Input panel is a .NET CF control, and when you add it to the form, it can be programmatically controlled from the code. You can see it when the text box control receives the focus:
inputPanel1.Enabled = true;
and close it when the control loses the focus:
inputPanel1.Enabled = false;
Error Handling and Debugging
Error handling in .NET CF is performed exactly like in the .NET Framework, by using a try….catch….finally structure. Microsoft Visual Basic also supports On Error GoTo and Resume [Next] for error handling, but try….catch… finally blocks are preferred because they are less resource intensive. Standard managed debugging is also supported in .NET CF in similar ways as in the .NET Framework. Deploying and debugging applications is supported on both devices and emulators.
Starting with .NET Compact Framework 3.5, unit testing is also supported for Windows Mobile projects.
Working with Data
.NET CF supports working with various data sources. The concept is the same as the .NET Framework and most important, ADO.NET is supported in most of its functionality. Windows Mobile applications can use SQL Server CE, XML, SQL Server, Web services, or WCF service as data sources.
XML is a common format that most data sources can export data to, and import data from. Even though it can be used by most data sources, we do not recommend it as a data store, because it has a large overhead, it is not optimized, and is much slower than a relational database, because it has no indexes. However, if there is no other option, XML can be used as a data store on the device.
SQL Server can directly be accessed from devices as a data store. It is a great concept if there is no need for an offline solution. If you can guarantee a constant connection, use it. It will keep data on the server up-to-date in real time, and reduce possibility of conflicts. Warehouses are a good example of an environment where this approach is acceptable, even desirable.
Web Services and WCF service are a good choice for transferring data in the n-tier environment when you do not have direct access to SQL Server (either for direct data access or for replication). In this scenario, the data source can be anything, and the local data store can be anything acceptable to .NET CF.
SQL Server Compact Edition
SQL Server CE is the best and most optimized data store in mobile solutions. It is only a data store (only has tables, nothing else). However, it has everything related to tables, indexes, primary and foreign keys, referential integrity, and constraints. It does not have triggers, because no programmability is supported in SQL Server CE. In addition to being RDBM, SQL Server CE has another big advantage. It can be used as a replication subscriber in SQL replication that makes it a perfect companion to SQL Server. It is very easy to control replication programmatically, and all conflicts are handled centrally by the administrator.
To add a new data source to a Windows Mobile project, follow the same procedure as with the .NET Framework, by using the same wizard. You have to select Microsoft SQL Server Compact Edition 3.x as a data source and also select a database. SQL Server CE databases have the .sdf extension. Select a database from a local disk, or from the device that is connected through ActiveSync or WMDC.
Because you are using SQL Server CE as the local data store, during deployment the database is copied to the device. Therefore, the connection string that was created when you selected a database in the previous step will not be valid on the device. The wizard will ask whether you want to change the connection string so that it always points to the application folder on the device.
Data Binding
As soon as the database is added to the project, you can use standard ADO.NET to work with it. You can bind controls to the data source by using the Data Binding object or binding directly to the data source. To work with data on the device, you can use standard datasets. The problem with a dataset on a mobile device is that you are creating a copy of data in-memory that resides on the device anyway. That approach is causing unnecessary resource usage and performance overhead. To avoid it, use the object that is specific to the SQL Server CE data adapter, SqlCeResultset. It is an optimized, updatable, and scrollable cursor which accesses SQL Server CE data directly from the application, without any need for a dataset. We recommend it for working with SQL Server CE data on the device.
To work with SQL Server CE data on the device, you have to add a reference to the System.Data.SqlServerCe namespace in the project.
The following code example shows how to define the reference in the code, and to create and use SqlCeResultset.
using System.Data.SqlServerCe;
...
SqlCeResultSet rs;
SqlCeCommand cmd = new SqlCeCommand(command text, sql ce connection);
rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
control.DataSource = rs;