设计时的 UWP:"Could not load file or assembly" 用于各种 NuGet 包

UWP at design-time: "Could not load file or assembly" for various NuGet packages

我正在使用 Visual Studio 2017 v15.3.3。我的UWP项目目标是Windows Creators Update 15063,我的Nuget包如下:

我的解决方案构建没有错误,并且运行良好,但是由于我的代码隐藏 and/or ViewModel 代码引用了 NuGet 包,我在 UWP 应用程序项目的设计时遇到了错误.这就是我所看到的:

这个问题并不是 System.Reactive 独有的。我在 Newtonsoft.Json v10.0.3 包中遇到了一个不同的(但看似相关的)错误:

Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The bound assembly has a version that is lower than that of the request. (Exception from HRESULT: 0x80132000)

有趣的是,将 System.Reactive 降级到 v3.0.0 并没有解决问题:错误声称系统找不到 v3.0.0 程序集.

最新的 AutoMapper 软件包 (v6.1.1) 存在类似问题

重现问题:

  1. 创建一个新的UWP项目(目标随便什么,好像没关系)
  2. 新建一个UserControl
  3. 在此 UserControl 的构造函数中,添加一些引用任何这些包的代码,例如 var s = new Subject<int>(); 并添加 using System.Reactive.Subjects;
  4. 将该用户控件添加为 MainPage.xaml
  5. 中的 XAML 元素
  6. 编译并注意错误

...或查看此示例 here

令人沮丧的是,这 3 个大包(System.Reactive、Newtonsoft.Json、AutoMapper)几周前在 Visual Studio 2015 年时在我的 UWP 项目中都运行良好在切换到 VS 2017 之前。

到目前为止唯一有效的方法是将 Newtonsoft.Json 从最新的 v10.0.3 降级到 v9.0.1(降级到 v10.0.2 或 10.0.1 无效)。不幸的是我不能降级 System.Reactive 因为他们有低于 3.0

的未列出版本

我还尝试修复我的 VS 安装,以及安装 mobile/xamarin 组件。

任何解决方法或见解将不胜感激,因为这对我来说是一个阻碍问题,谢谢!

问题是 System.Reactive3.0.0 版本与 UWP 的兼容性不是很好。我通过在包管理器控制台中使用以下命令更新到 System.Reactive 4.0.0-preview 来解决这个问题。

Install-Package System.Reactive -Version 4.0.0-preview00001

您也可以在 xaml 设计器中禁用项目代码来解决这个问题。更多可以参考Debugging or Disabling Project Code in XAML Designer.

In many cases, unhandled exceptions in the XAML designer can be caused by project code attempting to access properties or methods which return different values or work in different ways when your application is running in the designer. You can resolve these exceptions by debugging the project code in another instance of Visual Studio, or temporarily prevent them by disabling project code in the designer.

更新

对于Newtonsoft.Json,WinRT 组件似乎未能找到Newton.Json 的正确程序集。暂时的解决方法是手动添加 Newtonsoft.json.dll 文件。您可以通过以下步骤实现此目的:

右键引用->添加引用->浏览...->找到C:\Users.nuget\packages\Newtonsoft.Json.0.3\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.json.dll->单击添加按钮。 重建您的项目和 运行。这个错误应该消失了。\

您也可以使用上述方法解决AutoMapper问题。