当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to stop calling OnBackKeyPress method on pressing the hardware back key in wp8?

问题描述:

I have this requirement where I need to show a custom popUp as a page overlay. This custom PopUp is random and can show up on any page (based on some logic). I have registered the OnBackKeyPress event (for the current page) in this custom PopUp class.

But because almost all the pages of app also has an OnBackKeyPress method defined (again as per business requirement), the new event registration (in the custom PopUp class) takes place after the previous one. So on pressing the hardware back key, the OnBackKeyPress method written in pages is getting called first followed by the OnBackKeyPress method written in custom PopUp class. I need to avoid calling OnBackKeyPress method written in pages.

Solutions not acceptable :

  1. make the new popUp control as a PhoneApplicationPage (we need some transparency where we can show the current page's data)
  2. Put check on OnBackKeyPress method on all pages for the popUp (so many pages in ap!)
  3. Extend PhoneApplicationPage and write a new OnBackKeyPress method to handle the same (no no!)

网友答案:

Here is an example of how to subscribe an event before already subscribed handlers:

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");
}

Output will be:

handler_Func
handler_Func1

handler_Func2
handler_Func
handler_Func1
分享给朋友:
您可能感兴趣的文章:
随机阅读: