使用 c# 在 monogame 中访问其他 class 中的 GraphicsDeviceManager

access GraphicsDeviceManager in other class in monogame with c#

我用 C# 为我的单人游戏制作了一个选项菜单。 这是我 class 初始化函数的一部分。 我想在 window 屏幕中初始化游戏。这很完美。 现在我希望我可以在我的选项中将 window 屏幕更改为全屏。 我如何才能访问图形设备管理器,它是否正常工作?

我写的时候有权限

Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;

但随后我收到错误 CS0120 C# 非静态字段、方法或 属性 需要对象引用。 而且我不知道怎么处理。

namespace Game1.Game
{
    internal class Game1 : Microsoft.Xna.Framework.Game
    {
        private InputManager mInput;
        private ScreenManager mScreenManager;
        public GraphicsDeviceManager mGraphicsDeviceMgr;
        private bool mEscapeDown;

        public Game1()
        {
            mGraphicsDeviceMgr = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";
            mEscapeDown = false;
        }
        protected override void Initialize()
        {

            IsMouseVisible = true;
            mGraphicsDeviceMgr.ApplyChanges();

            // GameEngine
            mInput = new InputManager();
            mScreenManager = new ScreenManager(mGraphicsDeviceMgr, Content, mInput);
            SoundManager.InitializeContent(Content);

            base.Initialize();

然后我有一个 class MainMenuScreen:

public sealed class MainMenuScreen : Screen
    {
        private readonly ScreenManager mScreenManager;
        private readonly List<Button> mButtons;
        private int mStatistic;
        private readonly InputManager mInput;
[...]
private void ButtonOptions()
        {
            OptionScreen optionScreen = new OptionScreen(mScreenManager, mInput);
            mScreenManager.AddScreen(optionScreen);
            optionScreen.AddLabel("Options:");

            optionScreen.AddButton("Fullscreen",ButtonFullScreen);
            optionScreen.AddButton("Back", ButtonBack);
        }
private void ButtonFullScreen()
        {
            mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
            mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;
        }

因为永远只有一个 deviceManager。 使其静态化:

public static GraphicsDeviceManager mGraphicsDeviceMgr;

这将允许从项目中的任何位置访问 mGraphicsDeviceMgr 变量:

 Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
 Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;

由于您选择的特定命名空间 "Game1.Game",为了完整起见,需要将这些行更改为完全限定名称:

 Game1.Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
 Game1.Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;