基于页面的返回按钮的动作 [英] Page-based action for back button

查看:190
本文介绍了基于页面的返回按钮的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何定义的特定网页,即从页面后退按钮关闭弹出窗口和第二进行导航?

My question is how to define own action for given page i.e. first click on back button close popup and second navigates from page?

由于WP8.1 NavigationHelper是非常有用的但它只能给出一个总的行动点击返回键(退出页)。

Since WP8.1 NavigationHelper is quite useful yet it only gives one general action for clicking back key (exiting page).

我无法覆盖NavigationHelper,因为它没有给setter方法​​的命令和后退按钮单击处理程序是私有的,所以我不能取消固定它在进入页面。在NavigationHelper进行更改似乎难看因为我开发W8.1和放大器通用的应用程序; WP8.1和我有需要后退按钮自定义处理多页。

I can't overwrite NavigationHelper since it gives no setters for its commands and handler for back button click is private, so I can't unpin it on entering page. Making changes in NavigationHelper seems ugly since I'm developing universal app for W8.1 & WP8.1 and I have multiple pages that require custom handler for back button.

- 编辑 -

命令允许覆盖,但我会在每一页上使用原来的命令。解决方法是在每个页面上创建新的命令?

Commands allows overwriting, yet I'll use original command on each page. Solution is to create new command on each page?

推荐答案

我觉得你可以订阅 Windows.Phone.UI.Input.HardwareButtons.Back pressed 在NavigationHelper做之前(因为我已经检查了它在Page.Loaded事件订阅)。事实上,对于这一目的(如您稍后将添加事件处理器),您将需要一个特殊的InvokingMethod将调用你的事件处理器:

I think you can subscribe to Windows.Phone.UI.Input.HardwareButtons.BackPressed before the NavigationHelper does (as I've checked it subscribes in Page.Loaded event). In fact for this purpose (as you will be adding EventHandlers later) you will need a special InvokingMethod that will invoke your EventHandlers:

// in App.xaml.cs make a method and a listOfHandlers:
private List<EventHandler<BackPressedEventArgs>> listOfHandlers = new List<EventHandler<BackPressedEventArgs>>();

private void InvokingMethod(object sender, BackPressedEventArgs e)
{
   for (int i = 0; i < listOfHandlers.Count; i++)
       listOfHandlers[i](sender, e);
}

public event EventHandler<BackPressedEventArgs> myBackKeyEvent
{
   add { listOfHandlers.Add(value); }
   remove { listOfHandlers.Remove(value); }
}

// look that as it is a collection you can make a variety of things with it
// for example provide a method that will put your new event on top of it
// it will beinvoked as first
public void AddToTop(EventHandler<BackPressedEventArgs> eventToAdd) { listOfHandlers.Insert(0, eventToAdd); }

// in App constructor: - do this as FIRST eventhandler subscribed!
HardwareButtons.BackPressed += InvokingMethod;

// subscription:
(App.Current as App).myBackKeyEvent += MyClosingPopupHandler;
// or
(App.Current as App).AddToTop(MyClosingPopupHandler); // it should be fired first

// also you will need to change in NavigationHelper.cs behaviour of HardwereButtons_BackPressed
// so that it won't fire while e.Handeled == true
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (!e.Handled)
    {
        // rest of the code
    }
}

在这种情况下,返回pressed将NavigationHelper的事件处理程序之前被解雇,如果您设置 e.Handeled = TRUE; ,那么你应该留在同一页面

In this case BackPressed will be fired before NavigationHelper's eventhandler and if you set e.Handeled = true; then you should stay on the same Page.

还要注意,而不是修改App.xaml.cs可以延长这种方式您的Page类或NavigationHelper的,这取决于你的需求。

Note also that instead of modifying App.xaml.cs you can extend this way your Page class or NavigationHelper, it depends on your needs.

这篇关于基于页面的返回按钮的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆