C# 如何在Windows Phone 8.1上仅浏览一页
伙计。我正在手机应用程序中努力导航。我已经在我的项目中添加了三个空白页面,并设法在页面之间导航,但是。。。。 我希望一个用户在点击手机上的“后退按钮”时只返回一页,当然,当他再次按下按钮时,返回主页。发生的情况是,用户在按下设备上的后退按钮时退出应用程序。 这是我在名为:C# 如何在Windows Phone 8.1上仅浏览一页,c#,windows,xaml,windows-phone-8.1,C#,Windows,Xaml,Windows Phone 8.1,伙计。我正在手机应用程序中努力导航。我已经在我的项目中添加了三个空白页面,并设法在页面之间导航,但是。。。。 我希望一个用户在点击手机上的“后退按钮”时只返回一页,当然,当他再次按下按钮时,返回主页。发生的情况是,用户在按下设备上的后退按钮时退出应用程序。 这是我在名为: private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) {
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();
}
这个(很相似),但也不起作用
我最终尝试用基本页面而不是空白页面来构建新的应用程序,然后粘贴代码,但它又出现了一个字段。请帮帮我。我放弃了
这是我第二页的代码
// 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;
}
}
<代码> //空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkID=390556
名称空间NutriPal
{
///
///可以单独使用或在框架内导航到的空页。
///
公共密封部分类列表第页:第页
{
公共列表页()
{
this.InitializeComponent();
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
BrowsingManager browsing=(BrowsingManager)e.参数为BrowsingManager;
如果(浏览!=null)
{
listOfItems.DataContext=浏览;
//pageTitle.Text=浏览.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));
}
}
公共类NavigationHelper:DependencyObject
{
专用页页面{get;set;}
私有帧帧{get{返回this.Page.Frame;}}
///
///初始化类的新实例。
///
///对用于导航的当前页面的引用。
///此参考允许进行帧操作,并确保键盘
///导航请求仅在页面占用整个窗口时发生。
公共NavigationHelper(第页)
{
this.Page=Page;
//当此页面是可视化树的一部分时,请进行两项更改:
//1)将应用程序视图状态映射到页面的可视状态
//2)处理硬件导航请求
this.Page.Loaded+=(发件人,e)=>
{
#如果WINDOWS\u PHONE\u应用程序
Windows.Phone.UI.Input.HardwareButtons.BackPressed+=硬件按钮\u BackPressed;
#否则
//键盘和鼠标导航仅在占用整个窗口时适用
如果(this.Page.ActualHeight==Window.Current.Bounds.Height&&
this.Page.ActualWidth==Window.Current.Bounds.Width)
{
//直接收听窗口,因此无需聚焦
Window.Current.CoreWindow.Dispatcher.AcceleratoryActivated+=
CoreDispatcher_加速器已激活;
Window.Current.CoreWindow.Pointer已压缩+=
此.core窗口_指针已压缩;
}
#恩迪夫
};
//当页面不再可见时,撤消相同的更改
this.Page.unload+=(发件人,e)=>
{
#如果WINDOWS\u PHONE\u应用程序
Windows.Phone.UI.Input.HardwareButtons.BackPressed-=HardwareButtons\u BackPressed;
#否则
Window.Current.CoreWindow.Dispatcher.AcceleratoryActivated-=
CoreDispatcher_加速器已激活;
Window.Current.CoreWindow.Pointer已压缩-=
此.core窗口_指针已压缩;
#恩迪夫
};
}
导航到时的公共无效(导航目标e)
{
var frameState=SuspensionManager.SessionStateForFrame(this.Frame);
这。_pageKey=“Page-”+this.Frame.backbackbackdepth;
if(e.NavigationMode==NavigationMode.New)
{
//将新页面添加到页面时,清除前进导航的现有状态
//导航堆栈
var nextPageKey=此项。_pageKey;
int nextPageIndex=this.Frame.BackStackDepth;
while(frameState.Remove(nextPageKey))
{
nextPageIndex++;
nextPageKey=“Page-”+nextPageIndex;
}
//将导航参数传递到新页面
if(this.LoadState!=null)
{
this.LoadState(this,new LoadStateEventArgs(e.Parameter,null));
}
}
试试这个
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri("PageName", UriKind.Relative));
base.OnBackKeyPress(e);
}
应用程序从第二页或主页退出?你能检查一下你订阅了多少次HardwareButtons_BackPressed吗?(在app.xaml.cs中也检查一下)。还请检查是否使用NavigationHelper。我的应用程序使用的主页是从第二页和第三页开始的。我的主页中有公用文件夹和NavigationHelper。据我所知,有一个订阅:Windows.Phone.UI.Input.HardwareButtons.BackPressed+=HardwareButtons\u BackPressed;你能检查你的页面是否使用NavigationHelper吗(应该有
this.NavigationHelper=new NavigationHelper;
)?如果是,则导航助手订阅“后退”按钮。检查您是否在app.xaml.cs中有第二次订阅,如果是,则您的代码可能会触发两个负责后退导航的事件处理程序。我粘贴代码以便您可以查看。告诉我您需要知道我在哪里的代码块,因为我很困惑。我应该在哪里查找k表示这个。NavigationHelper=新的NavigationHelper;看起来
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
NavigationService.Navigate(new Uri("PageName", UriKind.Relative));
base.OnBackKeyPress(e);
}