【问题标题】:Odd "Object reference not set to an instance of an object" involving xWinForms涉及 xWinForms 的奇怪“对象引用未设置为对象的实例”
【发布时间】:2013-11-25 23:30:37
【问题描述】:

我一直在尝试让 xWinForms 3.0 库(xna 中支持表单的库)与我的 C# XNA 游戏项目一起使用,但我一直遇到同样的问题。 我添加对我的项目的引用,放入 using 语句,声明一个 formCollection 变量,然后尝试对其进行初始化。

每当我运行项目时,我都会停在这条线上:

formCollection = new FormCollection(this.Window, Services, ref graphics);

它给了我错误:

System.NullReferenceException 未处理 Message="Object 引用未设置为对象的实例。"
来源="Microsoft.Xna.Framework" StackTrace: 在 Microsoft.Xna.Framework.Graphics.VertexShader..ctor(GraphicsDevice 图形设备,字节 [] 着色器代码) 在 Microsoft.Xna.Framework.Graphics.SpriteBatch.ConstructPlatformData() 在 Microsoft.Xna.Framework.Graphics.SpriteBatch..ctor(GraphicsDevice 图形设备) 在 xWinFormsLib.FormCollection..ctor(GameWindow 窗口、IServiceProvider 服务、GraphicsDeviceManager 和图形) 在 C:\Users\Owner\Documents\School\Year 3\Winter\Soen 中的 GameSolution.Game2.LoadContent() 390\TeamWTF_3\SourceCode\GameSolution\GameSolution\Game2.cs:第 45 行 在 Microsoft.Xna.Framework.Game.Initialize() 在 C:\Users\Owner\Documents\School\Year 3\Winter\Soen 中的 GameSolution.Game2.Initialize() 390\TeamWTF_3\SourceCode\GameSolution\GameSolution\Game2.cs:第 37 行 在 Microsoft.Xna.Framework.Game.Run() 在 C:\Users\Owner\Documents\School\Year 3\Winter\Soen 中的 GameSolution.Program.Main(String[] args) 390\TeamWTF_3\SourceCode\GameSolution\GameSolution\Program.cs:第 14 行 内部异常:

在我下载的一个使用 xWinForms 的项目中,我将以下代码放入并编译并运行没有错误。 但是当我把它放在我的项目中时,我得到了错误。 我是否在包含 dll 或其他内容方面犯了一些愚蠢的错误?我已经在这里待了好几个小时,但我似乎找不到任何会导致这种情况的东西。

using xWinFormsLib; 

public class Game2 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    FormCollection formCollection; 

    public Game2() 
    { 
        graphics = new GraphicsDeviceManager(this); 
        Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 
        // TODO: Add your initialization logic here 

        base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
        // Create a new SpriteBatch, which can be used to draw textures. 
        spriteBatch = new SpriteBatch(GraphicsDevice); 

        formCollection = new FormCollection(this.Window, Services, ref graphics); 
    } 

    protected override void Update(GameTime gameTime) 
    { 
        base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
        base.Draw(gameTime); 
    } 
} 

任何帮助将不胜感激。_。

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    我终于知道发生了什么... 看来我需要某种配置文件。 当我使用 3.1 时,该库似乎适用于 XNA 3.0 配置文件似乎在两者之间进行了某种映射。一旦我添加了文件,它就解决了问题。我仍然不完全确定为什么。

    【讨论】:

      【解决方案2】:

      我应该强调我以前没有使用过这个库。但是,查看堆栈跟踪,引发异常的方法采用 GraphicsDevice 对象。我猜该对象来自您的“图形”对象的 GraphicDevice 属性。在将其传递给 FormCollection 构造函数之前,请尝试检查该属性是否包含您期望的内容。这可能有助于为您指明正确的方向。

      【讨论】:

        猜你喜欢
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多