itcamp 2012 - raffaele rialdi - introduction to winrt
TRANSCRIPT
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Introduction to
Windows Runtime (WinRT)
Raffaele Rialdi
@raffaeler
[email protected] http://www.iamraf.net
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development ITCamp 2012 sponsors
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development
• You already heard about
– «Tailored User eXperience»
• Inspiring confidence, Fast and fluid, Touch-first, Immersive,
Engaging and Alive, Connected
– Why current applications does not fit the new market
• We will dig into:
– What is the Windows Runtime (WinRT)
– Why Win8 is not a simple UI restyling
– What are the pillars (from a developer point of view)
– How can you get it to the max
Agenda
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development
• Procedural languages have no implicit lifetime
concept
– malloc free, CreateFile CloseHandle, …
• In .NET, Dispose pattern is a partial solution
– It's not automatic
– Can lead to resources leak
• Requires PInvoke for accessing C-Libraries
• Communication between processes is done via IPC
and not between components
OO vs not-OO
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Windows Runtime Architecture
Metro apps Desktop apps
Windows Runtime APIs and Services
Win32
Language Projections
.NET / Js / C++
XAML DirectX HTML HTML WPF SL Win form
MFC
DX
.NET / Js / C++
Windows Kernel Services
Win32 XAML Pickers … Network
UI Controls Media Storage
Windows Metadata
Windows Runtime Core
Runtime Broker
Filtered access to WinRT
BCL / libraries
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development
• It's the evolution of Component Object
Model (COM)
– Easier
– Totally different Type System
– Same metadata of the .NET Fx (ECMA-335)
• WinRT and CLR talks the same "language"
• No marshalling gotchas
• It's not a replacement for the CLR
• CLR is required for managed languages
infrastructure
What is the Windows Runtime
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Reference Count VS Garbage Collector
• Circular references • GC finds can find them as it freeze the app and search the roots
• Reference Count is autonomous and it can't
• In the new C++11 standard we have weak references
A B
Root
Parent
Parent
A B
Root
Parent
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development WinRT Type System
• Basic types - bool, integers, floats, enum, guid, type, object
• Strings - binary compatibile with .NET (string) and C++ STL (wstring)
- are immutable
- are value-types (non-nullable)
• There are Reference Types - All types that implement WinRT interfaces
• There are Value Types - All the others … for example arrays and structures
- Structures can't declare reference (deep/shallow copy problem)
• "Complex" types - Vector (collection) and Map (dictionary)
- Vector<T> implements IObservableVector<T> that is mapped to INotifyCollectionChanged
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Language Projections
•Projections map WinRT types to the projected language
• Different casing standards - Javascript (camelCase), C++ and .NET (PascalCase)
• Developers need to understand edge-cases - Javascript standard only support IEEE-754 (floats).
This means the max integer is 53 bit maximum
• Few types are defined both in WinRT and .NET - They are treated as 'special' and seen as equivalent
- INotifyPropertyChanged, INotifyCollectionChanged, …
• Some .NET types cannot be mapped transparently - Streams, Buffers, Tasks are mapped via extension methods
• I expect third party projections too - Java?, Python?, D language?, Delphi?, …
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development
Class Catalog
Explorer.exe
Activation System
Activate RPCSS
DCOM Launch
Application.exe
Application Activation
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Contracts : «XXI century's clipboard»
• A standard way to "talk" to other Apps or OS services
• A sort of publisher / subscriber pattern
• Main Contracts available:
• Search, Share, Protocol, PlayTo, App to App picking
• WinRT activate Apps that expose Contracts
• Running Apps is done via Launch Contract
• Tiles are NOT shortcuts!
• Apps use the Manifest to opt-in for Contracts
• They receive parameters in the activation method
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Asynchronous by design
• Special objects wraps Asynchronous Operations
• In WinRT they implement IAsyncOperation<T>
• AsTask extension method is used to convert it to a Task<T>
• In .NET these objects are Task<T>
• AsAsyncOperation extension method to obtain an IAsyncOperation<T>
• C# 5.0 have new async/await keywords
• Javascript and C++ can use "promises" pattern
Problem: Apps should never block UI thread
Solution: API that may take more than 50ms are only async
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Application lifecycle
• Suspension happens after ~5s the App is not visible
• Suspension message is used to save App state
• Resume happens when user switch to the App
• Typically used to refresh data from sensors, WS, …
•There is no notification for the close transition
• App state is already saved during suspension
App is
Activated
Process in
Active state
Suspend Process
in Suspended state
Low Memory (system)
App is
closed Resume
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development
Marketplace
The sandbox
Application Package
WinRT
Windows Kernel Services
Win32
Security Broker Proxy
Libraries
White-listed
Manifest
Application code
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Developing with WinRT • XAML UI for .NET and C++
• XAML / DirectX can share surfaces • DirectX inside a XAML control
• Large virtual DirectX surface in conjunction with XAML
• High Performance DirectX drawing, XAML overlapped
• Custom WinRT components are easy to write • i.e. exposing C# or C++ code to Javascript or vice-versa
• Custom components are private to the App (no RegSvr32)
• Cross language calls are cheap • No more P-Invokes!
• As fast as a vtable call
• Desktop Apps can use a WinRT APIs subset
• CLR have an additional weapon: "Portable Class Library" • A DLL with code that run on Metro, desktop, SL, …
• As it's IL code, it runs as 32 or 64 bits as well
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Metro profile for .NET development
• What else is not in the Metro profile?
• APIs that are already in WinRT (Storage, Sockets, Network, etc.)
• Server libraries like WCF, Asp.net, …
• APIs that could bypass the sandbox • System.Data, Remoting, AppDomain, Private Reflection, …
Metro Fx 4.5 WP7
# assemblies 15 120 22
# namespaces 60 400 88
# types ~1'000 ~14'000 ~2'000
# members ~10'000 ~110'000 ~14'000
Metro profile was a good opportunity to clean BCL • Removed: Xml DOM, WebClient, Threads, … • Moved Reflection in System.Reflection
• GetTypeInfo extension method to access reflection classes
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Developer thoughts
• No need to elevate (UAC) a Metro App • Admin Apps will run in the old desktop
• Storage • Apps can access only locations specified by manifest
• Users can pick (via UI) a file that is stored elsewhere
• No local db APIs at the moment
• Media and Sensors • Easy access to cameras, accelerometer, …
• Require user consent (requested via manifest)
• Network • Great library to ease oAuth authentication
• Flexible HttpClient replace WebClient
• Websockets support
• Dig into the APIs and discover it by yourself!
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Takeaways
• WinRT is a great step in Windows evolution
• Exposes native OS services in a pure OOP way
• Closes the gap between managed and native
languages
• Developers can use their current knowledge to
create Apps from small devices to classic PCs
• Marketplace is an opportunity for developers
and a sweet experience for end-users
• Think about performance/battery issues
• It's Framework.NET best friend
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Mobile &
Development Tools for WinRT
• WinRT.codeplex.com
• Debug Activation, WRL project template, ... (more to come)
itcampro @ itcamp12 # Premium conference on Microsoft technologies
Q & A