Windows手机的返回键关闭应用程序,而不是返回一个页面
本文关键字:返回 一个 手机 应用程序 Windows | 更新日期: 2023-09-27 18:02:58
在一个基本的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;
}
}
}
这在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后退键退出应用