我有这样的要求,在这里我需要显示一个自定义popUp作为页面覆盖。这个自定义PopUp是随机的,可以显示在任何页面上(基于某种逻辑)。我已经在这个自定义的OnBackKeyPress类中注册了PopUp事件(针对当前页面)。
但是,由于几乎所有的app页面都定义了一个OnBackKeyPress方法(同样是根据业务需求定义的),所以新的事件注册(在自定义的PopUp类中)是在前一个事件之后进行的。因此,在按下硬件回键时,首先调用页面中写入的OnBackKeyPress方法,然后是用自定义PopUp类编写的OnBackKeyPress方法。我需要避免调用用页面编写的OnBackKeyPress方法。
解决方案不可接受的:
PhoneApplicationPage (我们需要一些透明度,以便显示当前页面的数据)OnBackKeyPress方法放在popUp的所有页面上(ap中有这么多的页面!)PhoneApplicationPage并编写一个新的OnBackKeyPress方法来处理相同的问题(不!)发布于 2015-09-24 09:38:33
下面是如何在订阅处理程序之前订阅事件的示例:
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");
}产出将是:
handler_Func
handler_Func1
handler_Func2
handler_Func
handler_Func1https://stackoverflow.com/questions/32755185
复制相似问题