Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 How to debug "Unable to add reference to project x" error?

In Visual Studio 2012 in a blank new Class Library I am trying to add reference to an existing project in the solution, also a Class Library, MonoGame.Framework.Windows8 (https://github.com/mono/MonoGame), but get the error:

Unable to add reference to Project MonoGame.Framework.Windows8

I have built the existing library, MonoGame, successfully and made it a dependency of the new one. What could be the problem?

(It's extremely annoying the error message does not give a reason!)

like image 932
markmnl Avatar asked Dec 12 '12 04:12

markmnl


2 Answers

Trying to add the built dll has a reference gave a more explicit error message which has solved the problem for me, it said:

“A reference to ‘x’ could not be added. The project targets ‘.NetFramework’ while the file reference targets ‘.NetCore’. This is not a supported scenario”

The MonoGame Class Library is a Class Library (Windows Store apps) type! (The bit in parenthesis is important! Presumably the difference is the Windows Store apps type use .Net Core which is not the same as .Net Framework).

So to get it working your project must be a Class Library (Windows Store apps) which is available under the Windows Store option in Add New Project.

like image 87
markmnl Avatar answered Oct 03 '22 09:10

markmnl


I suspect the problem is to do with the 'Target Framework' in project options.

I don't know how MonoGame works for Windows 8 but when you're developing for Android the target framework options are the different versions of Android [e.g. Android 2.2 (Froyo)]. When you create a regular class library you have choices between the different versions of the .NET framework or Mono [e.g. Mono / .NET 4.0].

Unfortunately, these frameworks are not compatible with each other. You can't add a project reference for Android to a .NET framework class library and visa-versa.

The solution is to use a Portable Class Library. They are specifically designed to deal with this issue. The downside is that you will only have access to the subset of assemblies provided in the lowest denominator you choose to target.

You might also want to consider code sharing between projects using linked files. It can make maintenance a little trickier but gives you a little more control over code that should compile in both target frameworks but doesn't fit into the portable class library. Here's a related question:

Project reference vs. file links in Mono multi-target projects

The key to all of this once you understand what's going on here is how you structure your solution. With a little care you can have nearly all your code shared across different platforms.

like image 22
craftworkgames Avatar answered Oct 03 '22 10:10

craftworkgames