Пример #1
0
 /// <summary>
 /// Responds to DesktopWindowsXamlSource TakeFocusRequested event
 /// </summary>
 /// <param name="sender">DesktopWindowsXamlSource</param>
 /// <param name="args">DesktopWindowXamlSourceTakeFocusRequestedEventArgs</param>
 private void OnTakeFocusRequested(Windows.UI.Xaml.Hosting.DesktopWindowXamlSource sender, Windows.UI.Xaml.Hosting.DesktopWindowXamlSourceTakeFocusRequestedEventArgs args)
 {
     if (_lastFocusRequest == args.Request.CorrelationId)
     {
         // If we've arrived at this point, then focus is being move back to us
         // therefore, we should complete the operation to avoid an infinite recursion
         // by "Restoring" the focus back to us under a new correlationId
         var newRequest = new Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationRequest(
             Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.Restore);
         _xamlSource.NavigateFocus(newRequest);
         _lastFocusRequest = newRequest.CorrelationId;
     }
     else
     {
         // Focus was not initiated by WindowsXamlHost. Continue processing the Focus request.
         var reason = args.Request.Reason;
         if (reason == Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.First || reason == Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.Last)
         {
             var forward = reason == Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.First;
             _forceFocusNavigation = true;
             try
             {
                 Parent.SelectNextControl(this, forward, tabStopOnly: true, nested: false, wrap: true);
             }
             finally
             {
                 _forceFocusNavigation = false;
             }
         }
     }
 }
        /// <summary>
        /// Process Tab from host framework
        /// </summary>
        /// <param name="request"><see cref="System.Windows.Input.TraversalRequest"/> that contains requested navigation direction</param>
        /// <returns>Did handle tab</returns>
        protected override bool TabIntoCore(System.Windows.Input.TraversalRequest request)
        {
            if (_xamlSource.HasFocus && !_onTakeFocusRequested)
            {
                return(false); // If we have focus already, then we dont need to NavigateFocus
            }

            // Bug 17544829: Focus is wrong if the previous element is in a different FocusScope than the WindowsXamlHost element.
            var focusedElement = System.Windows.Input.FocusManager.GetFocusedElement(
                System.Windows.Input.FocusManager.GetFocusScope(this)) as FrameworkElement;

            var origin = BoundsRelativeTo(focusedElement, this);
            var reason = MapDirectionToReason[request.FocusNavigationDirection];

            if (_lastFocusRequest == Guid.Empty)
            {
                _lastFocusRequest = Guid.NewGuid();
            }

            var sourceFocusNavigationRequest = new WUX.Hosting.XamlSourceFocusNavigationRequest(reason, origin, _lastFocusRequest);

            try
            {
                var result = _xamlSource.NavigateFocus(sourceFocusNavigationRequest);

                // Returning true indicates that focus moved.  This will cause the HwndHost to
                // move focus to the source’s hwnd (call SetFocus Win32 API)
                return(result.WasFocusMoved);
            }
            finally
            {
                _lastFocusRequest = Guid.Empty;
            }
        }
Пример #3
0
        /// <summary>
        ///     Processes a tab key, ensuring that Xaml has an opportunity
        ///     to handle the command before normal Windows Forms processing.
        ///     (Xaml must be notified of keys that invoke focus navigation.)
        /// </summary>
        /// <returns>true if the command was processed</returns>
        protected override bool ProcessTabKey(bool forward)
        {
            // Determine if the currently focused element is the last element for the requested
            // navigation direction.  If the currently focused element is not the last element
            // for the requested navigation direction, navigate focus to the next focusable
            // element.
            if (!_xamlSource.HasFocus || _forceFocusNavigation)
            {
                _forceFocusNavigation = false;
                var reason  = forward ? Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.First : Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.Last;
                var request = new Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationRequest(reason, default(Windows.Foundation.Rect));
                _lastFocusRequest = request.CorrelationId;
                var result = _xamlSource.NavigateFocus(request);
                if (result.WasFocusMoved)
                {
                    return(true);
                }

                return(false);
            }
            else
            {
                // Temporary Focus handling for Redstone 5

                // Call Windows.UI.Xaml.Input.FocusManager.TryMoveFocus Next or Previous and return
                Windows.UI.Xaml.Input.FocusNavigationDirection navigationDirection =
                    forward ? Windows.UI.Xaml.Input.FocusNavigationDirection.Next : Windows.UI.Xaml.Input.FocusNavigationDirection.Previous;

                return(Windows.UI.Xaml.Input.FocusManager.TryMoveFocus(navigationDirection));
            }
        }
 /// <summary>
 /// Handles the <see cref="WUX.Hosting.DesktopWindowXamlSource.TakeFocusRequested" /> event.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="WUX.Hosting.DesktopWindowXamlSourceTakeFocusRequestedEventArgs"/> instance containing the event data.</param>
 private void OnTakeFocusRequested(object sender, WUX.Hosting.DesktopWindowXamlSourceTakeFocusRequestedEventArgs e)
 {
     if (_lastFocusRequest == e.Request.CorrelationId)
     {
         // If we've arrived at this point, then focus is being move back to us
         // therefore, we should complete the operation to avoid an infinite recursion
         // by "Restoring" the focus back to us under a new correctationId
         var newRequest = new WUX.Hosting.XamlSourceFocusNavigationRequest(
             WUX.Hosting.XamlSourceFocusNavigationReason.Restore);
         _xamlSource.NavigateFocus(newRequest);
     }
     else
     {
         _onTakeFocusRequested = true;
         try
         {
             // Last focus request is not initiated by us, so continue
             _lastFocusRequest = e.Request.CorrelationId;
             var direction = MapReasonToDirection[e.Request.Reason];
             var request   = new System.Windows.Input.TraversalRequest(direction);
             MoveFocus(request);
         }
         finally
         {
             _onTakeFocusRequested = false;
         }
     }
 }