C# 窗口的可视区域?

C# 窗口的可视区域?,c#,visual-studio-2013,null,xna,C#,Visual Studio 2013,Null,Xna,嗨,我需要找出我的游戏窗口的观看区域的大小。我发现这个代码: int height = GraphicsDevice.Viewport.Height; 但返回此错误: 对象引用未设置为对象的实例 有谁能帮我解决这个问题吗?感谢并抱歉我的英语高度看起来可能是一个类或其他东西,因为它的大小写,但最大的线索是错误部分,它说的是对象引用。末尾的.Height获取视口的高度对象。您需要访问对象的属性/成员变量(可能命名为height;找到它的最简单方法是代码完成)才能将其分配给变量。我想说,Graphi

嗨,我需要找出我的游戏窗口的观看区域的大小。我发现这个代码:

int height = GraphicsDevice.Viewport.Height;
但返回此错误: 对象引用未设置为对象的实例


有谁能帮我解决这个问题吗?感谢并抱歉我的英语

高度看起来可能是一个类或其他东西,因为它的大小写,但最大的线索是错误部分,它说的是对象引用。末尾的.Height获取视口的高度对象。您需要访问对象的属性/成员变量(可能命名为height;找到它的最简单方法是代码完成)才能将其分配给变量。

我想说,
GraphicsDevice.Viewport.height
此时为空,尚未初始化
GraphicsDevice
位于
Microsoft.Xna.Framework.Graphics
命名空间内

如果您是从游戏类继承,请尝试:

this.GraphicsDevice.Viewport.Height
或与

this.Game.GraphicsDevice.Viewport.Height
你想在哪里填充这些整数

添加了示例

using Microsoft.Xna.Framework.Graphics;
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    public int screenWidth;
    public int screenHeight;

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

    }

    protected override void Initialize()
    {
        screenWidth = GraphicsDevice.Viewport.Width;
        screenHeight = GraphicsDevice.Viewport.Height;
    }
}