I wanted to play around with game development the other day, so I looked around for a decent .NET Game Framework and decided to go with the MonoGame framework. After installing it, I used the MonoGame OpenGL template to create a project in Visual Studio. Unfortunately, the build failed with the "The type or namespace name 'Xna' does not exist" error, but fortunately, the solution was straightforward and is the focus of this post.
After installing the MonoGame extension and restarting Visual Studio, I created a new project with the "MonoGame Cross-Platform Desktop Application" template (a MonoGame project that uses OpenGL). After the project has been generated, I expected to build and run the project without any issue, but instead, it failed with the following errors:
I attempted to create another project using the same template, but the errors remained.
Looking at the project in Solution Explorer, I didn't notice any references, but there were two packages:
- MonoGame.Content.Builder.Task
- MonoGame.Framework.DesktopGL
Those are NuGet packages added by the template, but MonoGame.Framework.DesktopGL package seemed empty as there was nothing to expand to see what was inside the package.
So it appeared the issue was with the MonoGame.Framework.DesktopGL NuGet package.
Attempt 1 - update/reinstall the package
First I tried to update the package. We do that by right-clicking on the package and selecting "Update" from the context menu.
This opens the NuGet Manager, but the "Update" tab was empty.
Next, I tried to uninstall and reinstall the package back while still inside the NuGet Manager.
Inside the "Installed" tab, we select MonoGame.Framework.DesktopGL package and click "Uninstall".
After it was uninstalled, I had to install it back by going to the "Browse" tab, searching for "monogame desktopgl", selecting the MonoGame.Framework.DesktopGL package and clicking "Install".
However, the build failed again.
Attempt 2 - remove the package from the .nuget folder and install it again
Next, I decided to delete the MonoGame.Framework.DesktopGL package from the system. The package is located inside the .nuget
folder. This is the folder where all the downloaded NuGet packages are stored. To get there, we can right-click on the package in Solution Explorer and select "Open Folder in File Explorer" from the context menu.
Once inside the File Explorer, I moved up 2 folder levels to be inside the .nuget
folder.
We can also go there by using the following path in File Explorer:
I deleted the monogame.framework.desktopgl folder, went back to Visual Studio and then in the Solution Explorer removed the MonoGame.Framework.DesktopGL NuGet package from the project.
Now I needed to add the package back to the project using NuGet Manager. We do this by selecting the "Browse" tab, then search for "monogame desktopgl" and in the list selecting the MonoGame.Framework.DesktopGL package and clicking "Install".
After it was installed, I noticed that this time the MonoGame.Framework.DesktopGL package was showing the expand arrow next to it.
Building the solution was finally successful, giving me no more 'Xna' does not exist errors.
What was the issue?
It seems that when the first project using the MonoGame OpenGL template is created, the MonoGame.Framework.DesktopGL package is downloaded and added to the global packages .nuget
folder and in my case, it wasn't downloaded properly. When I was uninstalling/installing the package in the project, Visual Studio simply re-added the broken package from the .nuget
folder back to the project.
Conclusion
When we create a project from a template in Visual Studio, we don't expect the generated project to fail when we try to build them. But this is what happened with the MonoGame Cross-Platform Desktop Application template. The problem in my case was with the MonoGame.Framework.DesktopGL package and it was resolved by first deleting the package from the .nuget
global-packages folder and then reinstalling it back in the project.