Download the UWP library to the .NET Framework application

There are a number of articles ( codeproject , blog1 , blog2 , forum ) to use the WinRT Library in the .Net Framework console application on Windows 8.

I tried it with UWP on Windows 10. But that failed. I tried my best to compile without errors, but a BadImageFormatException occurred during execution.

This is what I did.

  • Create a console application with the goal of the .NET Framework 4.6.1.
  • Edit the .csproj file to add <TargetPlatformVersion>10.0</TargetPlatformVersion>
  • Link to three libraries as shown below.
    • c: \ Program Files (x86) \ Windows Kits \ 10 \ UnionMetadata \ Windows.winmd (shows Windows Runtime 1.4)
    • c: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5.1 \ System.Runtime.WindowsRuntime.dll (shows 4.0.10.0)
    • c: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5.1 \ System.Runtime.InteropServices.WindowsRuntime.dll (shows 4.0.0.0)

Unlike Windows 8 examples, an error appears when accessing System.Runtime.dll .

The code is as follows. Please note that the code is from the Microsoft forum .

 class Program { static void Main(string[] args) { Geolocator locator = new Geolocator(); var status = Geolocator.RequestAccessAsync(); if (status != null) { Console.WriteLine("not null"); Task.Run(async () => { Geoposition pos = await locator.GetGeopositionAsync(); Console.WriteLine(pos.Coordinate.Accuracy); }); } else { Console.WriteLine("null"); } Console.ReadLine(); } 

The compilation is fine, and the console displays not null . So the call to the library itself seems wonderful. But GetGeopositionAsync raises a BadImageFormatException .

Details of the exception message are described below.

An exception is thrown: 'System.BadImageFormatException' in the mscorlib.dll file

Additional Information: Failed to load the file or assembly "System.Runtime.WindowsRuntime, Version = 4.0.10.0, Culture = neutral, PublicKeyToken = b77a5c561934e089" or one of its dependencies. Reference assemblies must not be loaded to execute. They can only be loaded into the loader context for reflection only. (Exception from HRESULT: 0x80131058)

I already tried (1) to change the build configuration to x86 / x64 / AnyCPU (2) and set Copy local to true for all links, but the same error occurred.

In my opinion, this exception means that System.Runtime.WindowsRuntime trying to load some dependent library inside, but I don't know what it is.

+7
c # windows-runtime uwp
source share
2 answers

Here are the steps that work here, pay attention to the subtle differences between yours:

Step 1 : add to the .csproj file

  <PropertyGroup> <TargetPlatformVersion>10.0</TargetPlatformVersion> </PropertyGroup> 

Step 2 : add a link to

 C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd 

Step 3 : add a link to

 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll 

Note the differences, v4.5 instead of v4.5.1, and there are no links to System.Runtime.InteropServices.WindowsRuntime.dll .

It works great here (Win10, VS2015).

Side note , a call in Geolocator will work independently:

 This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A) 

It will not be accessible from the desktop, only the types referenced here are supported on the desktop.

+8
source share

I had this error and I upgraded the version of Microsoft.NETCore.UniversalWindowsPlatform from "5.0.0" to "5.2.2" and my program started working

+1
source share

All Articles