How can I make a BoxView react when a user drags or swipes it?

Platform: .NET MAUI| Category: Controls

In .NET MAUI, you can make a BoxView respond when a user drags or swipes it by using something called PanGestureRecognizer. Here’s an example:

myBoxView = new BoxView()
    {
        WidthRequest = 200,
        HeightRequest = 200,
        Color=Colors.Blue
    };
    var gestureRecognizer = new PanGestureRecognizer();
    gestureRecognizer.PanUpdated += (sender, e) =>
    {
        switch (e.StatusType)
        {
            case GestureStatus.Started:
                break;
            case GestureStatus.Running:
                double xOffset = myBoxView.TranslationX + e.TotalX;
                double yOffset = myBoxView.TranslationY + e.TotalY;
                myBoxView.TranslationX = xOffset;
                myBoxView.TranslationY = yOffset;
                break;
            case GestureStatus.Completed:
                break;
        }
    };

    myBoxView.GestureRecognizers.Add(gestureRecognizer);
    StackLayout views = new StackLayout()
    {
        Children = {myBoxView},
        VerticalOptions = LayoutOptions.CenterAndExpand,
        HorizontalOptions = LayoutOptions.CenterAndExpand
    };
    Content = views;

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.