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.
Youngjae
source share