Native Development for Windows Phone 8

28
WAY AHEAD™ Native Development for Windows Phone The Windows Runtime API and Modern C++

description

The largest change in the Windows Phone 8 SDK was the addition of support for native code. As a .NET developer you may be turned off by the idea of returning to an unmanaged language, but with the Windows Runtime (WinRT) API, Component Extensions (C++/CX) and C++ 11 support, native development for Windows Phone 8 may be less intimidating then you would think. In this talk I’ll go over some of the features of C++/CX and show examples of using the native WinRT API in a Windows Phone 8 app.

Transcript of Native Development for Windows Phone 8

Page 1: Native Development for Windows Phone 8

WAY AHEAD™

Native Development for Windows Phone

The Windows Runtime API and Modern C++

Page 2: Native Development for Windows Phone 8

2

» Senior Software Engineer at ALK Technologies» Led development effort to bring CoPilot GPS

navigation app to Windows Phone 8» #4 Free Navigation App in Window Phone store

» Windows Phone Dev by day and night» Released 6 of my own apps to Windows Phone store

» Blog: www.robwirving.com» Twitter: @robwirving

Rob Irving

Page 3: Native Development for Windows Phone 8

3

» History of Native on Windows Phone» Why use Native?» C++ 11» Windows Runtime (WinRT)» Component Extensions (C++/CX)» Options in Visual Studio» Sample Code» Q & A

Agenda

Page 4: Native Development for Windows Phone 8

4

» Lack of Native Development on WP7 hurt the platform» Lack of high-end games available on other platforms» More difficult/impossible to port existing apps from

other platforms

» Support for Native Development is the single largest change in the WP8 SDK

» Native Gaming and Middleware products» Unity 3D, Havok, Cocos 2D, etc.

History of Native on Windows Phone

Page 5: Native Development for Windows Phone 8

WAY AHEAD™

Why use Native?

Page 6: Native Development for Windows Phone 8

6

» Reusability» Existing code that you don’t want to rewrite» Use of existing 3rd party libraries (SQLite)

» Portability» Share code between Windows, iOS and Android» Write once, use it everywhere

Why use Native?

Page 7: Native Development for Windows Phone 8

7

» Game development with high-end graphics» Direct3D on Windows and Windows Phone» OpenGL on iOS and Android

» Performance» Should not be viewed as the main reason to use C++» Performance can be better using Native, but is often

exaggerated

Why use Native?

Page 8: Native Development for Windows Phone 8

8

» Portability & Reusability in action» ~1,000 Lines of C# for WP8» ~7,000 Lines of new C++ and C++/CX » ~800,000 Lines of existing C++ code

»~99% shared code between Windows Phone, iOS and Android!

CoPilot GPS – Native Case Study

Page 9: Native Development for Windows Phone 8

WAY AHEAD™ A brief introduction to Modern C++

C++ 11

Page 10: Native Development for Windows Phone 8

10

1979

1983

1998 2011

History of C++C++ began as anenhancementto C at Bell Labs

Originally named‘C with Classes’

Renamed to C++

C++ ISO committee defined C++ 98

No significant changes to C++ for 13 years

C++ ISO committee defined C++ 11

New features heavily influenced by managed languages like .NET

Page 11: Native Development for Windows Phone 8

11

» Type inference (auto)» Avoid explicitly declaring the type of a variable» Similar to .NET ‘var’ keyword

» Lambdas» Anonymous inline functions

C++ 11 Features

int num1 = 10;double num2 = 2.5

auto num3 = num1 / num2;

std::vector<CityLocation> cities;

std::sort(begin(cities), end(cities), [](CityLocation a, CityLocation b){ return a.distance < b.distance;});

Page 12: Native Development for Windows Phone 8

12

» Strongly type enums

» Foreach-ish loop

C++ 11 Features

int numbers[] = {0, 1, 2, 3, 4, 5};int sum = 0;

for(int& number : numbers){ sum += number;}

enum class Fruit{ Apple, Banana, Orange};

if(meal.Fruit == Fruit::Banana) // do Banana stuff

int fruitAbomination = Apple + Orange; // ERROR - Can’t do this anymore

Page 13: Native Development for Windows Phone 8

WAY AHEAD™

Windows Runtime API

Page 14: Native Development for Windows Phone 8

14

» Shared API between Windows Phone 8 and Windows Store Apps» Overlaps much of the existing Windows Phone .NET

API» Not all APIs are available for both

» ~11,000 Windows Runtime

» ~2,800 for both

» ~600 Windows Phone Runtime

Windows Runtime API (WinRT)

Page 15: Native Development for Windows Phone 8

15

» API is a COM-based Native API

» Accessible from C#, VB.NET and Component Extensions (C++/CX)» Also Javascript on Windows 8

» API definitions in Windows Metadata (.winmd) files» Similar format to .NET API definitions

Windows Runtime API (WinRT)

Page 16: Native Development for Windows Phone 8

16

» Native / Managed Interop» WinRT is the only supported method of interop

» The most painful parts of Native /Managed interop are gone with Windows Runtime» No P/Invoke» No Marshalling

Native Interop is easy with WinRT

Page 17: Native Development for Windows Phone 8

WAY AHEAD™

Component Extensions (C++/CX)

Page 18: Native Development for Windows Phone 8

18

» Syntax and Library abstractions to interact with COM based WinRT API

» Much easier to develop for compared to old COM programming

Component Extensions (C++/CX)

Page 19: Native Development for Windows Phone 8

19

» Syntactically similar to C++/CLI» ‘ref’ and ‘sealed’ classes » Reference pointers or ‘hats’ ^

Component Extensions (C++/CX)

public ref class Foo sealed{};

Foo^ foo = ref new Foo();

Page 20: Native Development for Windows Phone 8

20

» Differences from C++/CLI» No managed CLR» No Garbage Collection, ref counted objects instead» ref new instead of gcnew» Pure C++ classes allowed in C++/CX classes» Global ref ptr’s allowed

Component Extensions (C++/CX)

Page 21: Native Development for Windows Phone 8

21

» Properties» No value keyword» Can’t have public get, private set

Component Extensions (C++/CX)

public ref class Foobar sealed{ public: property int Foo; // automatic/trivial property property int Bar { int get() { return _bar; } void set(int newBar) { if(newBar > 0) _bar = newBar; } }

private: int _bar;};

Page 22: Native Development for Windows Phone 8

22

» Events » Very similar to .NET events» Unsubscribe using token

Component Extensions (C++/CX)

MyClass::MyClass(){ geoLocator = ref new Geolocator(); // subscribe eventToken = geoLocator->PositionChanged += ref new TypedEventHandler<Geolocator^, PositionChangedEventArgs^>(this, &MyClass::OnPosChanged);}

MyClass::~MyClass(){ // unsubscribe geoLocator->PositionChanged -= eventToken;}

void MyClass::OnPosChanged(Geolocator^ geoLocator, PositionChangedEventArgs^ args){ auto geoPos = args->Position->Coordinate;}

Page 23: Native Development for Windows Phone 8

23

» XAML with Direct 3D

Options in Visual Studio

Page 24: Native Development for Windows Phone 8

24

» Several Native Projects available

Options in Visual Studio

Page 25: Native Development for Windows Phone 8

WAY AHEAD™

Visual Studio Demo

Page 26: Native Development for Windows Phone 8

26

Additional Resources

Windows Phone 8 Development Internals(Whitchapel, McKenna)http://amzn.to/1a2989c

Modern C++ and Windows Store Apps(Poduri)http://amzn.to/GEW6Y4

Page 27: Native Development for Windows Phone 8

27

» Channel 9 – channel9.msdn.com» Build 2012 Sessions

channel9.msdn.com/Events/Build/2012/

» Build 2013 Sessions channel9.msdn.com/Events/Build/2013/

» Going Native channel9.msdn.com/Shows/C9-GoingNative

» C++ 11 Features in Visual C++ 11blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

Additional Resources

Page 28: Native Development for Windows Phone 8

28

» Questions & Answers

» Slides will be posted to blog: robwirving.com

Thank You!