0

我有一个小应用程序,有 3 秒的介绍页面,然后是内容页面。当我按下后退按钮时,我会回到介绍屏幕,但我想我应该回到系统。怎么做?

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;

namespace RSS {
    public partial class FeedPage : PhoneApplicationPage {
        public FeedPage() {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PhonePage1_Loaded);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
            clearBackStack();
            base.OnNavigatedTo(e);
        }

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

        void PhonePage1_Loaded(object sender, RoutedEventArgs e) {
         //   clearBackStack();
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("http://www.carmagazine.co.uk/Shared/Handlers/RssHandler.ashx?&N=190&Ns=P_Publication_Date|1&?"));
        }
        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
            SyndicationFeed feed;
            try {
                using (XmlReader reader = XmlReader.Create(e.Result)) {
                    feed = SyndicationFeed.Load(reader);
                    lista.ItemsSource = feed.Items;
                }
            } catch (WebException we) { MessageBox.Show("Internet connection is down.");}
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e) {

            WebBrowserTask webBrowserTask = new WebBrowserTask();
            String url = (String)((Button)sender).Tag;
            webBrowserTask.Uri = new Uri(url);
            webBrowserTask.Show();
        }
    }
}
4

2 回答 2

2

OnNavigateTo您应该在内容页面的方法中清除 BackStack

while (this.NavigationService.BackStack.Any())
{
   this.NavigationService.RemoveBackEntry();
}
于 2012-12-04T21:22:24.527 回答
0

以下代码是后退按钮按键的最佳实践。

protected override void OnBackKeyPress(CancelEventArgs e)
    {
        while (NavigationService.CanGoBack)
            NavigationService.RemoveBackEntry();

        base.OnBackKeyPress(e);

    }

这可确保您的应用程序在按下 BackKey 时退出并返回主屏幕。

于 2012-12-05T03:43:58.403 回答