本文介绍了UWP加载到库的.NET Framework应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些文章(的,,的,的)使用的WinRT库到Windows 8 .Net框架控制台应用程序。

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

我UWP试了一下在Windows 10,但未能做到。我挣扎着,没有错误编译,但它在运行时发生 BadImageFormatException

I tried it with UWP in Windows 10. But failed to make it. I struggled to compile without error, but it occurred BadImageFormatException in runtime.

这就是我所做的。


  1. 创建与.NET框架4.6.1目标控制台应用程序。

  2. 编辑.csproj的文件来添加< TargetPlatformVersion> 10.0 LT; / TargetPlatformVersion>

  3. 作为参考下面三个库。


    • C:\Program文件(x86)\Windows Kits\10\UnionMetadata\Windows.winmd(显示Windows运行时1.4)

    • C:\Program文件(x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll(节目4.0.10.0)

    • C:\Program文件(x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.InteropServices.WindowsRuntime.dll(节目4.0.0.0)

  1. Create Console application with .NET Framework 4.6.1 target.
  2. Edit .csproj file to add <TargetPlatformVersion>10.0</TargetPlatformVersion>
  3. Reference three libraries as 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)

在相反到Windows 8的例子,有错误时, System.Runtime.dll 引用。

In contrary to Windows 8 examples, there is error when System.Runtime.dll is referenced.

中的代码如下所示。注意,该代码是从。

The code is as below. Note that the code is from 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();
    }



编译是确定的,而不为空显示在控制台中。因此,库本身的调用似乎罚款。但 GetGeopositionAsync 事业 BadImageFormatException

Compilation is OK, and not null is displayed in console. So, invocation of library itself seems fine. But GetGeopositionAsync cause BadImageFormatException.

异常消息的细节。是如下

The exception message detail is as below.

抛出异常:System.BadImageFormatException在mscorlib.dll

其他信息:无法加载文件或程序集System.Runtime.WindowsRuntime,版本= 4.0.10.0,文化=中性公钥= b77a5c561934e089或它的一个依赖。参考组件不应该被加载执行。它们只能在只反射装载机上下文载入。 (异常来自HRESULT:0x80131058)

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

我已经尝试过(1)更改构建配置为在x86 / x64 /值为anycpu (2),并设置复制本地以真正的所有引用,但同样的错误发生。

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

在我看来,这异常是说 System.Runtime.WindowsRuntime 试图在内部加载一些依赖库,但我。不知道是什么

In my opinion, this exception is saying System.Runtime.WindowsRuntime tries to load some dependent library internally but I don't know what it is.

推荐答案

下面是工作在这里的步骤,注意你们之间的细微差别:

Here are the steps working over here, note the subtle differences between yours:

第1步:加入的.csproj文件

Step 1: Add to .csproj file

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



第2步:添加引用

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

第3步:添加引用

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

请注意的差异,而不是V4.5 V4.5.1,并没有引用 System.Runtime.InteropServices .WindowsRuntime.dll

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

这工作得很好这里(Win10,VS2015)。

This works fine here (Win10, VS2015).

边注后,打电话给Geolocator不管会失败:

Side note, call to Geolocator will fail regardless:

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



这不会是从桌面内的

这篇关于UWP加载到库的.NET Framework应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:05