c# - 将 UWP 库加载到 .NET Framework 应用中

标签 c# .net windows-runtime uwp

有许多文章(codeprojectblog1blog2forum)将 WinRT 库用于 Windows 8 中的 .Net Framework 控制台应用程序。

我在 Windows 10 中使用 UWP 进行了尝试。但未能成功。我努力编译没有错误,但它发生了 BadImageFormatException在运行时。

这是我做的。

  1. 使用 .NET Framework 4.6.1 目标创建控制台应用程序。
  2. 编辑 .csproj 文件以添加 <TargetPlatformVersion>10.0</TargetPlatformVersion>
  3. 引用以下三个库。
    • c:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd(显示 Windows 运行时 1.4)
    • c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll(显示 4.0.10.0)
    • c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.InteropServices.WindowsRuntime.dll(显示 4.0.0.0)

与 Windows 8 示例相反,System.Runtime.dll 时出现错误被引用。

代码如下。注意代码来自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 .

异常消息详情如下。

Exception thrown: 'System.BadImageFormatException' in mscorlib.dll

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,但同样的错误发生了。

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

最佳答案

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

第 1 步:添加到 .csproj 文件

  <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 的引用。

这在这里工作正常(Win10、VS2015)。

旁注,无论如何调用 Geolocator 都会失败:

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

它在桌面上不可用,only types referenced here are supported in desktop.

关于c# - 将 UWP 库加载到 .NET Framework 应用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34547285/

相关文章:

.net - 通过使用不同的项目配置来定位多个 .net 框架版本

c# - WinRt 中的反射获取继承类的参数

c# - 如何最好地将字节数组从 C# 获取到 C++ WinRT 组件

c# - Attempted to read or write protected memory exception occurs w/o debugging but not not w/调试

c# - 如何确定我的应用程序是否处于事件状态(有焦点)

.net - Entity Framework 4 使用查找引用保存实体克隆的问题

c# - 将 ThumbToolTip 添加到 slider

c# - 在运行时创建的控件的访问值

c# - 是否可以在 IE 和 Word 附加组件之间共享 ASP.NET session cookie

c# - C#中的IF实现