I have a holding event on a ListBoxItem
. So When I hold an item, it enters right in the function but it appears as it's fired twice.
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());
}
}
As I'm displaying a MessageDialog
msgToAddContact at the end of that function, the fact that it's fired twice, it makes the MessageDialog
displayed twice too.
If the first MessageBox.showAsync
is not finished, it crashes because it's not possible to show multiple MessageDialog
at the same time.
Does anyone knows how to block the second execution of the holding event?
Thanks in advance!
I've just found out why it's firing more than one time. Events like Holding or SelectionChanged are events with different states. In my case, The holding event has 3 states: started, completed, cancelled. The different states are proceeding as follow. When I hold an element one time, the state of the event is started, when the whole function inside the eventHandler is done, the Holding Event is fired a second time with a state of completed, same thing if the user cancels the event.
Msft explains it well here: EventHandler
To avoid executing the same code at each state, just add a condition at the beginning of the critical code which is to be executed just one time.
My code is actually looking that as you can compare with my firts post:
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());
}
}