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

时间:2015-07-15 20:58:35

标签: c# .net windows xaml windows-phone-8

使用基本的Windows通用应用程序,目前遇到的问题是,在按下我的列表的详细信息页面后,它不会将我带回主页面,而是完全关闭应用程序。

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

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;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

这在Windows Phone 8.1中是正常的。您可能希望在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