aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Controls/NavigationControl.cs
blob: b3869daffa71f7665191c3f31544df432811df5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Upgrade.Navigation;
using Tango.FSE.Upgrade.Views;
using Tango.SharedUI.Helpers;

namespace Tango.FSE.Upgrade.ViewModels
{
    [ModularNavigationContainer]
    public class MainViewVM : RemoteUpgradeViewModel
    {
        public override void OnApplicationReady()
        {
            InvokeUI(() =>
            {
                NavigationManager.MenuItems.Add(new NavigationMenuItem(() =>
                {
                    NavigationManager.NavigateTo<UpgradeModule>();
                })
                {
                    Name = "Remote Upgrade",
                    Index = 4,
                    Description = "Upgrade or downgrade the machine application and firmware versions remotely.",
                    Image = ResourceHelper.GetImageFromResources("Images/remote_upgrade.png"),
                });
            });
        }

        public async override Task<bool> OnNavigateBackRequest()
        {
            if (ModularNavigationManager.CurrentView == RemoteUpgradeView.WelcomeView)
            {
                return await base.OnNavigateBackRequest();
            }
            else
            {
                await ModularNavigationManager.NavigateBack();
                return false;
            }
        }
    }
}
' href='#n420'>420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Tango.SharedUI.Helpers;

namespace Tango.SharedUI.Controls
{
    [ContentProperty(nameof(Elements))]
    public class NavigationControl : UserControl
    {
        private Action _onCompleted;

        #region Transition Types

        public enum TransitionTypes
        {
            Fade,
            Zoom,
            Slide,
        }

        #endregion

        #region Navigation Element

        private class NavigationElement : ContentControl
        {
            public FrameworkElement Element
            {
                get { return (FrameworkElement)GetValue(ElementProperty); }
                set { SetValue(ElementProperty, value); }
            }
            public static readonly DependencyProperty ElementProperty =
                DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null));

            public ScaleTransform Scale
            {
                get { return (ScaleTransform)GetValue(ScaleProperty); }
                set { SetValue(ScaleProperty, value); }
            }
            public static readonly DependencyProperty ScaleProperty =
                DependencyProperty.Register("Scale", typeof(ScaleTransform), typeof(NavigationControl), new PropertyMetadata(null));

            public RotateTransform Rotate
            {
                get { return (RotateTransform)GetValue(RotateProperty); }
                set { SetValue(RotateProperty, value); }
            }
            public static readonly DependencyProperty RotateProperty =
                DependencyProperty.Register("Rotate", typeof(RotateTransform), typeof(NavigationControl), new PropertyMetadata(null));

            public TranslateTransform Translate
            {
                get { return (TranslateTransform)GetValue(TranslateProperty); }
                set { SetValue(TranslateProperty, value); }
            }
            public static readonly DependencyProperty TranslateProperty =
                DependencyProperty.Register("Translate", typeof(TranslateTransform), typeof(NavigationControl), new PropertyMetadata(null));

            public NavigationElement()
            {
                Visibility = Visibility.Hidden;

                Scale = new ScaleTransform(1, 1);
                Rotate = new RotateTransform(0);
                Translate = new TranslateTransform(0, 0);

                TransformGroup group = new TransformGroup();
                group.Children.Add(Scale);
                group.Children.Add(Rotate);
                group.Children.Add(Translate);

                RenderTransformOrigin = new Point(0.5, 0.5);
                RenderTransform = group;
            }

            public NavigationElement(FrameworkElement element) : this()
            {
                Element = element;
            }

            public void AnimateScale(DoubleAnimation animation)
            {
                Scale.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
                Scale.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
            }

            public void AnimateTranslateX(DoubleAnimation animation)
            {
                Translate.BeginAnimation(TranslateTransform.XProperty, animation);
            }

            public void AnimateOpacity(DoubleAnimation animation)
            {
                BeginAnimation(OpacityProperty, animation);
            }

            public void Reset()
            {
                Scale.BeginAnimation(ScaleTransform.ScaleXProperty, null);
                Scale.BeginAnimation(ScaleTransform.ScaleYProperty, null);
                Rotate.BeginAnimation(RotateTransform.AngleProperty, null);
                Translate.BeginAnimation(TranslateTransform.XProperty, null);
                Translate.BeginAnimation(TranslateTransform.YProperty, null);
                Opacity = 1;
            }

            public void Activate()
            {
                if (Content != Element)
                {
                    Content = Element;
                }

                Visibility = Visibility.Visible;
            }

            public void Deactivate(bool detach)
            {
                if (detach)
                {
                    Content = null;
                }

                Visibility = Visibility.Hidden;
            }
        }

        #endregion

        #region INavigationView

        public interface INavigationView
        {
            void OnNavigatedTo();
            void OnNavigatedFrom();
        }

        #endregion

        #region INavigationViewModel

        /// <summary>
        /// Represents a view data context which can be notified when its view is being navigated in or out.
        /// </summary>
        public interface INavigationViewModel
        {
            /// <summary>
            /// Called when the navigation system has navigated to this VM view.
            /// </summary>
            void OnNavigatedTo();

            /// <summary>
            /// Called when the navigation system has navigated from this VM view.
            /// </summary>
            void OnNavigatedFrom();
        }

        #endregion

        private Grid _grid;
        private bool _loaded;

        #region Properties

        public ObservableCollection<FrameworkElement> Elements
        {
            get { return (ObservableCollection<FrameworkElement>)GetValue(ElementsProperty); }
            set { SetValue(ElementsProperty, value); }
        }
        public static readonly DependencyProperty ElementsProperty =
            DependencyProperty.Register("Elements", typeof(ObservableCollection<FrameworkElement>), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnElementsChanged()));

        public FrameworkElement SelectedElement
        {
            get { return (FrameworkElement)GetValue(SelectedElementProperty); }
            set { SetValue(SelectedElementProperty, value); }
        }
        public static readonly DependencyProperty SelectedElementProperty =
            DependencyProperty.Register("SelectedElement", typeof(FrameworkElement), typeof(NavigationControl), new PropertyMetadata(null, (d, e) => (d as NavigationControl).OnSelectedElementChanged(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement)));

        public TransitionTypes TransitionType
        {
            get { return (TransitionTypes)GetValue(TransitionTypeProperty); }
            set { SetValue(TransitionTypeProperty, value); }
        }
        public static readonly DependencyProperty TransitionTypeProperty =
            DependencyProperty.Register("TransitionType", typeof(TransitionTypes), typeof(NavigationControl), new PropertyMetadata(TransitionTypes.Zoom));

        public bool TransitionAlwaysFades
        {
            get { return (bool)GetValue(TransitionAlwaysFadesProperty); }
            set { SetValue(TransitionAlwaysFadesProperty, value); }
        }
        public static readonly DependencyProperty TransitionAlwaysFadesProperty =
            DependencyProperty.Register("TransitionAlwaysFades", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));

        public Duration TransitionDuration
        {
            get { return (Duration)GetValue(TransitionDurationProperty); }
            set { SetValue(TransitionDurationProperty, value); }
        }
        public static readonly DependencyProperty TransitionDurationProperty =
            DependencyProperty.Register("TransitionDuration", typeof(Duration), typeof(NavigationControl), new PropertyMetadata(new Duration(TimeSpan.FromSeconds(0.5))));

        public bool KeepElementsAttached
        {
            get { return (bool)GetValue(KeepElementsAttachedProperty); }
            set { SetValue(KeepElementsAttachedProperty, value); }
        }
        public static readonly DependencyProperty KeepElementsAttachedProperty =
            DependencyProperty.Register("KeepElementsAttached", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));



        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedIndex.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(NavigationControl), new PropertyMetadata(0, (d, e) => (d as NavigationControl).OnSelectedIndexChanged()));

        public bool UseDefferedRendering
        {
            get { return (bool)GetValue(UseDefferedRenderingProperty); }
            set { SetValue(UseDefferedRenderingProperty, value); }
        }
        public static readonly DependencyProperty UseDefferedRenderingProperty =
            DependencyProperty.Register("UseDefferedRendering", typeof(bool), typeof(NavigationControl), new PropertyMetadata(false));



        private void OnSelectedIndexChanged()
        {
            SelectedElement = Elements[SelectedIndex > Elements.Count - 1 ? 0 : SelectedIndex];
        }

        #endregion

        #region Attached Properties

        #region NavigationName

        /// <summary>
        /// Determines the element navigation name.
        /// </summary>
        public static readonly DependencyProperty NavigationName =
            DependencyProperty.RegisterAttached("NavigationName",
            typeof(String), typeof(NavigationControl),
            new FrameworkPropertyMetadata(""));

        /// <summary>
        /// Sets the name of the navigation.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetNavigationName(FrameworkElement element, String value)
        {
            element.SetValue(NavigationName, value);
        }

        /// <summary>
        /// Gets the name of the navigation.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static String GetNavigationName(FrameworkElement element)
        {
            return element.GetValue(NavigationName).ToStringSafe();
        }

        #endregion

        #endregion

        #region Constructors

        public NavigationControl()
        {
            ClipToBounds = true;
            _grid = new Grid();
            Content = _grid;
            Elements = new ObservableCollection<FrameworkElement>();
            Loaded += NavigationControl_Loaded;
        }

        #endregion

        #region Event Handlers

        private void NavigationControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!_loaded)
            {
                _loaded = true;

                if (Elements != null)
                {
                    //foreach (var element in Elements.Where(x => x != SelectedElement))
                    //{
                    //    LoadVisual(element);
                    //}

                    SelectedElement = Elements.FirstOrDefault();
                }
            }
        }

        private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            SyncElements();
        }

        #endregion

        #region Private Methods

        private void OnElementsChanged()
        {
            if (Elements != null)
            {
                Elements.CollectionChanged -= Elements_CollectionChanged;
                Elements.CollectionChanged += Elements_CollectionChanged;
            }

            SyncElements();
        }

        private void SyncElements()
        {
            if (Elements == null)
            {
                _grid.Children.Clear();
            }
            else
            {
                var toRemove = _grid.Children.OfType<NavigationElement>().ToList().Where(x => !Elements.Contains(x.Element)).ToList();
                var toAdd = Elements.Where(x => !_grid.Children.OfType<NavigationElement>().Select(y => y.Element).ToList().Contains(x)).ToList();

                toRemove.ForEach(x => _grid.Children.Remove(x));

                if (UseDefferedRendering)
                {
                    foreach (var x in toAdd)
                    {
                        var navigationElement = new NavigationElement()
                        {
                            RenderTransformOrigin = new Point(0.5, 0.5),
                            Element = x,
                            Content = x,
                        };

                        _grid.Children.Add(navigationElement);

                        if (!KeepElementsAttached)
                        {
                            bool loaded = false;

                            navigationElement.Loaded += (_, __) =>
                            {
                                if (!loaded)
                                {
                                    if (x != SelectedElement)
                                    {
                                        loaded = true;
                                        navigationElement.Content = null;
                                    }
                                }
                            };
                        }
                    }
                }
                else
                {
                    toAdd.ForEach(x => _grid.Children.Add(
                    new NavigationElement()
                    {
                        RenderTransformOrigin = new Point(0.5, 0.5),
                        Element = x,
                        Content = KeepElementsAttached ? x : null,
                    }));
                }
            }

            if (!Elements.Contains(SelectedElement))
            {
                SelectedElement = null;
            }
        }

        private NavigationElement GetElementContainer(FrameworkElement element)
        {
            return _grid.Children.OfType<NavigationElement>().ToList().SingleOrDefault(x => x.Element == element);
        }

        private void Navigate(NavigationElement fromElement, NavigationElement toElement)
        {
            if (toElement == null || toElement == fromElement)
            {
                _onCompleted?.Invoke();
            }

            if (fromElement != null)
            {
                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);

                fromElement.Reset();
                toElement.Reset();

                fromAnimation.Completed += (_, __) =>
                {
                    fromElement.Deactivate(!KeepElementsAttached);
                };

                bool completed = false;

                toAnimation.Completed += (_, __) =>
                {
                    if (!completed)
                    {
                        completed = true;

                        INavigationView fromNavigationView = fromElement.Element as INavigationView;
                        INavigationView toNavigationView = toElement.Element as INavigationView;

                        if (fromNavigationView != null)
                        {
                            fromNavigationView.OnNavigatedFrom();
                        }
                        if (toNavigationView != null)
                        {
                            toNavigationView.OnNavigatedTo();
                        }

                        INavigationViewModel fromVM = fromElement.Element.DataContext as INavigationViewModel;
                        INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel;

                        if (fromVM != null && fromVM != toVM) fromVM.OnNavigatedFrom();
                        if (toVM != null) toVM.OnNavigatedTo();

                        _onCompleted?.Invoke();
                    }
                };

                switch (TransitionType)
                {
                    case TransitionTypes.Fade:
                        fromAnimation.From = 1;
                        fromAnimation.To = 0;

                        toAnimation.From = 0;
                        toAnimation.To = 1;

                        fromElement.AnimateOpacity(fromAnimation);
                        toElement.AnimateOpacity(toAnimation);

                        break;
                    case TransitionTypes.Zoom:
                        fromAnimation.From = 1;
                        fromAnimation.To = 0;

                        toAnimation.From = 0;
                        toAnimation.To = 1;

                        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;
                        }
                        else
                        {
                            fromAnimation.From = 0;
                            fromAnimation.To = ActualWidth;

                            toAnimation.From = -ActualWidth;
                            toAnimation.To = 0;
                        }

                        fromElement.AnimateTranslateX(fromAnimation);
                        toElement.AnimateTranslateX(toAnimation);

                        break;
                }

                if (TransitionAlwaysFades && TransitionType != TransitionTypes.Fade)
                {
                    DoubleAnimation fromFadeAnimation = new DoubleAnimation();
                    fromFadeAnimation.From = 1;
                    fromFadeAnimation.To = 0;
                    fromFadeAnimation.Duration = TransitionDuration;

                    DoubleAnimation toFadeAnimation = new DoubleAnimation();
                    toFadeAnimation.From = 0;
                    toFadeAnimation.To = 1;
                    toFadeAnimation.Duration = TransitionDuration;

                    fromElement.AnimateOpacity(fromFadeAnimation);
                    toElement.AnimateOpacity(toFadeAnimation);
                }

                fromElement.Activate();
                toElement.Activate();
            }
            else
            {
                toElement.Activate();
                toElement.Reset();

                INavigationViewModel toVM = toElement.Element.DataContext as INavigationViewModel;
                if (toVM != null) toVM.OnNavigatedTo();
            }
        }

        #endregion

        #region Properties Changes

        protected virtual void OnSelectedElementChanged(FrameworkElement fromElement, FrameworkElement toElement)
        {
            Navigate(GetElementContainer(fromElement), GetElementContainer(toElement));
        }

        #endregion

        #region Public Methods

        public void NavigateTo(FrameworkElement element)
        {
            var e = Elements.SingleOrDefault(x => x == element);

            if (e != null)
            {
                SelectedElement = element;
            }
        }

        public FrameworkElement NavigateTo(String navigationName)
        {
            return NavigateTo(navigationName, null);
        }

        public FrameworkElement NavigateTo(String navigationName, Action onCompleted = null)
        {
            _onCompleted = onCompleted;

            var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName);

            if (element != null)
            {
                if (SelectedElement == element)
                {
                    _onCompleted?.Invoke();
                }
                else
                {
                    SelectedElement = element;
                }
            }

            return element;
        }

        public FrameworkElement GetElement(String navigationName)
        {
            return Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName || x.GetType().Name == navigationName);
        }

        public FrameworkElement GetAndDetach(String navigationName)
        {
            var element = Elements.SingleOrDefault(x => GetNavigationName(x) == navigationName);
            GetElementContainer(element).Deactivate(!KeepElementsAttached);
            return element;
        }

        /// <remarks>
        /// This method needs to be called in order for 
        //  the element to print visibly at the correct size.
        /// </remarks>
        private static void LoadVisual(FrameworkElement element)
        {
            if (element.Parent == null)
            {
                try
                {
                    System.Windows.Forms.UserControl controlContainer = new System.Windows.Forms.UserControl();
                    controlContainer.Width = 100;
                    controlContainer.Height = 100;
                    var host = new ElementHost() { Child = element, Dock = System.Windows.Forms.DockStyle.Fill };
                    controlContainer.Controls.Add(host);
                    IntPtr handle = controlContainer.Handle;
                    host.Child = null;
                    controlContainer.Dispose();
                }
                catch { }
            }
        }

        #endregion
    }
}