Calling a Windows 10 Universal Class Library from a desktop application

Cerebrate picture Cerebrate · Jul 29, 2015 · Viewed 7k times · Source

In previous development, I have used a Portable Class Library to share code between a Windows 8.1 "Metro" app and a .NET/WPF desktop app.

Are Windows 10 Universal class libraries also usable from such desktop apps, and if not, what is the recommended way to share code in this scenario?

Answer

Shahed C - MSFT picture Shahed C - MSFT · Sep 3, 2015

First, let's take a look at the first part of your question:

Are Windows 10 Universal class libraries also usable from such desktop apps?

To answer this, I created a new solution in VS2015 with the following projects:

  • WPF desktop app, named wpfApp
  • Win10 UWP app, named win10App
  • Win10 UWP class library, named win10Lib

I was then able to add the class library as a reference to the UWP app. But, I got an error message when trying to add the library reference to the desktop application.

enter image description here

(SCREENSHOT: Err msg when trying to add UWP lib as ref to desktop app)

Next, the second part of your question:

and if not, what is the recommended way to share code in this scenario?

So, I then added a new Class Library project, but not the Universal kind... I selected the Portable Class Library option under Classic desktop apps. This allowed me to choose what platform(s) I want to support, including .NET 4.6 and Windows Universal 10.0.

enter image description here

(SCREENSHOT: Add Portable Class Library)

So, now I have a new class library project in my solution:

  • classic desktop class library (portable), named portableLib

I started adding a class library reference to my WPF desktop app once again, selecting my new portableLib project. However, I got the following error message:

enter image description here

(SCREENSHOT: Error message when adding portable class library ref to WPF project)

This happened because I selected 4.6 (the default for .NET Framework version) as one of the platforms while creating the class library, but my WPF project was still targeting an older version of the .NET Framework. I fixed this by editing the WPF app's project properties and updated the target to match the class library's framework version.

Finally, I added a reference to the new portable class library to my Win10 UWP app, and it got added successfully. I can now build the solution with no errors.

Here is a screenshot of my project structure, with references. enter image description here

Hope this helps!