Developing Desktop Apps for Ultrabook™ Devices in Windows 8*: Getting Started(Part 1)

By Paul Ferrill

Download Article


 Developing Desktop Apps for Ultrabook™ Devices in Windows 8*: Getting Started (PDF 1.04 MB)

The Ultrabook™ provides a rich set of sensor capabilities to enhance a wide range of applications. It also includes the possibility of a touchscreen to provide even more opportunities for enhancing the user experience. This two-part series explores what it takes to get your application up and running in the new Windows 8 operating system, and then to add enhancements to take advantage of the capabilities of Ultrabook.

First Steps for Existing Apps


If you’re using Microsoft Visual Studio* 2010 with Service Pack 1 (SP1) for development, you will be able to open your project in Microsoft Visual Studio 2012 with no conversion necessary. In fact, you should be able to “round trip” between Visual Studio 2010 with SP1 and Visual Studio 2012. If you add any new features to your application while in Visual Studio 2012, there is a risk of making it incompatible with Visual Studio 2010.

Convert

For existing Visual Studio 2010 or earlier projects, you will be presented with the opportunity to upgrade the first time you open the project with Visual Studio 2012. You’ll see a warning that the upgrade is one way: After you make the conversion, it won’t be possible to open the project with any earlier version of Visual Studio. Figure 1 provides an example of what this warning looks like when converting a Microsoft Visual Studio 2008 project.


Figure 1. Visual Studio* 2012 project conversion tool

The conversion tool makes a backup copy so you won’t lose your old version. Figure 2 shows the HTML output of the reporting tool, with a summary of the conversion and any issues you need to address. The last line in the Solution section shows that the conversion tool successfully migrated the tstGUIVS08 project.


Figure 2. Project conversion tool summary report

Compile

When you have a converted project, you should be able simply to click Run or press the F5 key to compile and run the solution. If your application is compiled successfully, you should see it open from the Windows 8 desktop. Figure 3 shows the tstGUI sample app running with data loaded.


Figure 3. Sample app running on the Windows 8* desktop

Debug

Visual Studio 2012 has all the same debugging tools as previous versions plus some. Figure 4 shows the code editor with a breakpoint set. You can also pause a running application to examine variables or single-step through sections of code—a great way to test new functionality in an interactive way. One of the handy features available to do this with a running program is the Immediate dialog box.

With the program paused, simply start typing in the Immediate dialog box. To print to the debug window, you would use something like the following:

Debug.Writeline("Column count = " + CurrentRow.GetUpperBound(0)")

The other neat thing about using the feature is the use of IntelliSense*. If you just want to browse the available object properties, you need only type the item of interest, such as CurrentRow above; as soon as you type the period, you’ll see a complete list of available methods and properties.


Figure 4. Visual Studio* 2012 code editor with a breakpoint set

The next step beyond the basic compiling and running of an existing application is to add new features to take advantage of available hardware capabilities.

Investigating New Capabilities


One easy way to learn how to use a new feature or function is to write a simple test application. Using the IntelliSense feature in Visual Studio 2012 makes this process even more interesting, as you explore the various options available when you instantiate a new object for the first time. Although much of the focus on developing Windows 8 applications has been toward the Windows Runtime (WinRT) environment, you have access to much of the same functionality from traditional desktop apps.

There is one default setting in Visual Studio 2012 you must change to take advantage of the Windows 8 platform application programming interfaces (APIs). Unfortunately, it requires a manual process, including editing the project file for your app. Depending on your choice of language, it will either be in the .csproj (C#) or .vbproj (Microsoft Visual Basic*) file.

Step 1 is to create a new project from within Visual Studio 2012. For this section, you use Visual Basic to create a new Windows Forms application with a project named tstproj. Figure 5 shows the tstproj directory in the folder tree beneath Projects.


Figure 5. Visual Studio* 2012 tstproj project folder

Using Microsoft Notepad, open tstproj.vbproj and add the following lines:

<PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

Save the file and reopen the project in Visual Studio 2012. You should now have access to the Windows 8 platform when you open the Reference Manager from the Project > References tab. A new option labeled Core under Windows now appears, as shown in Figure 6. Select the Windows check box, and then click OK.


Figure 6. Reference Manager with the new Windows option

With this reference added, you’ll be able to import any of the options from Windows.Devices.Touch

Many existing controls already respond to touch. If your application uses Windows Forms along with any control needing to scroll, you don’t have to do anything to make it touch-enabled. You might want to consider things like font size if you want the user to select an item using a finger. If your program uses icons, you need to size them appropriately, as well.

To add things like gestures or multitouch to your application, you need several new functions to respond to those actions. In general, touch works just like a mouse event, so you must write event handlers to respond to the different events. The next article will go into much more detail on just how to do so for desktop apps.

Sensors

The sensor platform is not new to Windows 8: Many of the functions were present in the Windows 7 software development kit (SDK). Understanding how to use the different sensors and the information they provide helps you as you evaluate the new features for inclusion in your own apps. Some sensors, such as the ambient light sensor, could add a new level of convenience for applications that could potentially experience different light levels.

Most of the sensors require a multi-step process to retrieve data from them. First, you must initialize the device to set things such as the minimal reporting interval before you actually start sampling data. Then, you must connect an event handler to respond to each sensor event and decide what to do with the data. This is important for a game that needs to respond to user motion.

Understanding Available APIs


With a wide range of new functionality available in the Windows 8 platform, it’s important to evaluate your application with an eye toward new or improved features. You might want to start with a list of questions to help guide you in your journey—for example:

  • How would a user of this application navigate using touch only?
  • How could access to location information enhance this application?
  • What other sensor information would add value to this application?

When you have answers to these questions, you can begin to evaluate the different API functions that would meet your new requirements. You will need to take into consideration the target platform, as not every Ultrabook™ has a complete complement of supported sensors. Some capabilities, such as the ambient light sensor, should be on every device. With a list of target functionality, you can then press on to the actual testing phase.

A list of APIs specifically for the Windows 8 desktop is available on the Microsoft website. (See the “For More Information” section of this article for a link.)

Testing API Functions

You need to do one last thing before you can test all of the API functions available to Windows 8 desktop apps: Set a reference to WinRT using the same Reference Manager you used earlier. The file name is buried under the Reference Assemblies directory, as shown in Figure 7, and is named System.Runtime.InteropServices.WindowsRuntime.dll.


Figure 7. Directory tree with the WinRT DLL selected

When you have this reference set, you’ll be able to reference all of the Windows 8 WinRT functions beyond what you get from just referencing the Imports statement. Part 2 of this series will look at this in detail. For a simple test program, you could just build a console application that sends the output of the sensors to the screen. This is actually a great way to quickly build test apps to determine whether the sensors are behaving the way you would expect.

Sample Code


Listing 1 provides a short Visual Basic program to display the current value of the light sensor.

Listing 1. Source code for a simple Visual Basic program to display the current light sensor value

Imports Windows.Devices.Sensors
Public Class Form1
    Private mylight As LightSensor
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Btn1.Click
        mylight = LightSensor.GetDefault
        Dim lightlevel As LightSensorReading = mylight.GetCurrentReading()
        If lightlevel IsNot Nothing Then
            TB1.Text = String.Format("{0,5:0.00}", lightlevel.IlluminanceInLux)
        End If
    End Sub
End Class

You’ll find several items of interest in this short program. First is the Imports Windows.Devices.Sensors statement, which is required for all Visual Basic programs that require access to any of the available sensors. The other point of interest is the actual flow of the code to read the sensor. The statement mylight = LightSensor.GetDefault sets a reference to the sensor and returns the current default settings. When you have a reference, you can then read the current sensor value using Dim lightlevel As LightSensorReading = mylight.GetCurrentReading(). This statement returns a single reading of the light sensor. You also have the ability to continuously read sensors and respond to change events if your program requires dynamic updating.

Figure 8 shows the running version of the sample app. Clicking Read Light Sensor polls the sensor, and then updates the value in the text box.


Figure 8. Visual Basic* Windows Forms program

Adapting Sample Code to Meet Real Needs

Simple demo programs are great for learning, but how do you use that information in a real program? One of the easiest-to-use functions is location. Several methods exist for getting the current location in Windows 8. Some of these methods require asynchronous operations, meaning you issue a call to the geolocation function, and then wait for it to return. Another function is simply to make a one-time call to the geolocator function and return the current position.

The following code snippet shows what you need to implement for this one-shot version:

Dim myloc as New Geolocator
 Dim mypos = myloc.GetGeoposition
 lbl1.text = mypos.GetResults.Coordinate.Latitude.ToString
 lbl2.text = mypos.GetResults.Coordinate.Longitude.ToString

The asynchronous method is actually the more elegant way to accomplish this task, but it’s beyond the scope of this article. In general, these methods provide a way to build applications that don’t sit around waiting on some activity to finish. Windows 8 uses asynchronous methods for many operations that in the past would render an application sluggish or nonresponsive. Learning how to use these methods in a desktop app will greatly improve your overall user experience and cut down on potential application lock errors, as well.

Wrap Up


Building apps for the Windows 8 desktop is really no different from any previous version of the Windows operating system in terms of the way you go about it. Many new capabilities and functions available from both an API and a hardware perspective give you many options for enhancing existing apps or building totally new ones. The key is in recognizing those small improvements you can make to elevate your app to the next level.

For More Information


Intel, the Intel logo, and Ultrabook are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

如需更全面地了解编译器优化,请参阅优化注意事项