Windows Phone 8页面导航

时间:2014-03-16 01:17:55

标签: windows-phone-8

我有一个我无法弄明白的问题,我现在非常绝望 - 我不知道为什么会这样:( 所以这就是问题所在:我正在编写一种猜那个Tune app。第一页是菜单页面,用户可以按“播放”按钮,他将导航到GenreSelectPage,在那里他选择一种类型并导航到GamePage。我想在GamePage上处理BackButtonPress - 当用户点击BackButton时,他导航到MainPage,而不是GenreSelectPage。这是代码:

 private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
    {
        base.OnBackKeyPress(e);
        this.player.Pause();
        var result = MessageBox.Show(AppResources.GamePageAlert, "Warning", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
            //NavigationService.RemoveBackEntry();
            //App.RootFrame.Navigate(new Uri(@"/MainPage.xaml", UriKind.Relative));
        }
        else
        {
            this.player.Play();
            e.Cancel = true;
        }
    }

然而,我在这里遇到了一个大问题,我无法解决。当我回到MainMEnu,再次回到GenreSelectPage并选择相同的类型,一切都很好 - 应用程序导航到GamePage,其中有4个答案的列表。但是,如果我选择其他类型,GamePage上的listBox将填充12或15个项目。另一方面,当我评论导航到MainPage并通常返回时,一切正常。 这是我的GenrePage代码:

public GenresPage()
    {
        InitializeComponent();
        this.DataContext = App.ViewModel.GenreHelper;
    }

    private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        this.genresListBox.SelectedIndex = -1;
        this.progressBar.Visibility = System.Windows.Visibility.Visible;
        this.genresListBox.ItemsSource = await App.ViewModel.GenreHelper.GetGenres();
        this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
        ClearCollections();
    }

    private static void ClearCollections()
    {
        if (App.ViewModel.TracksCollection.Count != 0)
        {
            App.ViewModel.TracksCollection.Clear();
            App.ViewModel.TrackCounter = 0;
        }
        if (App.ViewModel.AnswerCollection.Count > 0)
        {
            App.ViewModel.AnswerCollection.Clear();
        }
    }

    private async void NavigateToPlay(object sender, RoutedEventArgs e)
    {
        if (this.genresListBox.SelectedIndex != -1)
        {
            this.progressBar.Visibility = System.Windows.Visibility.Visible;
            await App.ViewModel.GetSongs();
            await App.ViewModel.GetAnswers();
            this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
            NavigationService.Navigate(new Uri(@"/Views/GamePage.xaml", UriKind.Relative));
        }     
    }

更新 在我的GamePage上,我只为MediaElement分配DataContext和持续时间:

public partial class GamePage : PhoneApplicationPage
{

    public GamePage()
    {
        InitializeComponent();
        this.DataContext = App.ViewModel;
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        var trackId = App.ViewModel.TracksCollection[App.ViewModel.TrackCounter].Id;
        var sampleUri = App.ViewModel.GetSampleUri(trackId);
        player.Source = new Uri(sampleUri.AbsoluteUri);
        player.Play();
    }


    private void GetTrackDuration(object sender, RoutedEventArgs e)
    {
        var player = (MediaElement)sender;
        if (player.CurrentState == System.Windows.Media.MediaElementState.Playing)
        {
            playerSeekBar.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
        }
    }

    private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
    {
        base.OnBackKeyPress(e);
        this.player.Pause();
        var result = MessageBox.Show(AppResources.GamePageAlert, "Warning", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
            //NavigationService.RemoveBackEntry();
            //App.RootFrame.Navigate(new Uri(@"/MainPage.xaml", UriKind.Relative));
        }
        else
        {
            this.player.Play();
            e.Cancel = true;
        }
    }
}

如果有人能够指出我做错了什么,我会非常感激 - 我整天都在争吵,我不知道是什么造成的。

提前非常感谢!!

0 个答案:

没有答案