我正在为Windows Phone 7开发一款游戏,我使用的是SLXNA(Silvelight + XNA)版本和我所拥有的一切,问题是导航游戏页面(GamePage.xaml)需要很多时间,我想制作一个显示"正在加载.."的页面,因为应用程序会一直停留在原地,直到您看到游戏页面。
感谢您的回答。问候
您有几个选择:
- 线
- 后台工作者
- 异步代码
这实际上取决于您希望在哪里进行加载。是游戏循环还是SL页面。 XNA 线程示例:
private Thread thread;
private bool isLoading;
private void LoadResources()
{
// Start loading the resources in an additional thread
thread = new Thread(new ThreadStart(gameplayScreen.LoadAssets));
thread.Start();
isLoading = true;
}
例如,当用户按下屏幕时调用 LoadResources 方法。
if (!isLoading)
{
if (input.Gestures.Count > 0)
{
if (input.Gestures[0].GestureType == GestureType.Tap)
{
LoadResources();
}
}
}
在游戏更新循环中
if (null != thread)
{
// If additional thread finished loading and the screen is not
// exiting
if (thread.ThreadState == ThreadState.Stopped && !IsExiting)
{
//start the level
}
}
向用户显示某些内容是个好主意,例如
private static readonly string loadingText = "Loading...";
并在绘制循环中
if (isLoading)
{
Vector2 size = smallFont.MeasureString(loadingText);
Vector2 messagePosition = new Vector2(
(ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
(ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
spriteBatch.DrawStringBlackAndWhite(smallFont, loadingText, messagePosition);
}