我有这个要求,我需要将自定义popUp显示为页面叠加层。这个自定义PopUp是随机的,可以显示在任何页面上(基于某些逻辑)。我在此自定义PopUp类中注册了OnBackKeyPress
事件(对于当前页面)。
但是因为几乎所有app页面都定义了OnBackKeyPress
方法(根据业务需求再次),新事件注册(在自定义PopUp类中)发生在前一个之后。因此,在按下硬件返回键时,首先调用页面中编写的OnBackKeyPress
方法,然后调用自定义PopUp类中编写的OnBackKeyPress
方法。我需要避免调用页面中写的OnBackKeyPress
方法。
无法接受的解决方案:
PhoneApplicationPage
(我们需要一些透明度,以便显示当前页面的数据)OnBackKeyPress
方法(ap中有这么多页面)。PhoneApplicationPage
并编写一个新的OnBackKeyPress
方法来处理相同的问题(不会!)答案 0 :(得分:0)
以下是如何在已订阅的处理程序之前订阅事件的示例:
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_Func1