【发布时间】:2015-03-22 20:31:01
【问题描述】:
伙计。我在手机应用程序中的导航上苦苦挣扎。我在我的项目中添加了三个空白页,并设法向前导航页面,但是.... 我希望用户在单击手机上的“返回按钮”时只返回一页,当然当他再次按下它时返回主页。发生的情况是,用户在按下设备上的后退按钮时退出应用程序。 这是我在名为的方法中默认获得的代码:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (this.GoBackCommand.CanExecute(null))
{
e.Handled = true;
this.GoBackCommand.Execute(null);
}
}
这是我尝试输入的方法,以及我在互联网上找到但没有用的方法:
if (Frame.CanGoBack)
{
e.Handled = true;
Frame.GoBack();
}
和这个(非常相似)但也没有工作皱眉| :(
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
我终于尝试使用基本页面而不是空白页面来构建新应用程序,然后粘贴代码但它再次出现。请帮我。我放弃了。
这是我的第二页代码
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace NutriPal
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ListPage : Page
{
public ListPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
BrowsingManager browsing = (BrowsingManager) e.Parameter as BrowsingManager;
if (browsing != null)
{
listOfItems.DataContext = browsing;
//pageTitle.Text = browsing.Title;
}
}
可能有助于 NavigationHelper 的代码块
public class NavigationHelper : DependencyObject
{
private Page Page { get; set; }
private Frame Frame { get { return this.Page.Frame; } }
/// <summary>
/// Initializes a new instance of the <see cref="NavigationHelper"/> class.
/// </summary>
/// <param name="page">A reference to the current page used for navigation.
/// This reference allows for frame manipulation and to ensure that keyboard
/// navigation requests only occur when the page is occupying the entire window.</param>
public NavigationHelper(Page page)
{
this.Page = page;
// When this page is part of the visual tree make two changes:
// 1) Map application view state to visual state for the page
// 2) Handle hardware navigation requests
this.Page.Loaded += (sender, e) =>
{
#if WINDOWS_PHONE_APP
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
#else
// Keyboard and mouse navigation only apply when occupying the entire window
if (this.Page.ActualHeight == Window.Current.Bounds.Height &&
this.Page.ActualWidth == Window.Current.Bounds.Width)
{
// Listen to the window directly so focus isn't required
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated +=
CoreDispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerPressed +=
this.CoreWindow_PointerPressed;
}
#endif
};
// Undo the same changes when the page is no longer visible
this.Page.Unloaded += (sender, e) =>
{
#if WINDOWS_PHONE_APP
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
#else
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated -=
CoreDispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerPressed -=
this.CoreWindow_PointerPressed;
#endif
};
}
public void OnNavigatedTo(NavigationEventArgs e)
{
var frameState = SuspensionManager.SessionStateForFrame(this.Frame);
this._pageKey = "Page-" + this.Frame.BackStackDepth;
if (e.NavigationMode == NavigationMode.New)
{
// Clear existing state for forward navigation when adding a new page to the
// navigation stack
var nextPageKey = this._pageKey;
int nextPageIndex = this.Frame.BackStackDepth;
while (frameState.Remove(nextPageKey))
{
nextPageIndex++;
nextPageKey = "Page-" + nextPageIndex;
}
// Pass the navigation parameter to the new page
if (this.LoadState != null)
{
this.LoadState(this, new LoadStateEventArgs(e.Parameter, null));
}
}
【问题讨论】:
-
应用退出第二页还是主页面?你能检查一下你订阅了多少次 HardwareButtons_BackPressed 吗? (也在 app.xaml.cs 中检查)。还要检查您是否使用 NavigationHelper。
-
我有我的应用程序开始使用的 MainPage,第二页和第三页。我里面有 Common 文件夹和 NavigationHelper。据我所知,有一个订阅:Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
-
你能检查一下你的页面是否使用 NavigationHelper(应该有
this.NavigationHelper = new NavigationHelper;)?如果是这样,那么导航助手订阅后退按钮。检查您在 app.xaml.cs 中是否有第二个订阅,如果有,那么您的代码可能会触发两个负责返回导航的事件处理程序。 -
我粘贴了代码,你可以看看。告诉我哪段代码你需要知道我在哪里,因为我很困惑。我应该在哪里寻找 this.NavigationHelper=new NavigationHelper;
-
这似乎是一个没有使用 NavigationHelper 的空白时代(与基本页面模板相比)。您在哪里订阅 BackButton?您可以共享示例项目吗?旁注:NavigationHelper 是一个模板,所以如果您没有更改它,则无需在此处粘贴。
标签: c# windows xaml windows-phone-8.1