为什么我不能在 Windows 通用应用程序中引用 .Net Core 1.0 程序集? [英] Why can't I reference a .Net Core 1.0 assembly in a Windows Universal app?

查看:25
本文介绍了为什么我不能在 Windows 通用应用程序中引用 .Net Core 1.0 程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在家使用 Visual Studio 2017 社区版,并尝试制作我的第一个通用 Windows 应用程序.该解决方案有两个程序集,一个用于 UI,另一个用于纯计算.一切都很好,直到我尝试从 UI 程序集引用计算程序集.它拒绝添加引用.一开始看我的阅读,我以为是目标版本不兼容的问题,但是即使我把计算的目标尽可能的低,UI的目标尽可能的高,也没有什么区别.

I'm using Visual Studio 2017 Community edition at home, and trying to make my first Universal Windows app. The solution has two assemblies, one for UI and one that is purely computational. Everything is fine until I try to reference the computational assembly from the UI assembly. It refuses to add the reference. At first, from my reading, I thought it was a matter of target version incompatibility, but even if I make the computational one's target as low as possible and the UI's target as high as possible, it makes no difference.

  • 计算程序集是一个面向 .Net Core 1.0 的 C# 类库.

UI 组件是通用 Windows,最小目标版本设置为:

The UI assesmbly is Universal Windows, with a min target version set either to:

  • Windows 10 Fall Creators Update (10.0; build 16299)"
  • Windows 10 (10.0; Build 10240)"

无论哪种方式,结果都是一样的.当我去项目>添加参考...>项目/解决方案>选择other_assembly并点击OK时,它只是说

Either way the result is the same. When I go Projects > Add Reference ... > Projects/Solution > select other_assembly and hit OK, it just says

无法添加对项目other_assembly"的引用."

"Unable to add reference to project 'other_assembly'."

没有解释,没有细节.

这里有什么障碍?我怎样才能绕过它,或者至少得到一个关于错误是什么的详细描述?

What's the obstacle here? How can I get around it, or at least get a verbose description of what the error is?

如果我将所有计算源文件复制到 UI 程序集中,则一切正常,但我希望能够支持多个平台.

If I copy all of the calculational source files into the UI assembly, everything builds OK, but I want to be able to support more than one platform.

推荐答案

所以当我们在 VS2017 中创建跨平台(例如可以在 Linux 系统上使用)类库时,我们有两个选择,一个是针对 .NET Core,另一个是针对.NET Standard.

So when we create a cross-platform (which can be used on Linux systems, for example) class library in VS2017 we have two options, one is targeting .NET Core, the other is targeting .NET Standard.

虽然 .NET Core.NET Standard 已经存在几年了,但它们仍然让我们大多数人感到困惑.幸运的是,我们有相当多的大量参考资料可以在线阅读.你可以谷歌,或者只是在 Stackoverflow 上找到最受好评的答案.

While .NET Core and .NET Standard have been around for a few years, they are still confusing most of us. Luckily we have quite a lot of references to read online. You can Google, or just find the most upvoted answers on Stackoverflow.

我想分享我在学习这个方面的经验.在我开始在我的 Ubuntu 系统上编写 .NET Core 应用程序后,我才完全理解这一点.

I would like to share my experience on learning this. I only fully understand this after I started writing .NET Core applications on my Ubuntu system.

.NET Core 是关于应用程序能够在多个平台上运行,所以你有一个 .NET Core 控制台应用程序,或者一个 ASP.NET Core 应用程序,你可以使用相同的dotnet run" 命令在 Windows 或 Ubuntu 上运行,而无需修改一行代码.

.NET Core is about the application being able to run on multiple platforms, so you have a .NET Core console application, or an ASP.NET Core application, you can run the application with the same "dotnet run" command on either Windows or Ubuntu without having to modify a single line of code.

目前 .NET Core 仅支持两种项目类型,Console 和 ASP.NET Core.UWP 仍然严格仅限于 Windows,您不能编写 UWP 应用程序并期望它在 Windows 以外的其他操作系统上运行.

Currently .NET Core only support two project types, Console and ASP.NET Core. UWP is still strictly Windows only, you can't write a UWP application and expect it to run on other OS than Windows.

另一方面,.NET Standard 是关于在不同的 .NET 应用程序之间共享类库代码 - Windows Forms、WPF、UWP、Mono、ASP.NET Core,无论它是什么项目类型. 因为它们都必须实现某些版本的 .NET Standard.这意味着,如果该库被编译为面向非常低的 .NET Standard 版本(如 1.0),则该库可以被所有人引用,是的,甚至是 WinForms(.NET 平台 4.5 或更高版本).

On the other hand, .NET Standard is about sharing class library code between different .NET applications - Windows Forms, WPF, UWP, Mono, ASP.NET Core, whatever project type it is. Because they all have to implement some version of .NET Standard. Which means that if the library is compiled to target a really low version of .NET Standard (like 1.0), that the library can be referenced by all, yes, even WinForms (.NET Platform 4.5 or higher).

那么让我们回到问题上来,如果你编译类库以面向 .NET Core,那么它只能被 .NET Core 应用程序使用,UWP 应用程序不能引用它,因为 UWP 不是跨平台的.平台(UWP 只是跨屏,运行在具有各种屏幕尺寸的不同 Windows 10 设备上;))

So let's get back to the question, if you compile the class library to target .NET Core, then it can only be used by a .NET Core application, a UWP application can't reference it because UWP is not cross-platform (UWP is only cross-screen, running on different Windows 10 devices with a variety of screen sizes;))

一个月后更新:当我写这个答案时,我从未听说过 MS 计划发布 .NET Core 3.0 在 2019 年 - 他们将在下一个主要版本中添加对 WinFormsWPF 的支持.NET Core,这意味着我的 #1 .NET Core 是关于跨平台的应用程序 会有些误导.

Update a month later: When I wrote this answer I had never heard of MS's plan on releasing the .NET Core 3.0 in 2019 - they will add support for WinForms and WPF to the next major version of .NET Core, which means my #1 point .NET Core is about application being cross-platform will be somewhat misleading.

这篇关于为什么我不能在 Windows 通用应用程序中引用 .Net Core 1.0 程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆