Beispiel #1
0
 public MainVM(INav nav, IVmFactory _vmf, IAuth _auth)
 {
     Nav         = nav;
     AuthService = _auth;
     NavCommand  = new NavCommand(nav, _vmf);
     NavCommand.Execute(ViewType.Login);
 }
        // handle keyboard navigation (tabs and gamepad)
        private void HamburgerMenu_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            var currentItem = FocusManager.GetFocusedElement() as FrameworkElement;
            var lastItem    = LoadedNavButtons.FirstOrDefault(x => x.HamburgerButtonInfo == (SecondaryButtons.LastOrDefault(a => a != Selected) ?? PrimaryButtons.LastOrDefault(a => a != Selected)));

            var focus = new Func <FocusNavigationDirection, bool>(d =>
            {
                if (d == FocusNavigationDirection.Next)
                {
                    return(FocusManager.TryMoveFocus(d));
                }
                else if (d == FocusNavigationDirection.Previous)
                {
                    return(FocusManager.TryMoveFocus(d));
                }
                else
                {
                    var control = FocusManager.FindNextFocusableElement(d) as Control;
                    return(control?.Focus(FocusState.Programmatic) ?? false);
                }
            });

            var escape = new Func <bool>(() =>
            {
                if (DisplayMode == SplitViewDisplayMode.CompactOverlay ||
                    DisplayMode == SplitViewDisplayMode.Overlay)
                {
                    IsOpen = false;
                }
                if (Equals(ShellSplitView.PanePlacement, SplitViewPanePlacement.Left))
                {
                    ShellSplitView.Content.RenderTransform = new TranslateTransform {
                        X = 48 + ShellSplitView.OpenPaneLength
                    };
                    focus(FocusNavigationDirection.Right);
                    ShellSplitView.Content.RenderTransform = null;
                }
                else
                {
                    ShellSplitView.Content.RenderTransform = new TranslateTransform {
                        X = -48 - ShellSplitView.OpenPaneLength
                    };
                    focus(FocusNavigationDirection.Left);
                    ShellSplitView.Content.RenderTransform = null;
                }
                return(true);
            });

            var previous = new Func <bool>(() =>
            {
                if (Equals(currentItem, HamburgerButton))
                {
                    return(true);
                }
                else if (focus(FocusNavigationDirection.Previous) || focus(FocusNavigationDirection.Up))
                {
                    return(true);
                }
                else
                {
                    return(escape());
                }
            });

            var next = new Func <bool>(() =>
            {
                if (Equals(currentItem, HamburgerButton))
                {
                    return(focus(FocusNavigationDirection.Down));
                }
                else if (focus(FocusNavigationDirection.Next) || focus(FocusNavigationDirection.Down))
                {
                    return(true);
                }
                else
                {
                    return(escape());
                }
            });

            if (IsFullScreen)
            {
                return;
            }

            switch (e.Key)
            {
            case VirtualKey.Up:
            case VirtualKey.GamepadDPadUp:

                if (!(e.Handled = previous()))
                {
                    Debugger.Break();
                }
                break;

            case VirtualKey.Down:
            case VirtualKey.GamepadDPadDown:

                if (!(e.Handled = next()))
                {
                    Debugger.Break();
                }
                break;

            case VirtualKey.Right:
            case VirtualKey.GamepadDPadRight:
                if (SecondaryButtonContainer.Items.Contains(currentItem?.DataContext) &&
                    SecondaryButtonOrientation == Orientation.Horizontal)
                {
                    if (Equals(lastItem.FrameworkElement, currentItem))
                    {
                        if (!(e.Handled = escape()))
                        {
                            Debugger.Break();
                        }
                    }
                    else
                    {
                        if (!(e.Handled = next()))
                        {
                            Debugger.Break();
                        }
                    }
                }
                else
                {
                    if (!(e.Handled = escape()))
                    {
                        Debugger.Break();
                    }
                }
                break;

            case VirtualKey.Left:
            case VirtualKey.GamepadDPadLeft:

                if (SecondaryButtonContainer.Items.Contains(currentItem?.DataContext) &&
                    SecondaryButtonOrientation == Orientation.Horizontal)
                {
                    if (Equals(lastItem.FrameworkElement, currentItem))
                    {
                        if (!(e.Handled = escape()))
                        {
                            Debugger.Break();
                        }
                    }
                    else
                    {
                        if (!(e.Handled = previous()))
                        {
                            Debugger.Break();
                        }
                    }
                }
                else
                {
                    if (!(e.Handled = escape()))
                    {
                        Debugger.Break();
                    }
                }
                break;

            case VirtualKey.Space:
            case VirtualKey.Enter:
            case VirtualKey.GamepadA:

                if (currentItem != null)
                {
                    var info = new InfoElement(currentItem);
                    var hamburgerButtonInfo = info.HamburgerButtonInfo;
                    if (hamburgerButtonInfo != null)
                    {
                        NavCommand.Execute(hamburgerButtonInfo);
                    }
                }

                break;

            case VirtualKey.Escape:
            case VirtualKey.GamepadB:

                if (!(e.Handled = escape()))
                {
                    Debugger.Break();
                }
                break;
            }
        }