Using BoxView with PanGesture and need it to take focus from all controls like button, on pressed event instead of released event

TazKing 85 Reputation points
2024-10-08T02:53:59.8533333+00:00

I'm using BoxView control with PanGesture to move window contents. It works fine and is very responsive as long as no other control in the window has the focus when I click BoxView. However, if an Editor has the focus when I click Boxview, the screen refresh rate is very slow and and laggy.

Upon releasing the mouse button, BoxView takes the focus and everything is fine.

I need the PanGesture routine to take the focus on the pressed event, instead of waiting for the release event. I don't understand the priorities of the mouse clicks in relation to the PanGesture well enough to solve this problem. Any ideas how to do this?

Here's my BoxView control and PanGesture handler


.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,082 questions
{count} votes

1 answer

Sort by: Most helpful
  1. TazKing 85 Reputation points
    2024-10-09T03:47:58.8333333+00:00

    Add a ghostControl to your code. It doesn't matter where since the control has 0 width and height (invisible). I used a Button control, but any control that can receive the focus will work (entry, editor, etc). BoxView and some others won't work since they can't get the focus.

    <Button x:Name="ghostControl" MinimumWidthRequest="0" WidthRequest="0" MinimumHeightRequest="0" HeightRequest="0" />
    
    

    Now, the control that's being dragged, needs a PointerEntered callback in addition to its PanUpdated callback.

                    <BoxView HeightRequest="15" WidthRequest="15" HorizontalOptions="End" Color="Gainsboro" BackgroundColor="White" CornerRadius="7,0,0,0" ToolTipProperties.Text="Click and drag to resize windows"  >
                        <!-- Gesture Recognizer to detect pan gestures -->
                        <BoxView.GestureRecognizers>
                            <PanGestureRecognizer PanUpdated="OnPanUpdated" />
                            <PointerGestureRecognizer PointerEntered="PointerGestureRecognizer_PointerEntered" />
                        </BoxView.GestureRecognizers>
                    </BoxView>
    
    		
    
    

    The PointerEntered callback is fired when the mouse moves over the BoxView and takes the focus from any control and sends the focus to the ghostControl with a Focus() command.

            // Take focus from any any control and send to ghost control
            private void PointerGestureRecognizer_PointerEntered(object sender, PointerEventArgs e)
            {
                ghostControl.Focus();
            }
    
    

    The BoxView can now be dragged and has a responsive screen redraw.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.