如何在Windows Phone 8.1上仅返回一页

时间:2015-03-22 20:31:01

标签: c# windows xaml windows-phone-8.1

人。我在手机应用程序中挣扎着导航。我已经为我的项目添加了三个空白页面,并设法向前导航页面到页面,但.... 我想要一个用户点击"后退按钮"在电话上只返回一页,当然再次按下它回到主页面。会发生什么,当用户按下设备上的后退按钮时退出应用程序。 这是我在默认方法中使用的代码:

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (this.GoBackCommand.CanExecute(null))
    {
        e.Handled = true;
        this.GoBackCommand.Execute(null);
    }
}

这是我试图在该方法中添加的内容,以及我在互联网上发现但未起作用的内容:

if (Frame.CanGoBack)
{
    e.Handled = true;
    Frame.GoBack();
}

和这个(非常相似)但没有工作Frown | :(

Frame rootFrame = Window.Current.Content as Frame;

if (rootFrame != null && rootFrame.CanGoBack)
{
    e.Handled = true;
    rootFrame.GoBack();
}

我终于尝试使用基本页面而不是空白页面构建新应用程序,然后粘贴代码,但再次使用该字段。请帮我。我放弃了。

这是我的第二页背后的代码

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace NutriPal
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ListPage : Page
    {

        public ListPage()
        {

            this.InitializeComponent();

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            BrowsingManager browsing = (BrowsingManager) e.Parameter as BrowsingManager;
            if (browsing != null)
            {
                listOfItems.DataContext = browsing;
                //pageTitle.Text = browsing.Title;
            }

        }

可能有助于NavigationHelper的代码块

  public class NavigationHelper : DependencyObject
    {
        private Page Page { get; set; }
        private Frame Frame { get { return this.Page.Frame; } }

        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationHelper"/> class.
        /// </summary>
        /// <param name="page">A reference to the current page used for navigation.  
        /// This reference allows for frame manipulation and to ensure that keyboard 
        /// navigation requests only occur when the page is occupying the entire window.</param>
        public NavigationHelper(Page page)
        {

            this.Page = page;


            // When this page is part of the visual tree make two changes:
            // 1) Map application view state to visual state for the page
            // 2) Handle hardware navigation requests
            this.Page.Loaded += (sender, e) =>
            {
#if WINDOWS_PHONE_APP
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#else
                // Keyboard and mouse navigation only apply when occupying the entire window
                if (this.Page.ActualHeight == Window.Current.Bounds.Height &&
                    this.Page.ActualWidth == Window.Current.Bounds.Width)
                {
                    // Listen to the window directly so focus isn't required
                    Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated +=
                        CoreDispatcher_AcceleratorKeyActivated;
                    Window.Current.CoreWindow.PointerPressed +=
                        this.CoreWindow_PointerPressed;
                }
#endif
            };

            // Undo the same changes when the page is no longer visible
            this.Page.Unloaded += (sender, e) =>
            {
#if WINDOWS_PHONE_APP
                Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
#else
                Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated -=
                    CoreDispatcher_AcceleratorKeyActivated;
                Window.Current.CoreWindow.PointerPressed -=
                    this.CoreWindow_PointerPressed;
#endif
            };
        }

 public void OnNavigatedTo(NavigationEventArgs e)
        {
            var frameState = SuspensionManager.SessionStateForFrame(this.Frame);
            this._pageKey = "Page-" + this.Frame.BackStackDepth;

            if (e.NavigationMode == NavigationMode.New)
            {
                // Clear existing state for forward navigation when adding a new page to the
                // navigation stack
                var nextPageKey = this._pageKey;
                int nextPageIndex = this.Frame.BackStackDepth;
                while (frameState.Remove(nextPageKey))
                {
                    nextPageIndex++;
                    nextPageKey = "Page-" + nextPageIndex;
                }

                // Pass the navigation parameter to the new page
                if (this.LoadState != null)
                {
                    this.LoadState(this, new LoadStateEventArgs(e.Parameter, null));
                }
            }

1 个答案:

答案 0 :(得分:0)

试试这个

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        NavigationService.Navigate(new Uri("PageName", UriKind.Relative));
        base.OnBackKeyPress(e);

    }