MonoGame Winforms - 加载内容

时间:2015-10-10 14:35:40

标签: winforms monogame

我正在使用Windows窗体为MonoGame创建一些工具。我正在使用您可以在Xbox Live论坛上找到的教程。我实现了图形设备,但我不知道如何加载内容。有人能帮助我吗?

我正在使用MonoGame 3.4和Visual Studio 2015

1 个答案:

答案 0 :(得分:1)

要加载内容,您需要ContentManager。 Monogame 3.4中的ContentManager的构造函数采用IServiceProvider实例并解析IGraphicsDeviceService以获取GraphicsDevice实例。

由于您已经实施了GraphicsDevice,因此您需要做的就是实施IGraphicsDeviceServiceIServiceProvider

我将实施ContentManager工作所必需的工作。

首先实现IGraphicsDeviceService以返回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;
}

然后实施IServiceProvider以返回IGraphicsDeviceService

public class ServiceProvider : IServiceProvider
{
    private readonly IGraphicsDeviceService deviceService;

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

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

最后,您可以初始化ContentManager的新实例。

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

请勿忘记添加对Microsoft.Xna.Framework.Content的引用。