MonoGame Winforms - 加载内容

MonoGame Winforms - Load Content

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

我正在使用 MonoGame 3.4 和 Visual Studio 2015

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

既然您已经实施了 GraphicsDevice,您需要做的就是实施 IGraphicsDeviceServiceIServiceProvider

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

先实现IGraphicsDeviceService到returnGraphicsDevice

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到returnIGraphicsDeviceService

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 的引用。