简体   繁体   English

如何强制某个事件序列

[英]How to force a certain Event sequence

I wrote a small control that creates a popup for my Win8 Phone application which does all the nasty things for me like rotation, proper placement etc. The popup is opened in a Popup control but not on a new phone page. 我编写了一个小控件,该控件为Win8 Phone应用程序创建了一个弹出窗口,该弹出窗口为我完成了所有麻烦的工作,例如旋转,正确放置等。该弹出窗口是在Popup控件中打开的,而不是在新的电话页面上打开的。 To close the popup, my control hooks up to the "backKeyPressed" event of the underlying page. 要关闭弹出窗口,我的控件将连接到基础页面的“ backKeyPressed”事件。 This works like charm until the underlying page has its own implementation of BackKeyPressed event. 直到基础页面具有自己的BackKeyPressed事件实现之前,它的工作原理类似于魅力。 In this case, the page event is triggered but not the popup control event. 在这种情况下,将触发页面事件,但不会触发弹出控件事件。

If I would own the event, I could create my own stack to call the last added event first, but I do not own the event of the pages. 如果我将拥有该事件,则可以创建自己的堆栈以首先调用最后添加的事件,但是我不拥有这些页面的事件。 As far as I know, I am unable to unregister any previously attached event handler and reassign it once my control unsubscribes from the event. 据我所知,一旦控件取消订阅该事件,我将无法注销任何先前附加的事件处理程序并重新分配它。

I could have only one implementation for the BackKeyPressed event which then informs the popup control to close itself (if open), if nothing was open, do the Page specific implementation. 对于BackKeyPressed事件,我可能只有一个实现,该事件然后通知弹出控件关闭自身(如果打开),如果没有打开,则执行Page特定实现。 But this would require code changes on all pages where I might want to use the popup. 但这需要在我可能要使用弹出窗口的所有页面上进行代码更改。 Even worse, if I have 5 possible popups, I would have to check all of them :-( So I am looking for an option to handle this centrally. 更糟糕的是,如果我有5个可能的弹出窗口,我将不得不检查所有这些弹出窗口:-(因此,我正在寻找一个集中处理此问题的选项。

What other options do I have to overcome this situation? 为了克服这种情况,我还有什么其他选择?

Normally you cannot change the order of fired events - they are executed in registered order, but it's not required by specifications - source . 通常,您无法更改触发事件的顺序-它们以注册顺序执行,但是规范-source并不需要。

But as Jon Skeet says here : 但是正如乔恩·斯凯特(Jon Skeet)在这里说的那样

Summary: For all sane events, you can rely on the ordering. 摘要:对于所有合理的事件,您可以依靠顺序。 In theory, events can do what they like, but I've never seen an event which doesn't maintain the appropriate ordering. 从理论上讲,事件可以按照自己的喜好进行,但是我从未见过没有保持适当顺序的事件。

it is fired in registered order and should be. 它以注册顺序被射击,应该被射击。

BUT for your purpose (I think) you can set an event to invoke your method where you would control the order. 但出于您的目的(我认为),您可以设置一个事件来调用您可以控制顺序的方法。 I think simple example can show this behaviour: 我认为简单的示例可以显示此行为:

public partial class MainPage : PhoneApplicationPage
{
    private List<EventHandler<CancelEventArgs>> listOfHandlers = new List<EventHandler<CancelEventArgs>>();

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

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

    public void AddToTop(EventHandler<CancelEventArgs> eventToAdd)
    {
        listOfHandlers.Insert(0, eventToAdd);
    }

    public MainPage()
    {
        InitializeComponent();
        this.BackKeyPress += InvokingMethod;
        myBackKeyEvent += (s, e) => { MessageBox.Show("Added first"); e.Cancel = true; };
        AddToTop((s, e) => { MessageBox.Show("Added later"); });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM