win8 on intel programming course desktop : sensors cédric andreolli [email protected]...
TRANSCRIPT
Win8 on Intel Programming CourseDesktop : Sensors
Cédric Andreolliwww.Intel-Software-Academic-Program.com
[email protected] Software
2013-03-20
Agenda
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
Sensors in Desktop applications
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…
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 projectCreate also a new solution and name your project “HelloWorld”
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)
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)
Enabling WinRT in WPF apps
Add the target (Version 1)You can add the TargetPlatformVersion in an existing PropertyGroup
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
Enabling WinRT in WPF apps
Reload the projectGo back in Visual Studio, you should see:
Click on Reload All
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
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
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:
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…
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 settingsA part of sensor programming for desktop is similar to sensor programming for Windows Store applicationsCreate a WPF window as follow
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
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
Programming the sensorsRetrieve a valid accelerometerAs in Windows Store Applications, use the Windows.Devices.Sensors.Accelerometer.GetDefault() method to retrieve a valid accelerometer
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
Programming the sensorsAdd a handler
Pressing Tab a second time will create the following function
Programming the sensorsSensors handlersIf you try to access the UI elements in the handler, you will get troubles
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
Programming the sensorsUsing a Dispatcher (version 1)A dispatcher can invoke a delegate
Programming the sensorsUsing a Dispatcher (version 2)Same code without the Action
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
Conclusion
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
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/