Create MonoGame project in Visual Studio without using installers

In last post, I presented how to solve “Unable to load freetype6.dll” issue during MonoGame project compilation process. I mentioned that the MonoGame SDK packages are supposed to work out-of-box via installers, but not currently due to chained dependencies.

Well, there are more on this. After I solved the compilation issue, everything went through well. However, when I ran the game binaries, I got (yet) another exception:

System.IO.FileNotFoundException: Could not load file or assembly ‘System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies. The system cannot find the file specified.

Wow, this is just wonderful. Why my games, which are Windows desktop applications, need System.Runtime.WindowsRuntime? Isn’t the assembly only required by Universal Windows Platform (or Microsoft Store) apps? What happened ?

Turns out, for some reason, the MonoGame project template my games are using points the MonoGame framework reference to the assembly for Universal Windows Platform (located in a directory named WindowsUniversal). Of course, I didn’t change the project type at all. All of this can be seen from Properties window in Visual Studio: just right-click the MonoGame entry from References in Solution Explorer window and choose Properties. Oddly, no matter how I tried to remove it and re-add MonoGame framework from Add Reference dialog, and no matter which backend I chose, it always results in adding the one for Universal Windows Platform instead. I guess the MonoGame project template may have something not correctly implemented, either.

So, the MonoGame project template is a no-go, and I cannot open or compile my game projects without MonoGame SDK being installed (Visual Studio will complain and refuse to load projects, due to the lack of MonoGame MSBuild targets). Instead of looking for the last known working MonoGame SDK version (which I really don’t want to do), I decided to look at the options for manual project setup. I noticed MonoGame has NuGet packages released along with installers, so I went on to experiment and see if I can build a workable MonoGame project without using customized project templates. I succeeded.

Here are the steps:

  1. I know it is a little bit annoying, but you still have to solve “Unable to load freetype6.dll” issue using one of the three ways introduced in the last post. It still exists even if we use our own project setup, as MonoGame requires to have content pipeline built in order to run the game
  2. Open Visual Studio and create a C# Console app with your choice of .NET Framework version (should be at least 4.5). Windows Forms or other types of app templates also work, but later on you probably have to remove unnecessary assembly references on your own
  3. Go to project properties (by right-clicking project name and choose Properties). In Application tab, change Output type to Windows Application. This will elinimate the console window when game is running
  4. Install the following packages from NuGet:
    • MonoGame.Framework.WindowsDX or MonoGame.Framework.WindowsGL (OpenGL and Portable / PCL ones may also work, but I have not tried them yet)
    • MonoGame.Content.Builder (contains MGCB.exe)
  5. Add XNA / MonoGame code, and use MonoGame Pipeline Tool to build content pipeline. Add generated .mgcb files into the project
  6. VERY important! Change Build Action of all .mgcb files to MonoGameContentReference in Properties window (open by right-clicking corresponding files and choose Properties)
    • If MonoGameContentReference option was not shown in the list (it happened to me), you may need to reload the project.
  7. Now you should be able to simply build the project and run

The only downside for this is that .mgcb files cannot be opened in MonoGame Pipeline Tool directly from Visual Studio, though this can be easily solved by alternative means.