【问题标题】:Windows phone back button closes app instead of going back a pageWindows phone 后退按钮关闭应用程序而不是返回页面
【发布时间】: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;
        }
    }
}

【问题讨论】:

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


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多