Creating Windows Runtime Components


Published on

Creating Windows Runtime Components

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Creating Windows Runtime Components

  1. 1. Creating Windows Runtime ComponentsMirco VaniniWinRT ComponentsMicrosoft® MVP Windows Embedded
  2. 2. Agenda Libraries for Windows Store Apps Recap - WinRT Internal Projection Windows Runtime Components via C# Understand the Cost When to Create Managed WindowsRuntime Components Q&A Links Contatti
  3. 3. Libraries for Windows Store Apps Class Library (Windows Store Apps).NET library with classes and methods that areavailable for Windows Store apps. Portable Class LibraryOnly a subset of .NET library is available(depends on target platform). Windows Runtime ComponentConsumed in any language that supportsWinRT
  4. 4. WinRT – architectureWindowsMetadata &NamespaceLanguage ProjectionWindows CoreWindows Runtime CoreXAML Storage …NetworkUI Pickers MediaControlsMetro style appRuntimeBrokerLanguageSupport(CLR, WinJS,CRT)Web Host(HTML, CSS,JavaScript))
  5. 5. WinRT - object COM Based !o IUnknown• AddRef• Release• QueryInterfaceo IDispatcho IInspectable• GetIids• GetRuntimeClassName• GetTrustLevelo Windows Metadata (WinMD) - ECMA-335Object
  6. 6. Windows Runtime APIs Available to all programming languages Requires a language neutral typesystemWindowsRuntimeJavaScriptC#/VBC++
  7. 7. APIs in multiple languages Many languages use PascalCasing for namesof types, methods, properties, and events JavaScript has well established namingconventionso “Types” are PascalCasedo Methods and Properties are camelCasedo Events are all lowercase Windows Runtime uses PascalCasing for typesand members Windows Runtime maps JavaScriptmethods, properties and event names to itsconventions
  8. 8. Windows Runtime types
  9. 9. Windows Runtime types - Strings Immutable or mutable?o Immutable – JavaScript, .NET; Mutable – C++ Nullo JavaScript: null is an object, string is a typeo C++: std::string has no null semanticso .NET System.String: reference type has a nulldistinguished value As a result…o Windows Runtime: strings immutable, no nullrepresentation
  10. 10. Windows Runtime types - References Target languages handle pointers andreferences differentlyo C++: All types can be passed by value or byreferenceo .NET: Objects are passed by reference, value typesby valueo JavaScript: Objects passed by reference, numberspassed by valueo Windows Runtime: Objects (Interfaces) passed byreference, all other types passed by value As a result…o Windows Runtime: method parameters are [in] or[out], never [in, out]
  11. 11. Projections
  12. 12. Object Creation
  13. 13. Runtime Callable Wrapper The common language runtime exposes COMobjects through a proxy called the runtimecallable wrapper (RCW). The runtime creates exactly one RCW for eachCOM object, regardless of the number ofreferences that exist on that object. The runtime maintains a single RCW perprocess for each objectThe standard wrapper enforcesbuilt-in marshaling rules. Forexample, when a .NET client passesa String type as part of an argumentto an unmanaged object, thewrapper converts the string to aBSTR type. Should the COM objectreturn a BSTR to its managedcaller, the caller receives a String
  14. 14. Windows Runtime Components via C#/VB With the .NET Framework 4.5, you can usemanaged code to create your own WindowsRuntime types, packaged in a WindowsRuntime component. You can use your component in Windows Storeapps with C++, JavaScript, Visual Basic, or C#
  15. 15. Traditional COM in Managed CodeComVisible(true), Guid("06D7901C-9045-4241-B8A0-39A1AC0F8618")]public interface IWindowsApiSample{string HelloWorld();}[ComVisible(true), [ClassInterface(ClassInterfaceType.None)][ComDefaultInterface(typeof(IWindowsApiSample))]public class WindowsApiSample : IWindowsApiSample{public string HelloWorld() {return "Hello, World!";}}
  16. 16. Windows Runtime in managed codeSamplepublic sealed class MyClassLibrary{public string HelloWorld(){return "Hello, World!";}}
  17. 17. Windows Runtime Components Rules Public classes must be sealed. All public types must have a root namespacethat matches the assembly name. The fields, parameters, and return values of allthe public types and members in yourcomponent must be Windows Runtime types. A public class or interface cannot:o Be generic.o Implement an interface that is not a WindowsRuntime interface.o Derive from types that are not in the WindowsRuntime..NET Framework mappings of Windows Runtime types
  18. 18. demo
  19. 19. Windows Runtime Components Rules Using the Windows Runtime from JavaScriptand managed code:o The Windows Runtime can be called from eitherJavaScript or managed code.o Windows Runtime objects can be passed back andforth between the two.o Events can be handled from either side.o The ways you use Windows Runtime types in the twoenvironments differ in some details, becauseJavaScript and the .NET Framework support theWindows Runtime differently.
  20. 20. Windows Runtime Components Rules You can throw any exception type that isincluded in the .NET for Windows Store apps -supported APIs. You cant declare your own public exceptiontypes in a Windows Runtime component. The way the exception appears to the callerdepends on the way the calling languagesupports the Windows Runtime.o In JavaScript, the exception appears as an object inwhich the exception message is replaced by a stacktrace.o In C++, the exception appears as a platformexception.
  21. 21. demo
  22. 22. Windows Runtime Components Rules You can return a managed type, created inmanaged code, to JavaScript as if it were thecorresponding Windows Runtime type.o When a managed type implements multipleinterfaces, JavaScript uses the interface that appearsfirst in the list.o If you pass an unassigned JavaScript variable as astring argument, what you get is the string"undefined".
  23. 23. demo
  24. 24. Windows Runtime Components Rules You can declare events by using the standard.NET Framework event pattern or otherpatterns used by the Windows Runtime.o When you expose an event in the WindowsRuntime, the event argument class inherits fromSystem.Object. It doesnt inherit fromSystem.EventArgs, as it would in the .NETFramework, because EventArgs is not a WindowsRuntime type.o If you declare custom event accessors for yourevent, you must use the Windows Runtime eventpattern. Custom events and event accessors inWindows Runtime Components
  25. 25. demo
  26. 26. Windows Runtime Components Rules To implement an asynchronous method in yourcomponent, add "Async" to the end of themethod name and return one of the WindowsRuntime interfaces that representasynchronous actions or operations:o IAsyncActiono IAsyncActionWithProgress<TProgress>o IAsyncOperation<TResult>o IAsyncOperationWithProgress<TResult, TProgress>
  27. 27. Windows Runtime Components Rules For asynchronous actions and operations thatdo not support cancellation or progressreporting, you can use the AsAsyncAction orAsAsyncOperation<TResult> extension methodto wrap the task in the appropriate interface.
  28. 28. demo
  29. 29. Understand the Cost Creating Windows Runtime Components usingmanaged languages is a powerful feature. It’s important to understand the cost whenyou’re using it in projects:o Windows Store apps built using native code don’trequire the CLR to run.o The code written for the managed Windows RuntimeComponent will be compiled just-in-time (JIT).o The GC might pause your app while it’s performingwork.
  30. 30. When to Create Managed Windows RuntimeComponents When to Create Managed Windows RuntimeComponents:o Consumable from all of the Windows Storelanguages.o Your team is more experienced in C# or Visual Basicthan in C++o You have existing algorithms written in a managedlanguageo …
  31. 31. When to Create Managed Windows RuntimeComponents If you are creating a component for use only inWindows Store apps with Visual Basic orC#, and the component does not containWindows Store controls, consider using theClass Library (Windows Store apps) templateinstead of the Windows Runtime Componenttemplate !!! Alternatives for Managed Projects:o Class library for Windows Storeo Portable Class Libraryo Extension SDK
  32. 32. Q&A
  33. 33. LinksCreating Windows Runtime Components in C#and Visual BasicWalkthrough: Creating a simple component in C#or Visual Basic and calling it from JavaScript.Windows Runtime Components in a .NET World
  34. 34. NetMF@Work31 Maggio 2013 ore 09:00Sede MicrosoftVia Avignone 10, RomaUn’intera giornata per parlare di.NET Micro Framework e .NETGadgeteer, dall’introduzione allarealizzazione di soluzioni IoT realiper un mondo di deviceinterconessi
  35. 35. feedback10Blog http://mircovanini.blogspot.comEmail info@proxsoft.itWeb www.proxsoft.itTwitter @MircoVaniniContattio feedback su:• codice feedback:• ????