Building Windows 8 Metro Apps in C# and XAML

Quick tour around what Metro programming means for .NET programmers. Example for WPF and SL changes and how to integrate your app in the Windows 8 Metro Style Apps.

METRO

Metro is the new name of application name to run on Windows 8. Applications are Touch UI enabled, full screen (also called immersive). Microsoft provides a user interface guidelines. Windows 8 can run on samll cpu/memory/disk. It exists an application store. Security aspect is part of the game. Planning to migrate WPF applications to Metro need a lot of work.

XAML and Codebehind

Built-in controls are build using XAML. Extended Application Model Language. It covers elemnts and properties, also event handlers. It exists user controls. XAML uses namespaces. Elements correspond to objects. XAML is a tree description of objects. Metro and WPF is defferent. In WPF, XAML elements are .NET objects ; in Metro, XAML elements are WinRT objects, the new native API. For C# developpers, CLR just make use of RCW (runtime com wrappers) to make .NET types to WinRT objects. So XAML are usable from C++ or JS and C#. WinRT objects are esay to use in C#.

Asynchronous APIs

Metro applications must be highly responsive : fast and fluid. They should be responsiveness. Motion should be natural and fluid. Metro starting screen enables to user to scrool. Sliding applications requires performance. The motion should be smooth and it is hard to achieve. Repainting moving items is frequent. The natural feeling of motion should be guaranted. WinRT allows this with an animation system. WinRT avoid blocking APIs. Example, reading a file is not ablocking operation like ReadFile or fread() because it blocks the UI thread. Instead there are non blocking alternatives and APIs must return in less than 50 ms (<50 ms). WinRT asynchronous patterns is provided throught C# async and await keywords.

Example via new Geolocator()

IAsyncOperation interfaces are transposed into async C# pattern. Private async void Button_Click… objGeoposition = locator.GetGeopositionAsync(); C# and await keyword do the job easy. It is easy to do multiple operations without pain, but don’t disconnect your brain. It is a language extension support.

Files and Networking

A classical windows application has access to files and network resources on the current users. Computer subverting occurs sometimes like command&control servers. Metro has changed the model. The application have acces to limited resources like a local application folder; Fo the others, it requires “capabilities”. Example are Documents, Music, library acess, removable storage, outbound internet access, local network, user credentials, inbound connections, devices, location. Applications need to request for capabilities. It is linked with the installation features provided by the store : requested and granted at installation. There are few documentation about this for the moment. Runtime failure will occur if an application try to use something not previously declared.

Visual Studio 11 Dev Preview

Package.appxmanifest file enable to declare capabilities. There checkboxes for each capabilities. When the user accepts and let your app to be installed, it can later, disable some features requested. Be carefull of that.

Integration with Win8 UI

The start screen shows application icon. Application can share data. Metro offers a way to do sharing. Users can also search inside application if they have declared a contract. Contracts are for Share, search, etc. Application bar is a minimal menu with commands. Part of the Win8 application development is to integrate with the new metro style UI. Snapping allows applications to be sticked, flipped and switched and then, application layout is changed. Applications views are differents and are provided by the application.

WinRT and .NET

Built-ins XAML objects. WinRT objets are easy to use with .NET. Types are automatically mapped to .NET world. Collections are mapped with familiar interfaces. But something it differs ; examples are streams witch are not wrapped automatically. WinRT is completely asynchronous. Wrappers and located in System.Runtime.WindowsRuntime.dll. It exists synchronous APIs but be carefull not to use theses blocking methods in the UI thread because your app will be terminated by Metro… WinRT exposes buffers. Objects lifecycle is managed via COM and reference counting and not with a garbage collector. Be aware of circular references. .NET GC will release COM references. WinRT provide method like Marshal.ReleaseComObject. Some .NET features are reduced or missing. You should look to the WinRT equivalence to .NET methods. Porting existing .NET code to Metro is a major challenge.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: