我正在尝试将简单的 XNA 应用程序嵌入到 Winforms MDI 应用程序中,但我似乎遇到了问题。我正在遵循 http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1 的示例我不确定我做错了什么。
在我的 MDI 父级中,我通过以下方式实例化渲染表单:
private void MainForm_Load(object sender, EventArgs e)
{
var render = new RenderForm();
render.MdiParent = this;
render.Show();
}
我的渲染表单的代码是:
public class RenderForm : Form
{
private XnaRenderer _renderer;
protected override void OnCreateControl()
{
if (!DesignMode)
_renderer = new XnaRenderer(Handle, ClientSize.Width, ClientSize.Height);
base.OnCreateControl();
}
protected override void OnPaint(PaintEventArgs e)
{
_renderer.RenderScene(null);
}
}
因此,当创建表单时,它会尝试创建我的 XnaRenderer
类的实例。 ClientSize.Width
为 284,ClientSize.Height
为 261,并且 Handle
看起来有效。构造函数的代码是:
public XnaRenderer(IntPtr windowHandle, int width, int height)
{
_graphicsService = new GraphicsDeviceService(windowHandle, width, height);
SetViewport(width, height);
}
GraphicsDeviceService
类本质上与示例代码中的类相同,但它不是单例。构造函数的代码是:
public GraphicsDeviceService(IntPtr windowHandle, int width, int height)
{
_presentationParams = new PresentationParameters
{
BackBufferFormat = SurfaceFormat.Color,
BackBufferHeight = Math.Max(height, 1),
BackBufferWidth = Math.Max(width, 1),
DepthStencilFormat = DepthFormat.Depth24,
DeviceWindowHandle = windowHandle,
PresentationInterval = PresentInterval.Immediate
};
_graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach,
_presentationParams);
}
但是,当实例化 GraphicsDevice
对象时,我得到以下 InvalidOperationException
:
An unexpected error has occurred.
没有更多的异常消息,也没有内部异常,这使得在没有大量 XNA 知识的情况下很难调试。
有人看到我做错了什么吗?
最佳答案
明白了!
在构建演示参数时,我需要添加 IsFullScreen = false
。
如果它给出了一个很好的异常消息,会更容易弄清楚
关于c# - 为什么创建 GraphicsDevice 会导致实例化时出现 "unexpected error"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594647/