将 UWP 库加载到 .NET Framework 应用程序中 [英] Load UWP library into .NET Framework app
问题描述
有很多文章(codeproject、blog1、blog2, forum) 将 WinRT 库用于 Windows 8 中的 .Net Framework 控制台应用程序.
There are a number of articles(codeproject, blog1, blog2, forum) to use WinRT library into .Net Framework console application in Windows 8.
我在 Windows 10 中使用 UWP 尝试过.但未能成功.我努力编译没有错误,但它在运行时发生 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.
这就是我所做的.
- 使用 .NET Framework 4.6.1 目标创建控制台应用程序.
- 编辑 .csproj 文件以添加
10.0 - 参考以下三个库.
- c:Program Files (x86)Windows Kits10UnionMetadataWindows.winmd(显示 Windows 运行时 1.4)
- c:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5.1System.Runtime.WindowsRuntime.dll(显示 4.0.10.0)
- c:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5.1System.Runtime.InteropServices.WindowsRuntime.dll(显示 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();
}
编译正常,控制台显示not null
.所以,调用库本身似乎没问题.但是 GetGeopositionAsync
导致 BadImageFormatException
.
Compilation is OK, and not null
is displayed in console. So, invocation of library itself seems fine. But GetGeopositionAsync
cause BadImageFormatException
.
异常信息详情如下.
抛出异常:mscorlib.dll 中的System.BadImageFormatException"
Exception thrown: 'System.BadImageFormatException' in mscorlib.dll
附加信息:无法加载文件或程序集System.Runtime.WindowsRuntime,Version=4.0.10.0,Culture=neutral,PublicKeyToken=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) 并将 Copy Local 设置为 true 对所有引用,但同样的错误发生了.
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 Kits10UnionMetadataWindows.winmd
第 3 步:添加对
C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5System.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 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!