当我将 Texture2D 作为参数传递给 class 时,此纹理为空

When I pass a Texture2D as parameter to a class, this texture is null

这是我遇到问题的代码:

namespace Menu_test
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        #region Initialization
        ContentManager content;
        Texture2D playgame;
        Texture2D exit;

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base()
        {
            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry(playgame);
            MenuEntry exitMenuEntry = new MenuEntry(exit);

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            Art.Load(content);

            playgame = Art.PlayGame;
            exit = Art.Exit;
            if (playgame==null)
                throw new ArgumentNullException();
        }


        /// <summary>
        /// Unloads graphics content for this screen.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
                               new GameplayScreen());
        }


        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
        }




        #endregion
    }
}

所以在 MainMenuScreen() 中我的 playgamenull,但在 LoadContent() 中它不是 null。基本上我想做的是将 Texture2D 作为参数传递给 MenuEntry class,但纹理甚至在传递给 class 之前就为空。 如果您想尝试 运行,可以下载完整的项目 here

感谢您的阅读。

LoadContent 在您的 MainMenuScreen 初始化后由 XNA 调用。您可能必须将 playGame 对象的使用推迟到加载纹理或在 LoadContent.

中初始化 MenuEntries 的那一刻