diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs | 260 |
1 files changed, 163 insertions, 97 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs index 89427e1ed..64e2dbf84 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; @@ -12,6 +13,7 @@ using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Threading; +using Tango.Core; using Tango.SharedUI.Helpers; namespace Tango.SharedUI.Controls @@ -19,7 +21,15 @@ namespace Tango.SharedUI.Controls [ContentProperty(nameof(Elements))] public class NavigationControl : UserControl { - private Action _onCompleted; + private event Action NavigationCompleted; + private Thread _navigationThread; + private ProducerConsumerQueue<NavigationQueueItem> _navigationQueue; + + private class NavigationQueueItem + { + public NavigationElement FromElement { get; set; } + public NavigationElement ToElement { get; set; } + } #region Transition Types @@ -296,6 +306,11 @@ namespace Tango.SharedUI.Controls Content = _grid; Elements = new ObservableCollection<FrameworkElement>(); Loaded += NavigationControl_Loaded; + + _navigationQueue = new ProducerConsumerQueue<NavigationQueueItem>(); + _navigationThread = new Thread(NavigationThread); + _navigationThread.IsBackground = true; + _navigationThread.Start(); } #endregion @@ -409,135 +424,174 @@ namespace Tango.SharedUI.Controls private void Navigate(NavigationElement fromElement, NavigationElement toElement) { - if (toElement == null || toElement == fromElement) + _navigationQueue.BlockEnqueue(new NavigationQueueItem() { - _onCompleted?.Invoke(); - } + FromElement = fromElement, + ToElement = toElement, + }); + } - if (fromElement != null) + private void NavigationThread() + { + while (true) { - DoubleAnimation toAnimation = new DoubleAnimation(); - toAnimation.Duration = TransitionDuration; + var item = _navigationQueue.BlockDequeue(); - DoubleAnimation fromAnimation = new DoubleAnimation(); - fromAnimation.Duration = TransitionDuration; + var navigationCompleted = false; - int fromIndex = Elements.IndexOf(fromElement.Element); - int toIndex = Elements.IndexOf(toElement.Element); + NavigationElement fromElement = item.FromElement; + NavigationElement toElement = item.ToElement; - fromElement.Reset(); - toElement.Reset(); - - fromAnimation.Completed += (_, __) => + Dispatcher.BeginInvoke(new Action(() => { - fromElement.Deactivate(!KeepElementsAttached); - }; - - bool completed = false; + if (toElement == null || toElement == fromElement) + { + //navigationCompleted = true; + Debug.WriteLine("NavigationControl: THIS MIGHT CAUSE PROBLEMS !!"); + } - toAnimation.Completed += (_, __) => - { - if (!completed) + if (fromElement != null) { - completed = true; + DoubleAnimation toAnimation = new DoubleAnimation(); + toAnimation.Duration = TransitionDuration; + + DoubleAnimation fromAnimation = new DoubleAnimation(); + fromAnimation.Duration = TransitionDuration; + + int fromIndex = Elements.IndexOf(fromElement.Element); + int toIndex = Elements.IndexOf(toElement.Element); - INavigationView fromNavigationView = fromElement.Element as INavigationView; - INavigationView toNavigationView = toElement.Element as INavigationView; + fromElement.Reset(); + toElement.Reset(); - if (fromNavigationView != null) + fromAnimation.Completed += (_, __) => { - fromNavigationView.OnNavigatedFrom(); - } - if (toNavigationView != null) + fromElement.Deactivate(!KeepElementsAttached); + }; + + bool completed = false; + + toAnimation.Completed += (_, __) => { - toNavigationView.OnNavigatedTo(); - } + if (!completed) + { + completed = true; - INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + INavigationView fromNavigationView = fromElement.Element as INavigationView; + INavigationView toNavigationView = toElement.Element as INavigationView; - if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); - if (toVM != null) toVM.OnNavigatedTo(); + if (fromNavigationView != null) + { + fromNavigationView.OnNavigatedFrom(); + } + if (toNavigationView != null) + { + toNavigationView.OnNavigatedTo(); + } - _onCompleted?.Invoke(); - } - }; + INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel; + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - switch (TransitionType) - { - case TransitionTypes.Fade: - fromAnimation.From = 1; - fromAnimation.To = 0; + if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom(); + if (toVM != null) toVM.OnNavigatedTo(); - toAnimation.From = 0; - toAnimation.To = 1; + navigationCompleted = true; + } + }; - fromElement.AnimateOpacity(fromAnimation); - toElement.AnimateOpacity(toAnimation); + switch (TransitionType) + { + case TransitionTypes.Fade: + fromAnimation.From = 1; + fromAnimation.To = 0; - break; - case TransitionTypes.Zoom: - fromAnimation.From = 1; - fromAnimation.To = 0; + toAnimation.From = 0; + toAnimation.To = 1; - toAnimation.From = 0; - toAnimation.To = 1; + fromElement.AnimateOpacity(fromAnimation); + toElement.AnimateOpacity(toAnimation); - fromElement.AnimateScale(fromAnimation); - toElement.AnimateScale(toAnimation); + break; + case TransitionTypes.Zoom: + fromAnimation.From = 1; + fromAnimation.To = 0; - break; - case TransitionTypes.Slide: + toAnimation.From = 0; + toAnimation.To = 1; - if (toIndex > fromIndex) - { - fromAnimation.From = 0; - fromAnimation.To = -ActualWidth; + fromElement.AnimateScale(fromAnimation); + toElement.AnimateScale(toAnimation); + + break; + case TransitionTypes.Slide: + + if (toIndex > fromIndex) + { + fromAnimation.From = 0; + fromAnimation.To = -ActualWidth; - toAnimation.From = ActualWidth; - toAnimation.To = 0; + toAnimation.From = ActualWidth; + toAnimation.To = 0; + } + else + { + fromAnimation.From = 0; + fromAnimation.To = ActualWidth; + + toAnimation.From = -ActualWidth; + toAnimation.To = 0; + } + + fromElement.AnimateTranslateX(fromAnimation); + toElement.AnimateTranslateX(toAnimation); + + break; } - else + + if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) { - fromAnimation.From = 0; - fromAnimation.To = ActualWidth; + DoubleAnimation fromFadeAnimation = new DoubleAnimation(); + fromFadeAnimation.From = 1; + fromFadeAnimation.To = 0; + fromFadeAnimation.Duration = TransitionDuration; - toAnimation.From = -ActualWidth; - toAnimation.To = 0; - } + DoubleAnimation toFadeAnimation = new DoubleAnimation(); + toFadeAnimation.From = 0; + toFadeAnimation.To = 1; + toFadeAnimation.Duration = TransitionDuration; - fromElement.AnimateTranslateX(fromAnimation); - toElement.AnimateTranslateX(toAnimation); + fromElement.AnimateOpacity(fromFadeAnimation); + toElement.AnimateOpacity(toFadeAnimation); + } - break; - } + fromElement.Activate(); + toElement.Activate(); + } + else + { + toElement.Activate(); + toElement.Reset(); - if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade) - { - DoubleAnimation fromFadeAnimation = new DoubleAnimation(); - fromFadeAnimation.From = 1; - fromFadeAnimation.To = 0; - fromFadeAnimation.Duration = TransitionDuration; + INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; + if (toVM != null) toVM.OnNavigatedTo(); - DoubleAnimation toFadeAnimation = new DoubleAnimation(); - toFadeAnimation.From = 0; - toFadeAnimation.To = 1; - toFadeAnimation.Duration = TransitionDuration; + navigationCompleted = true; + } + })); - fromElement.AnimateOpacity(fromFadeAnimation); - toElement.AnimateOpacity(toFadeAnimation); + for (int i = 0; i < 30; i++) + { + if (navigationCompleted) + { + break; + } + Thread.Sleep(100); } - fromElement.Activate(); - toElement.Activate(); - } - else - { - toElement.Activate(); - toElement.Reset(); - - INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel; - if (toVM != null) toVM.OnNavigatedTo(); + Dispatcher.BeginInvoke(new Action(() => + { + NavigationCompleted?.Invoke(); + })); } } @@ -571,7 +625,19 @@ namespace Tango.SharedUI.Controls public FrameworkElement NavigateTo(String navigationName, Action onCompleted = null) { - _onCompleted = onCompleted; + Action completed = null; + + completed = () => + { + try + { + onCompleted?.Invoke(); + } + catch { } + NavigationCompleted -= completed; + }; + + NavigationCompleted += completed; var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName); @@ -586,7 +652,7 @@ namespace Tango.SharedUI.Controls if (SelectedElement == element) { await Task.Delay((int)TransitionDuration.TimeSpan.TotalMilliseconds * 2); - _onCompleted?.Invoke(); + NavigationCompleted?.Invoke(); } else { |
