35
Creating Windows Runtime Components Mirco Vanini WinRT Components Microsoft® MVP Windows Embedded

Creating Windows Runtime Components

Embed Size (px)

DESCRIPTION

Creating Windows Runtime Components

Citation preview

Page 1: Creating Windows Runtime Components

Creating Windows Runtime Components Mirco Vanini

WinRT Components

Microsoft® MVP Windows Embedded

Page 2: Creating Windows Runtime Components

Agenda

Libraries for Windows Store Apps Recap - WinRT Internal Projection Windows Runtime Components via

C# Understand the Cost When to Create Managed Windows

Runtime Components Q&A Links Contatti

Page 3: Creating Windows Runtime Components

Libraries for Windows Store Apps

Class Library (Windows Store Apps).NET library with classes and methods that are available 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 supports WinRT

Page 4: Creating Windows Runtime Components

WinRT – architecture

Windows Metadata

& Namespa

ce

Language Projection

Windows Core

Windows Runtime Core

XAML Storage …Network

UI Pickers MediaControls

Metro style app

Runtime Broker

Language Support

(CLR, WinJS, CRT)

Web Host (HTML, CSS, JavaScript))

Page 5: Creating Windows Runtime Components

WinRT - object

COM Based !o IUnknown

• AddRef• Release • QueryInterface

o IDispatch o IInspectable

• GetIids• GetRuntimeClassName• GetTrustLevel

o Windows Metadata (WinMD) - ECMA-335

Object

IInspectable

IUnknown

Page 6: Creating Windows Runtime Components

Windows Runtime APIs

Available to all programming languages

Requires a language neutral type system

Windows Runtime

Page 7: Creating Windows Runtime Components

APIs in multiple languages

Many languages use PascalCasing for names of types, methods, properties, and events

JavaScript has well established naming conventionso “Types” are PascalCasedo Methods and Properties are camelCasedo Events are all lowercase

Windows Runtime uses PascalCasing for types and members

Windows Runtime maps JavaScript methods, properties and event names to its conventions

Page 8: Creating Windows Runtime Components

Windows Runtime types

Page 9: Creating Windows Runtime Components

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 'null'

distinguished value As a result…

o Windows Runtime: string's immutable, no null representation

Page 10: Creating Windows Runtime Components

Windows Runtime types - References

Target languages handle pointers and references differentlyo C++: All types can be passed by value or by

referenceo .NET: Objects are passed by reference, value

types by valueo JavaScript: Objects passed by reference,

numbers passed by valueo Windows Runtime: Objects (Interfaces) passed

by reference, all other types passed by value As a result…

o Windows Runtime: method parameters are [in] or [out], never [in, out]

Page 11: Creating Windows Runtime Components

Projections

Page 12: Creating Windows Runtime Components

Object Creation

Page 13: Creating Windows Runtime Components

Runtime Callable Wrapper

The common language runtime exposes COM objects through a proxy called the runtime callable wrapper (RCW).

The runtime creates exactly one RCW for each COM object, regardless of the number of references that exist on that object.

The runtime maintains a single RCW per process for each object The standard wrapper enforces

built-in marshaling rules. For example, when a .NET client passes a String type as part of an argument to an unmanaged object, the wrapper converts the string to a BSTR type. Should the COM object return a BSTR to its managed caller, the caller receives a String

Page 14: Creating Windows Runtime Components

Windows Runtime Components via C#/VB

With the .NET Framework 4.5, you can use managed code to create your own Windows Runtime types, packaged in a Windows Runtime component.

You can use your component in Windows Store apps with C++, JavaScript, Visual Basic, or C#

Page 15: Creating Windows Runtime Components

Traditional COM in Managed Code

[ComVisible(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!"; }}

Page 16: Creating Windows Runtime Components

Windows Runtime in managed code Sample

public sealed class MyClassLibrary{ public string HelloWorld() { return "Hello, World!"; }}

Page 17: Creating Windows Runtime Components

Windows Runtime Components Rules

Public classes must be sealed. All public types must have a root

namespace that matches the assembly name.

The fields, parameters, and return values of all the public types and members in your component must be Windows Runtime types.

A public class or interface cannot:o Be generic.o Implement an interface that is not a Windows

Runtime interface.o Derive from types that are not in the Windows

Runtime.

.NET Framework mappings of Windows Runtime types

Page 18: Creating Windows Runtime Components

demo

Page 19: Creating Windows Runtime Components

Windows Runtime Components Rules

Using the Windows Runtime from JavaScript and managed code:o The Windows Runtime can be called from

either JavaScript or managed code.o Windows Runtime objects can be passed back

and forth between the two.o Events can be handled from either side.o The ways you use Windows Runtime types in

the two environments differ in some details, because JavaScript and the .NET Framework support the Windows Runtime differently.

Page 20: Creating Windows Runtime Components

Windows Runtime Components Rules

You can throw any exception type that is included in the .NET for Windows Store apps - supported APIs.

You can't declare your own public exception types in a Windows Runtime component.

The way the exception appears to the caller depends on the way the calling language supports the Windows Runtime.o In JavaScript, the exception appears as an

object in which the exception message is replaced by a stack trace.

o In C++, the exception appears as a platform exception.

Page 21: Creating Windows Runtime Components

demo

Page 22: Creating Windows Runtime Components

Windows Runtime Components Rules

You can return a managed type, created in managed code, to JavaScript as if it were the corresponding Windows Runtime type.o When a managed type implements multiple

interfaces, JavaScript uses the interface that appears first in the list.

o If you pass an unassigned JavaScript variable as a string argument, what you get is the string "undefined".

Page 23: Creating Windows Runtime Components

demo

Page 24: Creating Windows Runtime Components

Windows Runtime Components Rules

You can declare events by using the standard .NET Framework event pattern or other patterns used by the Windows Runtime.o When you expose an event in the Windows

Runtime, the event argument class inherits from System.Object. It doesn't inherit from System.EventArgs, as it would in the .NET Framework, because EventArgs is not a Windows Runtime type.

o If you declare custom event accessors for your event, you must use the Windows Runtime event pattern. Custom events and event accessors in Windows Runtime Components

Page 25: Creating Windows Runtime Components

demo

Page 26: Creating Windows Runtime Components

Windows Runtime Components Rules

To implement an asynchronous method in your component, add "Async" to the end of the method name and return one of the Windows Runtime interfaces that represent asynchronous actions or operations: o IAsyncActiono IAsyncActionWithProgress<TProgress>o IAsyncOperation<TResult>o IAsyncOperationWithProgress<TResult,

TProgress>

Page 27: Creating Windows Runtime Components

Windows Runtime Components Rules

For asynchronous actions and operations that do not support cancellation or progress reporting, you can use the AsAsyncAction or AsAsyncOperation<TResult> extension method to wrap the task in the appropriate interface.

Page 28: Creating Windows Runtime Components

demo

Page 29: Creating Windows Runtime Components

Understand the Cost

Creating Windows Runtime Components using managed languages is a powerful feature.

It’s important to understand the cost when you’re using it in projects:o Windows Store apps built using native code

don’t require the CLR to run.o The code written for the managed Windows

Runtime Component will be compiled just-in-time (JIT).

o The GC might pause your app while it’s performing work.

Page 30: Creating Windows Runtime Components

When to Create Managed Windows Runtime Components

When to Create Managed Windows Runtime Components:o Consumable from all of the Windows Store

languages. o Your team is more experienced in C# or Visual

Basic than in C++ o You have existing algorithms written in a

managed language o …

Page 31: Creating Windows Runtime Components

When to Create Managed Windows Runtime Components

If you are creating a component for use only in Windows Store apps with Visual Basic or C#, and the component does not contain Windows Store controls, consider using the Class Library (Windows Store apps) template instead of the Windows Runtime Component template !!!

Alternatives for Managed Projects:o Class library for Windows Store o Portable Class Libraryo Extension SDK

Page 32: Creating Windows Runtime Components

Q&A

Page 34: Creating Windows Runtime Components

NetMF@Work

31 Maggio 2013 ore 09:00

Sede MicrosoftVia Avignone 10, Roma

Un’intera giornata per parlare di .NET Micro

Framework e .NET Gadgeteer, dall’introduzione alla realizzazione di soluzioni

IoT reali per un mondo di device interconessi

Page 35: Creating Windows Runtime Components

feedback

10

Blog http://mircovanini.blogspot.com

Email [email protected]

Web www.proxsoft.it

Twitter @MircoVanini

Contatti

o feedback su:• http://xedotnet.org/feedback

o codice feedback: • ????