August in France means only one thing: vacation time. Now, Christophe Nasarre, Microsoft Premier Field Engineer from Microsoft France, is back from his personal vacation with another video on WinRT development. It is time to go back to the fundamentals with this video post where you'll learn how to use WinRT APIs in your Desktop applications.
During our //Build episode, I quickly presented my WinMDiff tool that displays the differences between two versions of WinRT. The implementation is using the unmanaged reflection API of .NET to load .winmd files containing the WinRT metadata.
This is possible because WinRT is using the same metadata file format as .NET has used for years. So shouldn’t this make easier for a .NET Desktop application to use a WinRT type? It definitively helps! Like a Windows Store App, a Desktop App sees WinRT types as .NET Types and the projection layer does the rest.
First, the project must be manually changed in its XML format by adding the following PropertyGroup element:
Once this is done, reload the project and add a reference to the Windows | Core | Windows 255.255.255.255 metadata. After these changes, you can write .NET code that is using WinRT types as if they were other .NET types: IntelliSense and auto-completion will work as expected. Unfortunately, you still need to add a few references to make it compile:
Even though Visual Studio lets you write and compile code that is using WinRT types, you might end up with exceptions are runtime. The reason is simple: not all WinRT types are supposed to be callable from a Desktop application due to security and sandboxing.
For each WinRT type, the last section of the MSDN documentation tells you if it is usable from a Windows Store App and from a Desktop application. In addition, I’ve provided the DualApi Finder tool that lists the WinRT types callable from a Desktop application. It detects the types that are decorated with the DualApiPartition attribute.
Watch the video for the code sample about how to use WinRT networking and file access APIs in a Desktop application. Last but not least, don’t miss the final part that describes the pitfalls you might face.
For your convenience, here’s the video timeline (note that it will launch a new browser window):
Thank you for watching!
Original content from Christophe Nasarre; posted by MSPFE Editor Arvind Shyamsundar