首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止调用OnBackKeyPress方法按下wp8中的硬件回键?

如何停止调用OnBackKeyPress方法按下wp8中的硬件回键?
EN

Stack Overflow用户
提问于 2015-09-24 06:56:13
回答 1查看 50关注 0票数 1

我有这样的要求,在这里我需要显示一个自定义popUp作为页面覆盖。这个自定义PopUp是随机的,可以显示在任何页面上(基于某种逻辑)。我已经在这个自定义的OnBackKeyPress类中注册了PopUp事件(针对当前页面)。

但是,由于几乎所有的app页面都定义了一个OnBackKeyPress方法(同样是根据业务需求定义的),所以新的事件注册(在自定义的PopUp类中)是在前一个事件之后进行的。因此,在按下硬件回键时,首先调用页面中写入的OnBackKeyPress方法,然后是用自定义PopUp类编写的OnBackKeyPress方法。我需要避免调用用页面编写的OnBackKeyPress方法。

解决方案不可接受的

  1. 将新的popUp控件设置为PhoneApplicationPage (我们需要一些透明度,以便显示当前页面的数据)
  2. OnBackKeyPress方法放在popUp的所有页面上(ap中有这么多的页面!)
  3. 扩展PhoneApplicationPage并编写一个新的OnBackKeyPress方法来处理相同的问题(不!)
EN

回答 1

Stack Overflow用户

发布于 2015-09-24 09:38:33

下面是如何在订阅处理程序之前订阅事件的示例:

代码语言:javascript
复制
class EventTesterClass // Simple class that raise an event
{
    public event EventHandler Func;

    public void RaiseEvent()
    {
        Func(null, EventArgs.Empty);
    }
}

static void subscribeEventBeforeOthers<InstanceType>(InstanceType instance, string eventName, EventHandler handler) // Magic method that unsubscribe subscribed methods, subscribe the new method and resubscribe previous methods
{
    Type instanceType = typeof(InstanceType);
    var eventInfo = instanceType.GetEvent(eventName);
    var eventFieldInfo = instanceType.GetField(eventInfo.Name,
                                        BindingFlags.NonPublic |
                                        BindingFlags.Instance |
                                        BindingFlags.GetField);

    var eventFieldValue = (System.Delegate)eventFieldInfo.GetValue(instance);
    var methods = eventFieldValue.GetInvocationList();

    foreach (var item in methods)
    {
        eventInfo.RemoveEventHandler(instance, item);
    }
    eventInfo.AddEventHandler(instance, handler);
    foreach (var item in methods)
    {
        eventInfo.AddEventHandler(instance, item);
    }
}

static void Main(string[] args)
{
    var evntTest = new EventTesterClass();
    evntTest.Func += handler_Func;
    evntTest.Func += handler_Func1;
    evntTest.RaiseEvent();
    Console.WriteLine();
    subscribeEventBeforeOthers(evntTest, "Func", handler_Func2);
    evntTest.RaiseEvent();
}

static void handler_Func(object sender, EventArgs e)
{
    Console.WriteLine("handler_Func");
}

static void handler_Func1(object sender, EventArgs e)
{
    Console.WriteLine("handler_Func1");
}

static void handler_Func2(object sender, EventArgs e)
{
    Console.WriteLine("handler_Func2");
}

产出将是:

代码语言:javascript
复制
handler_Func
handler_Func1

handler_Func2
handler_Func
handler_Func1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32755185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档