The .NET Framework is an API that can be used from server applications, Windows desktop applications, and is available for Windows Store apps. For Windows Store apps just a subset of the framework is available. The Windows Runtime (WinRT) is an API for Windows Store apps. However, some parts of it cannot be used from Windows Store apps, only from Windows desktop apps. The Visual Studio 2012 templates doesn’t give you a direct way to reference this runtime. This article shows how this can be done.
WinRT APIs
Some of the APIs from the Windows Runtime (WinRT) cannot be called from Windows Store apps, they are only available for Windows desktop apps. The documentation of the PackageManager class (namespace Windows.Management.Deployment) shows that this API is only available for desktop apps.
References with Windows Store Apps
Creating a Windows Store C# project, the Reference Manager shows a Windows category along with Assemblies, Solution, and Browse:
References with Windows Desktop Apps
Creating a desktop application, the Reference Manager lists Assemblies, Solution, COM, and Browse. The Windows category is missing.
The way how to add the missing Windows category cannot be found easily. I found it by comparing the project file of a Windows store application with the project file of a console application and found this missing TargetPlatformVersion element that needs to be defined within the PropertyGroup element.
<TargetPlatformVersion>8.0</TargetPlatformVersion>
This element can be easily added from within Visual Studio by selecting the project in Solution Explorer, using the context menu Unload Project which gives a new context menu to edit the project file.
With the RTM version of Windows Store project templates, this element is no longer used. Maybe the project type GUIDs make the difference now. However, using the TargetPlatformVersion element is documented in the MSDN library. See Windows tab –> Extension Subgroup for more information.
With this change, the Windows tab is available in the Reference Manager:
There’s still something missing. Compiling code including the WinRT API gives clear errors that the System.Runtime assembly is missing. This assembly needs to be referenced. This assembly is not in the default list of assemblies in the Reference Manager, but you can browse for it in the directory C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades, or add it directly to the project file.
Now the PackageManager from the Windows.Management.Deployment namespace together with the Package class from the Windows.ApplicationModel namespace can be used to show information about installed Windows Store apps.
var pm = new PackageManager(); IEnumerable<Package> packages = pm.FindPackages(); foreach (var package in packages) { try { Console.WriteLine("Architecture: {0}", package.Id.Architecture.ToString()); Console.WriteLine("Family: {0}", package.Id.FamilyName); Console.WriteLine("Full name: {0}", package.Id.FullName); Console.WriteLine("Name: {0}", package.Id.Name); Console.WriteLine("Publisher: {0}", package.Id.Publisher); Console.WriteLine("Publisher Id: {0}", package.Id.PublisherId); if (package.InstalledLocation != null) Console.WriteLine(package.InstalledLocation.Path); Console.WriteLine(); } catch (FileNotFoundException ex) { Console.WriteLine("{0}, file: {1}", ex.Message, ex.FileName); } }
Thís API also requires administrative privileges, that’s why I’m adding an application manifest file to the project setting the level attribute of the requestedExecutionLevel element to requireAdministrator.
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo>
Now the application runs successfully showing installed Windows Store apps.
More information about the Windows Runtime can be found in my new book Professional C# 2012, and of course in my workshops.
Christian
The data needs to be synchronized. This is the same with Windows Live Writer. Sometimes I start writing several blog posts, continue working on it, and publishing it on other days.
Posted by: rate professor | 03/01/2013 at 07:25 PM
Thanks for this discussion. I have read articles about this subject but yours explains the topic clearly.
Posted by: Sam Royerson | 03/23/2013 at 12:51 AM