[solved] ContentManager problem with GraphicsDevice when run under WPF

I am currently trying to get a MonoGame-based engine to run under WPF. So far, things are working out nicely, based on the WPFInterop sample as shown here:

http://panthernet.ru/en/component/jdownloads/viewdownload/4-graphx/3-monogame-wpf-interop-sample

However, loading assets such as SpriteFonts via the content manager does not work so far, the exception thrown complains being “No Graphics Device Service.” This issue has been discussed already here:

As far as I can see, the problem has not been solved, though. The obvious problem seems to be that the ContentManager does not get the reference to the GraphicsDevice. So I tried to fix this via:

var container = new ServiceContainer();
container.AddService(typeof(GraphicsDevice), _graphicsDevice);
_content = new ContentManager(container);

Now, I get the error message: “No Graphics Device Service”. Problem is now, that for a construction of a IGraphicsDeviceService to add to the container, I would need a instance of Game, wouldn’t I?

I have the feeling the solution is not far off, but I do not seem to work it out. I am happy about any suggestions!

You need to create a dummy graphics device manager:

internal class DummyGraphicsDeviceManager : IGraphicsDeviceService
{
  public GraphicsDevice GraphicsDevice { get; private set; }

  // Not used:
  public event EventHandler<EventArgs> DeviceCreated;
  public event EventHandler<EventArgs> DeviceDisposing;
  public event EventHandler<EventArgs> DeviceReset;
  public event EventHandler<EventArgs> DeviceResetting;

  public DummyGraphicsDeviceManager(GraphicsDevice graphicsDevice)
  {
    GraphicsDevice = graphicsDevice;
  }
}

Then add it to the services:

container.AddService(typeof(IGraphicsDeviceService), _graphicsDevice);
1 Like

Thanks a lot, it works! But I am still a little confused: I am using a already existing GraphicsDevice to initialize a GraphicsDeviceManager, which then provides the interface for my ContentManager?

I am using a already existing GraphicsDevice to initialize a GraphicsDeviceManager, which then provides the interface for my ContentManager?

Sorry, I don’t understand this questions.

I will try again: So, first, I am creating a GraphicsDevice, e.g., using the default adapter and one of the two possible profiles. Then, I hand a reference of this GraphicsDevice to the (dummy) GraphicsDeviceManager, which I thought was there to, well, handle all my GraphicsDevice, in case I have multiple.

So, basically, I am confused, since I thought that hierarchy-wise, I would first have to create a GraphicsDeviceManager, before even thinking about a GraphicsDevice. But that’s apparently not correct.

Thanks for the clarification.

The purpose of the IGraphicsDeviceService interface is:

Defines a mechanism for retrieving GraphicsDevice objects.

The DummyGraphicsDeviceManager fulfills only this purpose at the moment. It enables other services, like the ContentManager, to get a hold of the GraphicsDevice.

If you want, you can move your custom GraphicsDevice creation code into the DummyGraphicsDeviceManager. One could argue that this would be a cleaner design.

Ah, okay, got it. Thanks again!

Please see Are the any samples of Monogame in WPF with a working contentmanager? for a complete working version of the MonoGame engine running inside a WPF window along with the ContentManager

Nice. My version is running now, too.