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

Posted

技术标签:

【中文标题】Windows phone 后退按钮关闭应用程序而不是返回页面【英文标题】:Windows phone back button closes app instead of going back a page 【发布时间】:2015-10-05 02:48:14 【问题描述】:

正在开发一个基本的 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;
        
    

【问题讨论】:

看这里:***.com/questions/24335925/… 【参考方案1】:

这在 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

【讨论】:

以上是关于Windows phone 后退按钮关闭应用程序而不是返回页面的主要内容,如果未能解决你的问题,请参考以下文章

windows phone 7 让应用程序在后退按钮上运行

Sencha Touch - Windows Phone 8 Phonegap 应用程序栏不隐藏在后退按钮单击

当按下后退按钮并打开导航抽屉时,我的应用程序关闭而不是关闭抽屉

后退按钮关闭应用程序而不是转到上一个片段 android 导航组件

Windows Phone开发:关于导航的小技巧

如何使用浏览器后退按钮关闭引导模式而不是返回页面?`