我创建了一个小游戏(仅通过xaml而不是xna),该游戏有4个xaml页面:MainPage,ChoseLevelPage,PlayingPage和ScorePage
我第一次玩时,游戏运行顺利(从MainPage-> ChoseLevelPage-> PlayingPage导航)
完成关卡后,它会从PlayingPage-> scorePage-> MainPage导航
但是第二次我选择关卡并进行播放(MainPage-> ChoseLevelPage-> PlayingPage)时,选择关卡的动作(从ChoseLevelPage-> PlayingPage)花费了很长时间,PlayingPage中的游戏玩法也非常滞后,有时会使我的应用崩溃
“我的PlayingPage”包含一个MediaElement(关卡的播放歌曲),一个DispatcherTimer(确定关卡的播放时间)和一个StoryBoard,每2秒钟重复一次(每次关卡)
在OnNavigatedFrom事件中,我将mediaelement和storyboard设置为停止,但我只是不知道它们是否从内存中清除(以及我在PlayingPage中使用的其他资源)?
void dispatcherTimer1_Tick(object sender, EventArgs e)
{
if (time > 0) // this variable cout the time left, decreased by 1 second
{
time --;
}
else // when "time" =0, stop the level and navigate to ScorePage
{
dispatcherTimer1.Stop(); // stop the dispatcher
NavigationService.Navigate(new Uri("/Pages/ScorePage.xaml", UriKind.Relative));
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
myStoryboard.Stop(); // stop the story board
meSong.Stop(); // stop the mediaelement
App.MyModel.PlayingSong = null; // set source of the medialement to null
base.OnNavigatedFrom(e);
}
最佳答案
当您返回MainPage时,请确保清除后堆栈,否则您以前的PlayingPage实例将保留在内存中,这可能解释了您所面临的问题。
您可以通过一个简单的循环清除后退堆栈,最好在MainPage的OnNavigatedTo
事件中进行:
while (this.NavigationService.BackStack.Any())
{
this.NavigationService.RemoveBackEntry();
}
关于c# - 清除WP8中Page使用的内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25656727/