PPTX - Intel Software Academic Program

Win8 on Intel Programming Course
Desktop : Sensors
Cédric Andreolli
[email protected]
Intel Software
Sensors in desktop applications
This part will introduce the sensors in desktop applications
Enabling WinRT in WPF applications
A quick “how to” that shows the steps needed to enable
WinRT API in WPF applications
Programming the Sensors
An example that will show how to use the sensors in your
desktop applications
Sensors in Desktop applications
Sensors in desktop applications
Why do we need the sensors?
Desktop applications represent the biggest part of todays
A lot of applications can’t run in Modern UI because of
native libraries dependencies
How to access the sensors in desktop applications ?
Sensors can be programmed with the win32 API but…
It is complicated…
Sensors in desktop applications
And what about the WinRT API?
WinRT is way simpler than win32…
And WinRT can be enabled in C# but we must change
some configuration files to access it!
Desktop Applications can use the sensors through
WinRT but Visual Studio need some configuration
Enabling WinRT in WPF applications
Enabling WinRT in WPF apps
Create a new WPF project
Create also a new solution and name your project
Enabling WinRT in WPF apps
Set up the environment
Right now, WinRT is not available, you can’t add the
reference to WinRT (You can look for it in References -> Add but you
won’t find it)
Enabling WinRT in WPF apps
How can we reference it?
By default, WinRT is available for applications that target
Windows 8 Store.
This information can be added in the .csproj file
Open the file HelloWorld.csproj
This file is in your Visual Studio project directory
(Documents\Visual Studio 2012\Projects\WPF\HelloWorld)
Enabling WinRT in WPF apps
Add the target (Version 1)
You can add the TargetPlatformVersion in an existing
Enabling WinRT in WPF apps
Add the target (Version 2)
You can create a PropertyGroup
This is the best solution because all your project can access
this property
Enabling WinRT in WPF apps
Reload the project
Go back in Visual Studio, you should see:
Click on Reload All
Enabling WinRT in WPF apps
Add the reference
In the Solution Explorer, right click on References -> Add
On the left, select Windows -> Core and check Windows
Enabling WinRT in WPF apps
Add the reference
Click on OK
Congratulations, WinRT is Linked!
WinRT can be linked by specifying Windows 8
as target for our application
Enabling WinRT in WPF apps
…but we are not done
The devices are available right now but the devices event
handlers can’t be used
If you try to use it:
Enabling WinRT in WPF apps
Add the Runtime DLL
Some runtime DLL are missing
You can find those DLL in C:\Program Files (x86)\Reference
The files are:
Right click on your project’s name and select Add
Enabling WinRT in WPF apps
Add the Runtime DLL
To access WinRT you must also link 2 DLLs
(Runtime and InteropServices)
Programming the sensors
Programming the sensors
Project settings
A part of sensor programming for desktop is similar to
sensor programming for Windows Store applications
Create a WPF window as follow
Programming the sensors
Project settings
Add name properties for each of your Textbox
In the next slides, the Textboxes will be called accX, accY
and accZ
Create an accelerometer
Open the C# file and add
Programming the sensors
Solve missing references
The class Accelerometer is unkown, in the C# editor, right
click on it, select Resolve and click on the first option
Programming the sensors
Retrieve a valid accelerometer
As in Windows Store Applications, use the
method to retrieve a valid accelerometer
Programming the sensors
Add a handler
Visual Studio can help you generating your handler
Display auto-completion by pressing Ctrl+Tab
Let Visual Studio add the handler by pressing Tab when the tooltip appear
Programming the sensors
Add a handler
Pressing Tab a second time will create the following
Programming the sensors
Sensors handlers
If you try to access the UI elements in the handler, you will
get troubles
Programming the sensors
Sensors handlers
The handler is launched by the sensor manager which
doesn’t run on the UI thread
This thread is not allowed to access UI elements
How to solve this problem?
WPF provides dispatchers that can send a job to the thread
of a specific element
Each UI control has a Dispatcher attribute
Programming the sensors
Using a Dispatcher (version 1)
A dispatcher can invoke a delegate
Programming the sensors
Using a Dispatcher (version 2)
Same code without the Action
Programming the sensors
Run the program
You should see the content of the Textbox change if your computer has
an accelerometer
Except for the thread managment, using the
sensors in desktop applications is almost similar
to Windows Store application
WinRT is available in desktop applications
Using WinRT offers an easy way to access interesting
features in Windows desktop application
You just need to specify the target and to reference 2 DLLs
Sensors programming
Programming the sensors is not difficult in desktop
You just need to resolve the thread troubles
License Creative Commons – By 3.0
You are free:
• to Share — to copy, distribute and transmit the work
• to Remix — to adapt the work
• to make commercial use of the work
Under the following conditions:
• Attribution — You must attribute the work in the manner specified by the author or licensor (but
not in any way that suggests that they endorse you or your use of the work).
With the understanding that:
• Waiver — Any of the above conditions can be waived if you get permission from the copyright
• Public Domain — Where the work or any of its elements is in the public domain under applicable
law, that status is in no way affected by the license.
• Other Rights — In no way are any of the following rights affected by the license:
Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations;
The author's moral rights;
Rights other persons may have either in the work itself or in how the work is used, such as publicity or
privacy rights.
Notice — For any reuse or distribution, you must make clear to others the license terms of this
work. The best way to do this is with a link to this web page.

similar documents