当窗口不在焦点时,XNA 添加睡眠!我前段时间解决了这个问题,覆盖了 Game 类并改变了 Game 类理解其形式是否处于活动状态的方式。
据我所知,没有办法在不使用代码修改 Game 类行为的情况下禁用此行为。
特别是,我前段时间发现的方式是一个真正的黑客\怪癖!非常不干净的解决方案,但我发现的唯一方法。
public class MyGame
{
private MethodInfo pActivate;
public MyGame()
{
// We need to access base HostActivate method, that unfortunally, is private!
// We need to use reflection then, of course this method is an hack and not a real solution!
// Ask Microsoft for a better implementation of their class!
this.pActivate = typeof(Game).GetMethod("HostActivated", BindingFlags.NonPublic | BindingFlags.Instance);
}
protected sealed override void OnDeactivated(object sender, EventArgs args)
{
base.OnDeactivated(sender, args);
// Ok, the game form was deactivated, we need to make it believe the form was activated just after deactivation.
if (!base.Active)
{
// Force activation by calling base.HostActivate private methods.
this.pActivate.Invoke(this, new object[] { sender, args });
}
}
}