Tiểu Kết
Tiểu Kết

Reputation: 55

Clear memory used by Page in WP8

I created a small game (just by xaml, not xna ), this game have 4 xaml page: MainPage, ChoseLevelPage, PlayingPage and ScorePage The first time I play , the game worked smooth (navigate from MainPage -> ChoseLevelPage -> PlayingPage) when the level finished, it navigate from PlayingPage -> scorePage -> MainPage

but the second time i chose a level and play (MainPage -> ChoseLevelPage -> PlayingPage), the action chose level (from ChoseLevelPage -> PlayingPage) takes so long and the gameplay in PlayingPage also very lag and sometimes crash my app

My PlayingPage conntains a MediaElement (playback song for level), a DispatcherTimer (to cout down time of level) , and a StoryBoard repeated after each 2 second (for each turn in level)

At the OnNavigatedFrom event , I set the mediaelement and storyboard to stop, but I just don't know if they cleared from memory or not (and also other resources I used in 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);
    }

Upvotes: 2

Views: 388

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39017

When you're back to MainPage, make sure to clear the back stack, otherwise your previous instance of the PlayingPage will stay in memory, probably explaining the issue you're facing.

You can clear the backstack with a simple loop, preferably in the OnNavigatedTo event of your MainPage:

while (this.NavigationService.BackStack.Any())
{
   this.NavigationService.RemoveBackEntry();
}

Upvotes: 1

Related Questions