简体   繁体   English

Windows Phone后退按钮关闭应用程序,而不是返回页面

[英]Windows phone back button closes app instead of going back a page

Working on a basic Windows Universal app and currently have the issue where upon pressing back on the details page of my lists it doesn't take me back to the main page but instead closes the app entirely. 使用基本的Windows Universal应用程序时,当前遇到的问题是,按一下我的列表的详细信息页面后,并不能带我回到主页,而是完全关闭了该应用程序。

MainPage.xaml.cs MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.Phone.UI;
using Windows.UI.Xaml.Navigation;

namespace MoreUniversalAppWork
{
    public sealed partial class MainPage : Page
    {
        public QuoteReader qr = new QuoteReader();
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
            qr.DownloadQuotes();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }
        private void Hardware_Back(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            if (Frame.Content is ViewQuote)
            {
                Frame.Navigate(typeof(MainPage));
                e.Handled = true;
            }
        }
        private void LoadQuotes(object sender, RoutedEventArgs e)
        {
            FillList();
        }
        private void FillList()
        {
            // Downloads the JSON file with the quotes and filles List //
            qr.DownloadQuotes();
            var quotes = qr.quotes_dictionary();
            list_quotes.Items.Clear();
            foreach (var q in quotes)
            {
                list_quotes.Items.Add(q.Value);
            }
            if (list_quotes.Items.Count > 0)
            {
                list_quotes.ScrollIntoView(list_quotes.Items[0]);
            }
        }
        private void ViewQuote(object sender, SelectionChangedEventArgs e)
        {
            // Upon clicking quote in list, details page is opened //
            Frame.Navigate(typeof(ViewQuote), list_quotes.SelectedValue);
        }
    }
}

ViewQuote.xaml.cs ViewQuote.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ComponentModel;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

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

namespace MoreUniversalAppWork
{
    public sealed partial class ViewQuote : Page
    {
        string quote;
        public ViewQuote()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            quote = e.Parameter as string;
            quote_display.Text = quote;
        }
    }
}

This is normal in Windows Phone 8.1. 这在Windows Phone 8.1中是正常的。 You may want to have a workaround in App.xaml.cs file. 您可能要在App.xaml.cs文件中找到解决方法。

public App()
{
    this.InitializeComponent();   
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;        
}

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;

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

WP8.1 back button quits the app WP8.1后退按钮退出应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM