我试图在MonoGame v3.6中连接Autofac v4.6.2。
这就是我在program.cs中的内容
public static class Program
{
private static IContainer Container { get; set; }
[STAThread]
static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<TheGame>().InstancePerLifetimeScope().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
builder.RegisterType<GraphicsDeviceManager>().InstancePerLifetimeScope().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
builder.RegisterType<GraphicsAdapter>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<GraphicsDevice>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<GraphicsProfile>().AsSelf().InstancePerLifetimeScope();
builder.Register((c, p) => GraphicsProfile.HiDef).As<GraphicsProfile>();
builder.RegisterType<PresentationParameters>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<EditorCamera>().As<ICamera>().InstancePerLifetimeScope();
Container = builder.Build();
using (var scope = Container.BeginLifetimeScope())
{
var game = Container.Resolve<TheGame>();
game.Run();
}
}
}
当我运行此操作时,在尝试解析TheGame
时,GraphicsDevice
类中会抛出空引用异常,因为GraphicsAdapter
为空。< / p>
这是GraphicsDevice
类构造函数:
public GraphicsDevice(GraphicsAdapter adapter, GraphicsProfile graphicsProfile, PresentationParameters presentationParameters);
GraphicsAdapter
类构造函数:
public GraphicsAdapter();
我错过了什么?如果有人能在这里帮助我,我将不胜感激!
(运行VS2017社区)
编辑:
我所有魔法所涉及的游戏类看起来像这样: 公共课TheGame:游戏 { GraphicsDeviceManager图形;
// Lots of members declared
internal TheGame()
{
// Be default this is public, but hidden for autofac integration
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
public TheGame(ICamera camera, GraphicsDeviceManager graphicsDeviceManager)
{
Content.RootDirectory = "Content";
graphics = graphicsDeviceManager;
this.camera = camera;
}
// ...
GraphicsDevice
是GraphicsDeviceManager
public class GraphicsDeviceManager : IGraphicsDeviceService, IDisposable, IGraphicsDeviceManager
{
public static readonly int DefaultBackBufferWidth;
public static readonly int DefaultBackBufferHeight;
public GraphicsDeviceManager(Game game);
~GraphicsDeviceManager();
// ....
public bool HardwareModeSwitch { get; set; }
public GraphicsDevice GraphicsDevice { get; }
public bool IsFullScreen { get; set; }
public GraphicsProfile GraphicsProfile { get; set; }
public event EventHandler<PreparingDeviceSettingsEventArgs> PreparingDeviceSettings;
// ...
}