/// <summary> /// Begins a storyboard and waits for it to complete. /// </summary> public static async Task BeginAsync(this Storyboard storyboard) { await EventAsync.FromEvent <object>( eh => storyboard.Completed += eh, eh => storyboard.Completed -= eh, storyboard.Begin); }
/// <summary> /// Waits for the element to load (construct and add to the main object tree). /// </summary> public static async Task WaitForLoadedAsync(this FrameworkElement frameworkElement) { if (frameworkElement.IsInVisualTree()) { return; } await EventAsync.FromRoutedEvent( eh => frameworkElement.Loaded += eh, eh => frameworkElement.Loaded -= eh); }
/// <summary> /// Scrolls to horizontal offset asynchronously. /// </summary> /// <param name="scrollViewer">The scroll viewer.</param> /// <param name="offset">The offset.</param> /// <returns>The task that completes when scrolling is complete.</returns> public static async Task ScrollToHorizontalOffsetAsync(this ScrollViewer scrollViewer, double offset) { if (offset < 0) { offset = 0; } if (offset > scrollViewer.ScrollableWidth) { offset = scrollViewer.ScrollableWidth; } var currentOffset = scrollViewer.HorizontalOffset; // ReSharper disable CompareOfFloatsByEqualityOperator if (offset == currentOffset) { return; } if (!scrollViewer.ChangeView(offset, null, null, true)) { return; } if (scrollViewer.HorizontalOffset == offset) { return; } if (scrollViewer.HorizontalOffset != currentOffset) { return; } // ReSharper restore CompareOfFloatsByEqualityOperator await EventAsync.FromEvent <ScrollViewerViewChangedEventArgs>( eh => scrollViewer.ViewChanged += eh, eh => scrollViewer.ViewChanged -= eh); }
/// <summary> /// Scrolls to vertical offset asynchronously. /// </summary> /// <param name="scrollViewer">The scroll viewer.</param> /// <param name="offset">The offset.</param> /// <returns>The task that completes when scrolling is complete.</returns> public static async Task ScrollToVerticalOffsetAsync(this ScrollViewer scrollViewer, double offset) { if (offset < 0) { offset = 0; } if (offset > scrollViewer.ScrollableHeight) { offset = scrollViewer.ScrollableHeight; } var currentOffset = scrollViewer.VerticalOffset; // ReSharper disable CompareOfFloatsByEqualityOperator if (offset == currentOffset) { return; } //scrollViewer.ScrollToVerticalOffset(offset); if (scrollViewer.VerticalOffset == offset) { return; } if (scrollViewer.VerticalOffset != currentOffset) { return; } // ReSharper restore CompareOfFloatsByEqualityOperator await EventAsync.FromEvent <ScrollViewerViewChangedEventArgs>( eh => scrollViewer.ViewChanged += eh, eh => scrollViewer.ViewChanged -= eh); }
/// <summary> /// Waits for the next layout update event. /// </summary> /// <param name="frameworkElement">The framework element.</param> /// <returns></returns> public static async Task WaitForLayoutUpdateAsync(this FrameworkElement frameworkElement) { await EventAsync.FromEvent <object>( eh => frameworkElement.LayoutUpdated += eh, eh => frameworkElement.LayoutUpdated -= eh); }
/// <summary> /// Waits for the element to unload (disconnect from the main object tree). /// </summary> public static async Task WaitForUnloadedAsync(this FrameworkElement frameworkElement) { await EventAsync.FromRoutedEvent( eh => frameworkElement.Unloaded += eh, eh => frameworkElement.Unloaded -= eh); }
/// <summary> /// Waits for the LostFocus event. /// Note that the event might not be raised if the focus has already been lost or is queued to be lost, /// so some polling might be advised if that is the case. /// </summary> /// <param name="control">The control whoe focuse loss is being awaited.</param> public static async Task WaitForLostFocusAsync(this UIElement control) { await EventAsync.FromRoutedEvent( eh => control.LostFocus += eh, eh => control.LostFocus -= eh); }
/// <summary> /// Waits for the button Click event. /// </summary> public static async Task <RoutedEventArgs> WaitForClickAsync(this ButtonBase button) { return(await EventAsync.FromRoutedEvent( eh => button.Click += eh, eh => button.Click -= eh)); }