Alessandro Lioi
Alessandro Lioi

Reputation: 63

MonoGame Winforms - Load Content

I am creating some tools for MonoGame using Windows Forms. I'm using the tutorial that you can found on the Xbox Live forums. I implemented the Graphics Device but I don't know how to Load Content. Can someone help me?

I'm using MonoGame 3.4 and Visual Studio 2015

Upvotes: 1

Views: 489

Answers (1)

GMich
GMich

Reputation: 577

To load content you'll need the ContentManager. The ContentManager's constructor in Monogame 3.4 takes an IServiceProvider instance and resolves the IGraphicsDeviceService to get the GraphicsDevice instance.

Since you have already implemented the GraphicsDevice, all you need to do is implement the IGraphicsDeviceService and IServiceProvider .

I'll implement just the necessary for the ContentManager to work.

First implement the IGraphicsDeviceService to return the GraphicsDevice.

public class DeviceManager : IGraphicsDeviceService
{
    public DeviceManager(GraphicsDevice device)
    {
        GraphicsDevice = device;
    }
    public GraphicsDevice GraphicsDevice
    {
        get;
    }
    public event EventHandler<EventArgs> DeviceCreated;
    public event EventHandler<EventArgs> DeviceDisposing;
    public event EventHandler<EventArgs> DeviceReset;
    public event EventHandler<EventArgs> DeviceResetting;
}

Then implement the IServiceProvider to return the IGraphicsDeviceService

public class ServiceProvider : IServiceProvider
{
    private readonly IGraphicsDeviceService deviceService;

    public ServiceProvider(IGraphicsDeviceService deviceService)
    {
        this.deviceService = deviceService;
    }

    public object GetService(Type serviceType)
    {
        return deviceService;
    }
}

and finally you can initialize a new instance of the ContentManager.

 var content = new ContentManager(
                  new ServiceProvider(
                       new DeviceManager(graphicsDevice)));

Don't forget to add a reference to Microsoft.Xna.Framework.Content.

Upvotes: 1

Related Questions