When I change vertical size of XNA game window to

2019-07-07 05:35发布

问题:

When I resize the game window and the viewport's height becomes 0, GC disposes of spritebatch, I think. Is this what happens? How do I prevent this?

回答1:

Form gameForm = (Form)Form.FromHandle(Window.Handle);
gameForm.MinimumSize = new System.Drawing.Size(800, 600);

Short and sweet!



回答2:

I had this same problem and some quick debugging shows that XNA calls UnloadContent and then LoadContent again to reinitialise resources; my guess it it loses the GraphicsDevice or something, hence the reload.

Creating things pertaining to the GraphicsDevice in LoadContent solves this problem.

Now, for this particular case, setting a minimum size is a good idea, but I don't know if this is a portable solution between Xbox and Windows. However, there might be other situations in which something similar occurs, where UnloadContent is called, so it's probably best to adhere to this practice.