【问题标题】:Holding event fired twice windows phone 8.1举行事件触发两次windows phone 8.1
【发布时间】:2015-02-02 02:15:00
【问题描述】:

我在ListBoxItem 上有一个举办活动。所以当我拿着一个项目时,它会直接进入函数,但它会显示为它被触发了两次。

private async void OutersAndContactInTel_Holding(object sender, HoldingRoutedEventArgs e)
{
    try
    {
        FrameworkElement element = (FrameworkElement)e.OriginalSource;
        if (element.DataContext != null && element.DataContext is Contact)
        {
            Contact selectedContact = (ImOutContact)element.DataContext;
            if (selectedContact.IsOuter)
            {
                MessageDialog msgToAddContact = new MessageDialog("Voulez-vous vraiment suivre " + selectedContact.Pseudo + " ?");
                msgToAddContact.Commands.Add(new UICommand("Oui", (UICommandInvokedHandler) =>
                {
                    AddContactProcess(selectedContact);
                }));
                msgToAddContact.Commands.Add(new UICommand("Non"));

                this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => msgToAddContact.ShowAsync());
            }
            else
            {
                MessageDialog msgToInviteContact = new MessageDialog("Envoyez une invitation à  l'utilisation de l'application par sms à " + selectedContact.NomPrenom + " ?");
                msgToInviteContact.Commands.Add(new UICommand("Oui", (UICommandInvokedHandler) =>
                {
                    SendSmsToInvite(selectedContact);
                }));
                msgToInviteContact.Commands.Add(new UICommand("Non"));
                await msgToInviteContact.ShowAsync();
            }
        }
    }
    catch (Exception ex)
    {
        MessageDialog errorMessage = new MessageDialog(CustomDialogMessage.getMessageContent(CustomDialogMessage.ERROR_MESSAGE));
        this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => errorMessage.ShowAsync());
    }
}

当我在该函数的末尾显示 MessageDialog msgToAddContact 时,它被触发了两次,这使得 MessageDialog 也显示了两次。

如果第一个MessageBox.showAsync 没有完成,它会崩溃,因为不可能同时显示多个MessageDialog

有谁知道如何阻止举行事件的第二次执行?

提前致谢!

【问题讨论】:

  • 我刚刚编辑了帖子:)
  • 有什么特别的原因让您不等待Dispatcher.RunAsync
  • 不,没有理由......当我测试时,我删除了它是为了查看行为,但它是同样的问题......

标签: c# ui-thread eventhandler messagedialog


【解决方案1】:

我刚刚发现它为什么会多次发射。像 Holding 或 SelectionChanged 这样的事件是具有不同状态的事件。在我的例子中,holding 事件有 3 个状态:startedcompletedcancelled。不同的状态如下进行。 当我一次持有一个元素时,事件的状态是started,当eventHandler内的整个函数完成时,第二次触发Holding事件,状态为completed,如果用户取消事件,也是一样。

Msft 在这里解释得很好:EventHandler

为避免在每个状态下执行相同的代码,只需在要执行一次的关键代码的开头添加一个条件。

我的代码实际上看起来就像你可以与我的第一篇文章相比:

private async void OutersAndContactInTel_Holding(object sender, HoldingRoutedEventArgs e)
{
    try
    {
        FrameworkElement element = (FrameworkElement)e.OriginalSource;
        if (element.DataContext != null && element.DataContext is Contact && e.HoldingState == Windows.UI.Input.HoldingState.Started)
        {
            Contact selectedContact = (ImOutContact)element.DataContext;
            if (selectedContact.IsOuter)
            {
                MessageDialog msgToAddContact = new MessageDialog("Voulez-vous vraiment suivre " + selectedContact.Pseudo + " ?");
                msgToAddContact.Commands.Add(new UICommand("Oui", (UICommandInvokedHandler) =>
                {
                    AddContactProcess(selectedContact);
                }));
                msgToAddContact.Commands.Add(new UICommand("Non"));

                this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => msgToAddContact.ShowAsync());
            }
            else
            {
                MessageDialog msgToInviteContact = new MessageDialog("Envoyez une invitation à  l'utilisation de l'application par sms à " + selectedContact.NomPrenom + " ?");
                msgToInviteContact.Commands.Add(new UICommand("Oui", (UICommandInvokedHandler) =>
                {
                    SendSmsToInvite(selectedContact);
                }));
                msgToInviteContact.Commands.Add(new UICommand("Non"));
                await msgToInviteContact.ShowAsync();
            }
        }
    }
    catch (Exception ex)
    {
        MessageDialog errorMessage = new MessageDialog(CustomDialogMessage.getMessageContent(CustomDialogMessage.ERROR_MESSAGE));
        this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => errorMessage.ShowAsync());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多