Hey all, I had a lot of trouble with MonoGame.Forms so I made this little guide for others who might also struggle with using it in the future!
1. Add the Extension
This extension is available for Visual Studio. It contains project templates and templates to create a new MonoGame Control class:
2. Change the Namespaces
By default, the MonoGame.Forms template starts you out with the namespace “MonoGame.Forms.DX” which is not our project’s namespace.
Before:
After:
You need to change the following files’ namespaces:
- Editor
- Program.cs
- Form1.cs
- Form1.Designer.cs
- Also change the instantiation of the sample control’s namespace
- Editor.Controls
- SampleControl1.cs
- Any other controls that you add to your project from the templates
For Windows Form designer classes, you will need to right click and select “View Code” to change their namespaces:
I’m not sure if these properties’ namespaces need to be changed because it works without changing them, but I usually do:
You will probably still see this error if you try to open the form inside the designer:
You actually haven’t done anything wrong, all you need to do it build and it will open correctly.
After it is built, close the tab with the form and now open it back up. It should look something like this:
3. Add the MonoGame Configuration Folder
In order to be able to open the MGCB editor and for it to build, you will need to add a folder called “.config” to the project. The easiest way to do this is to copy the folder from an existing MonoGame project.
If you want to add it manually, then create a folder called “.config” and add a file called “dotnet-tools.json”
Inside the file, add the following text:
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-mgcb": {
"version": "3.8.1.303",
"commands": [
"mgcb"
]
},
"dotnet-mgcb-editor": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor"
]
},
"dotnet-mgcb-editor-linux": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-linux"
]
},
"dotnet-mgcb-editor-windows": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-windows"
]
},
"dotnet-mgcb-editor-mac": {
"version": "3.8.1.303",
"commands": [
"mgcb-editor-mac"
]
}
}
}
The version may vary so the best thing to do would be to copy the “.config” folder from an existing MonoGame project.
4. Add the Build Tasks
In order to get Visual Studio to build the content of the project and add it to the output folder, you need to edit the “.csproj” file. To do this, you need to unload the project by right clicking the project and choosing unload:
If the “.csproj” file doesn’t open automatically, double click the unloaded project. You can add the following tasks anywhere inside of the “Project” tag:
<!--Start of the extra build tasks for Content-->
<ItemGroup>
<None Include="Content\**" />
<PackageReference Include="MonoGame.Framework.WindowsDX" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Forms.DX" Version="3.0.0" />
</ItemGroup>
<!--End of the extra build tasks for Content-->
For example, it can go right at the top like this:
If you are using a repository system like git, comment out or delete the following lines:
The reason for taking out that line is because if you use this project in a repo and clone to a new location, it will throw this “error” even when the build would work otherwise. Not quite sure why…
Save the “.csproj” file and then reload the project by right clicking on the project and pressing “Reload Project”:
You will probably see two “Content.mgcb” files. These are actually the same file so don’t try to delete one.
If this bothers you, you can comment out or delete this line:
If you are interested in what each line is doing here is why:
This line allows you to view your content inside the Solution Explorer:
Without it, you would only see the “Content” folder with the “Content.mgcb” file inside even if there are other files or folders inside:
Before:
After:
Note: after adding content, usually the files won’t show up in the solution explorer until you unload and reload your project again. Don’t worry, they are there, Visual Studio just isn’t showing you them
These lines tell Visual Studio to make the MGCB Editor build your content:
The version might vary for you so if you want to make sure, create a DirectX MonoGame project and copy these two tasks from that projects “.csproj” file:
This line includes the MonoGame.Forms.DX package. Normally this isn’t needed but if cloning from a repo to a new location, it will think the NuGet package for MonoGame.Forms.DX is missing and you would have to install it again through NuGet manager. This does what NuGet would have done but it is a bit easier since you are already in the “.csproj” file.
This shows what it should look like if you view the NuGet package manager:
…and that’s it. Yeah, it’s a pretty long process but now you can create a Windows Form using MonoGame as a control!
Here is the short version of all the completely necessary steps:
- Copy over the “.config” folder
- Edit the “.csproj” file to add the build tasks and get rid of certain existing tasks
Thanks to @MysticRiverGames for helping me understand how to add the “.config” folder, DKR for the stack overflow answer on “missing” but not actually missing NuGet package error, and @BlizzCrafter for making this tool in the first place!
If you have any corrections or suggestions please reply!
Have a great day! -ReBuff