private async Task ShowAsync(Snackbar snackbar, SnackbarMessageQueueItem messageQueueItem, ManualResetEvent actionClickWaitHandle) { snackbar.Visibility = Visibility.Visible; snackbar.SetCurrentValue(Snackbar.IsActiveProperty, false); //create and show the message, setting up all the handles we need to wait on var mouseNotOverManagedWaitHandle = CreateAndShowMessage(snackbar, messageQueueItem, actionClickWaitHandle); var durationPassedWaitHandle = new ManualResetEvent(false); StartDuration(messageQueueItem.Duration.Add(snackbar.ActivateStoryboardDuration), durationPassedWaitHandle); //wait until time span completed (including pauses and mouse overs), or the action is clicked await WaitForCompletionAsync(mouseNotOverManagedWaitHandle, durationPassedWaitHandle, actionClickWaitHandle); //close message on snackbar snackbar.SetCurrentValue(Snackbar.IsActiveProperty, false); //we could wait for the animation event, but just doing //this for now...at least it is prevent extra call back hell await Task.Delay(snackbar.DeactivateStoryboardDuration); //remove message on snackbar snackbar.SetCurrentValue(Snackbar.MessageProperty, null); mouseNotOverManagedWaitHandle.Dispose(); durationPassedWaitHandle.Dispose(); }
private async Task ShowAsync(Snackbar snackbar, SnackbarMessageQueueItem messageQueueItem, ManualResetEvent actionClickWaitHandle) { //create and show the message, setting up all the handles we need to wait on var tuple = CreateAndShowMessage(snackbar, messageQueueItem, actionClickWaitHandle); var snackbarMessage = tuple.Item1; var mouseNotOverManagedWaitHandle = tuple.Item2; var durationPassedWaitHandle = new ManualResetEvent(false); StartDuration(messageQueueItem.Duration.Add(snackbar.ActivateStoryboardDuration), durationPassedWaitHandle); //wait until time span completed (including pauses and mouse overs), or the action is clicked await WaitForCompletionAsync(mouseNotOverManagedWaitHandle, durationPassedWaitHandle, actionClickWaitHandle); //close message on snackbar snackbar.SetCurrentValue(Snackbar.IsActiveProperty, false); //we could wait for the animation event, but just doing //this for now...at least it is prevent extra call back hell await Task.Delay(snackbar.DeactivateStoryboardDuration); //this prevents missing resource warnings after the message is removed from the Snackbar //see https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/2040 snackbarMessage.Resources = SnackbarMessage.defaultResources; //remove message on snackbar snackbar.SetCurrentValue(Snackbar.MessageProperty, null); mouseNotOverManagedWaitHandle.Dispose(); durationPassedWaitHandle.Dispose(); }
private async Task ShowAsync(Snackbar snackbar, SnackbarMessageQueueItem messageQueueItem) { await Task.Run(async() => { //create and show the message, setting up all the handles we need to wait on var actionClickWaitHandle = new ManualResetEvent(false); var mouseNotOverManagedWaitHandle = await snackbar.Dispatcher.InvokeAsync( () => CreateAndShowMessage(snackbar, messageQueueItem, actionClickWaitHandle)); var durationPassedWaitHandle = new ManualResetEvent(false); DurationMonitor.Start(messageQueueItem.Duration.Add(snackbar.ActivateStoryboardDuration), _pausedEvent, durationPassedWaitHandle, _disposedEvent); //wait until time span completed (including pauses and mouse overs), or the action is clicked await WaitForCompletionAsync(mouseNotOverManagedWaitHandle, durationPassedWaitHandle, actionClickWaitHandle); //close message on snackbar await snackbar.Dispatcher.InvokeAsync( () => snackbar.SetCurrentValue(Snackbar.IsActiveProperty, false)); //we could wait for the animation event, but just doing //this for now...at least it is prevent extra call back hell _disposedEvent.WaitOne(snackbar.DeactivateStoryboardDuration); //remove message on snackbar await snackbar.Dispatcher.InvokeAsync( () => snackbar.SetCurrentValue(Snackbar.MessageProperty, null)); mouseNotOverManagedWaitHandle.Dispose(); durationPassedWaitHandle.Dispose(); }) .ContinueWith(t => { if (t.Exception == null) { return; } var exc = t.Exception.InnerExceptions.FirstOrDefault() ?? t.Exception; Trace.WriteLine("Error occured whilst showing Snackbar, exception will be rethrown."); Trace.WriteLine($"{exc.Message} ({exc.GetType().FullName})"); Trace.WriteLine(exc.StackTrace); throw t.Exception; }); }