问题
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.
The computational assembly is a C# class library targeted for .Net Core 1.0.
The UI assesmbly is Universal Windows, with a min target version set either to:
- "Windows 10 Fall Creators Update (10.0; build 16299)" or
- "Windows 10 (10.0; Build 10240)"
Either way the result is the same. When I go Projects > Add Reference ... > Projects/Solution > select other_assembly and hit OK, it just says
"Unable to add reference to project 'other_assembly'."
No explanation, no details.
What's the obstacle here? How can I get around it, or at least get a verbose description of what the error is?
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.
回答1:
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
.
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.
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 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.
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.
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).
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;))
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.
回答2:
Please check the following screenshot, UWP is subset of .NET Core. It also has a number of APIs that are unique to UWP. UWP apps can be .NET core apps, but the reverse is not necessarily true. Not all .NET core apps are UWP apps. UWP is only for the windows ecosystem.
As @magicandre1981 said, the better way is that porting .netcore class library to .net standard 2.0 that could be used uwp min target version to 16299. For .net standard please refer this document.
来源:https://stackoverflow.com/questions/53512852/why-cant-i-reference-a-net-core-1-0-assembly-in-a-windows-universal-app