MonoGame: Using libraries with Windows Phone projects

image

A challenge faced by the MonoGame team when they created Windows Phone 8 support was that XNA is actually included with the platform, this created a unique challenge because it was

a direct contradiction to the XNA support being provided by MonoGame itself, this caused no end of problems until a solution was found.

The answer was simple, ignore the XNA libs native to Windows Phone at build time.

*NOTE

MonoGame NuGet packages have now been released so that you don’t need to go through this headache any more, there are both full packages and binaries only releases for the main 3.0.1 release and the current dev branch.
Check them out in this post – https://darkgenesis.zenithmoon.com/monogame-nuget-packages-are-go/
Installing the binary only version of the new NuGet package will do the tasks in this post for you 😀


The Problem

If you manually reference the MonoGame framework from any project and try to use XNA components you will likely run in to the following error:

Error 1:
The type ‘Microsoft.Xna.Framework.Vector3’ exists in both
‘c:\Program Files (x86)\MonoGame\v3.0\Assemblies\WindowsPhone\x86\MonoGame.Framework.dll’ and
‘c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\WindowsPhone\v8.0\Microsoft.Xna.Framework.dll’

The compiler is basically stating there are two versions of the class used (in this case the Vector3 struct) and it cannot decide which one to use.  Using an alias wouldn’t help because they have the same name, same namespace and so on.


Solving this

If you use the MonoGame Windows Phone 8 project template, the fix is automatically applied for you but if you create your own projects and / or create libraries / components to your project that require the “MonoGame.Framework” referenced assembly then you will need to apply the fix manually, read on.

1: Open the “.csproj” of the project you are adding the MonoGame reference to.

2: Add the following block after the <Imports> section

All this does (as explained above) is tell the MSBuild task to ignore the Microsoft XNA reference libraries during the build so that only the MonoGame versions exist.

Ensure you do this for any project in your solution that references the MonoGame framework, not needed if the project isn’t.


One other thing

Another thing to note if you build your game project manually instead of using the MonoGame Windows Phone 8 template (some people just like it that way) is that there is some other additional configuration included you should be aware of, this revolves round the target platform used for builds.

If you are targeting the emulator, then the “x86” versions of the assemblies are used to build the project.

 

If you are targeting a device (or publishing to the store) then the “ARM” versions of the framework are used.

Be aware of this if you decide to go solo (see example below)


Final Example

Just to finish this post off and as a sanity check to ensure you got it right, here’s an example project file with the above section included:

%d bloggers like this: