Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reference a .NetStandard library from my Windows 10 UWP app?

There are TONS of posts, blogs, articles, etc... explaining all of this confusing stuff with regard to .Net/.NetCore/.NetStandard and I have read many of them.

Here is the issue, I have a Windows 10 UWP app and I need to reference a library that I created. I first created the library as a NetCore library but I could not reference that from my UWP app (which is confusing because UWP uses .Net Core but the .Net Core library I created assumed this was for ASP.NET?).

So, then I tried to create the library as a PCL library and targeted ONLY Windows 10 UWP (which it then forces you to Windows 8.1 because they are the same). With this type of library I am confident I would be able to reference it from my UWP app but it caused me to lose too many .Net namespaces that I could not get the library to build.

Finally, I then selected the link, in the project properties, to target the .Net Standard instead. I selected .Net Standard 1.4 and suddenly, I had all the namespaces I needed available to me and I was able to build my library. I am also able to successfully add it as a reference to my UWP app.

However, I am getting the following 6 exceptions and they don't tell me too much about the root cause so I can fix it.

Exceptions

So, my main question is, can you reference a .Net Standard 1.4 library in your UWP app? Second, any ideas what these exceptions mean?

Thanks!

EDIT - I have uploaded both csproj files here:csproj files

like image 381
Michael Bedford Avatar asked Oct 30 '22 08:10

Michael Bedford


1 Answers

Okay, I found that answer but it was based mainly on this answer which I did not see before posting my question due to search terms (I have literally spent over a day searching and trying to find an answer).

Answer that helped:

Here is a summary:

  1. .NetStandard 1.4 is supported by UWP.

  2. By default, VS 2015 Update 3 template for a UWP project imports Microsoft.NetCore.UniversalWindowsPlatform version 5.1.0. I am sure the VS 2017 template will start off with a newer version, 5.1.0 is fairly old. So, based on the answer linked above, I updated (using NuGet) to the newest version allowed by VS 2015 Update 3 which at the time of this answer, is version 5.2.3. There is a 5.3.x version but it requires VS 2017.

  3. Finally, I modified the project.json in my UWP project to import the netstandard1.4 framework. It looks like this:

    "frameworks": {
      "uap10.0": {
        "imports": "netstandard1.4"
      }    
    

And, with those simple steps, I can get the UWP project to build and those 6 errors above go away.

Hope this helps!

like image 176
Michael Bedford Avatar answered Nov 12 '22 23:11

Michael Bedford