win8 on intel programming course desktop : sensors cédric andreolli [email protected]...

34
Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli www.Intel-Software-Academic-Program.com [email protected] Intel Software 2013-03-20

Upload: colten-franklin

Post on 14-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Win8 on Intel Programming CourseDesktop : Sensors

Cédric Andreolliwww.Intel-Software-Academic-Program.com

[email protected] Software

2013-03-20

Page 2: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Agenda

Page 3: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Agenda

Sensors in desktop applicationsThis part will introduce the sensors in desktop applications

Enabling WinRT in WPF applicationsA quick “how to” that shows the steps needed to enable WinRT API in WPF applications

Programming the SensorsAn example that will show how to use the sensors in your desktop applications

Page 4: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Sensors in Desktop applications

Page 5: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Sensors in desktop applications

Why do we need the sensors?Desktop applications represent the biggest part of todays applicationsA 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…

Page 6: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

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

Page 7: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF applications

Page 8: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Create a new WPF projectCreate also a new solution and name your project “HelloWorld”

Page 9: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Set up the environmentRight 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)

Page 10: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

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.csprojThis file is in your Visual Studio project directory (Documents\Visual Studio 2012\Projects\WPF\HelloWorld)

Page 11: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the target (Version 1)You can add the TargetPlatformVersion in an existing PropertyGroup

Page 12: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the target (Version 2)You can create a PropertyGroupThis is the best solution because all your project can access this property

Page 13: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Reload the projectGo back in Visual Studio, you should see:

Click on Reload All

Page 14: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the referenceIn the Solution Explorer, right click on References -> Add References…On the left, select Windows -> Core and check Windows

Page 15: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the referenceClick on OKCongratulations, WinRT is Linked!

WinRT can be linked by specifying Windows 8 as target for our application

Page 16: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

…but we are not doneThe devices are available right now but the devices event handlers can’t be used

If you try to use it:

Page 17: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the Runtime DLLSome runtime DLL are missingYou can find those DLL in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 The files are:

System.Runtime.dllSystem.Runtime.InteropServices.WindowsRuntime.dll

Right click on your project’s name and select Add References…

Page 18: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Enabling WinRT in WPF apps

Add the Runtime DLL

To access WinRT you must also link 2 DLLs (Runtime and InteropServices)

Page 19: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensors

Page 20: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensors

Project settingsA part of sensor programming for desktop is similar to sensor programming for Windows Store applicationsCreate a WPF window as follow

Page 21: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsProject settingsAdd name properties for each of your TextboxIn the next slides, the Textboxes will be called accX, accY and accZ

Create an accelerometerOpen the C# file and add

Page 22: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsSolve missing referencesThe class Accelerometer is unkown, in the C# editor, right click on it, select Resolve and click on the first option

Page 23: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsRetrieve a valid accelerometerAs in Windows Store Applications, use the Windows.Devices.Sensors.Accelerometer.GetDefault() method to retrieve a valid accelerometer

Page 24: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsAdd a handlerVisual 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

Page 25: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsAdd a handler

Pressing Tab a second time will create the following function

Page 26: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsSensors handlersIf you try to access the UI elements in the handler, you will get troubles

Page 27: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsSensors handlersThe handler is launched by the sensor manager which doesn’t run on the UI threadThis 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 elementEach UI control has a Dispatcher attribute

Page 28: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsUsing a Dispatcher (version 1)A dispatcher can invoke a delegate

Page 29: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsUsing a Dispatcher (version 2)Same code without the Action

Page 30: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Programming the sensorsRun the programYou 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

Page 31: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

Conclusion

Page 32: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

ConclusionWinRT is available in desktop applicationsUsing WinRT offers an easy way to access interesting features in Windows desktop applicationYou just need to specify the target and to reference 2 DLLs

Sensors programmingProgramming the sensors is not difficult in desktop applicationsYou just need to resolve the thread troubles

Page 33: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20
Page 34: Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli  paul.guermonprez@intel.com Intel Software 2013-03-20

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

holder. • 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.

http://creativecommons.org/licenses/by/3.0/