如何在 Xamarin Forms Android 中向左或向右滑动时禁用预测返回手势?

问题描述 投票:0回答:2

在我的 Xamarin Forms Android 应用程序中,当我们从屏幕边缘向左或向右滑动时,我试图禁用此预测后退手势,该应用程序将后退或前进,并且它会干扰我的选项卡式页面中的滑动。有时,当我们滑动切换标签时,它会意外地最小化应用程序。 所以我在我的自定义 ContentPage 渲染器中尝试了以下解决方案,但仍然没有用。 有人可以帮我吗?

我尝试了以下所有可能性,但问题仍然存在。

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace ServiceMattersApp.Droid.Renderers
{
    public class CustomPageRenderer : PageRenderer
    {
        public CustomPageRenderer(Context context) : base(context)
        {
        }
        protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
        {
            if (changed && Element != null)
            {
                
                Android.Graphics.Rect exclusionRect = new Android.Graphics.Rect(0, 0,0,0);
                var rects = new Android.Graphics.Rect[] { exclusionRect };
                SystemGestureExclusionRects = rects;
            }

            base.OnLayout(changed, left, top, right, bottom);
        }

    }
}

这是我尝试过的其他东西。

 var rootView = ((Activity)Context).FindViewById(Android.Resource.Id.Content);
                if (rootView != null)
                {
                    ViewCompat.SetSystemGestureExclusionRects(rootView, rects);
                    rootView.RootView.SystemGestureExclusionRects = rects;
                }

我什至在 OnElementChanged 方法中尝试了这些代码,但滑动仍然存在。 预期结果 - 应为屏幕的左侧和右侧禁用预测滑动系统手势。 谁能帮我解决这个问题

xamarin.forms xamarin.android swipe-gesture custom-renderer predictive-back
2个回答
0
投票

首先,您将

new Android.Graphics.Rect(0,0,0,0)
传递给排除矩形,这是一个大小为 0 的矩形。您反而想传递
new Android.Graphics.Rect(left, top, right, bottom)
.


然而,还有一个更根本的问题。根据

setSystemGestureExclusionRects
文档

系统将对其考虑的排除项的垂直范围设置 200dp 的限制。

换句话说,您的禁区最大可以是 200dp,或略高于 1 英寸,这使得该 API 基本上无用。

据我所知,我们没有好的解决方法。您可以挂钩“后退”回调,这至少可以防止页面返回,但不会阻止操作系统捕获滑动并且不会将其发送到您的应用程序。这个功能对我们这些使用绘图应用程序的人来说是一个巨大的中指,这些应用程序现在已经完全坏掉了,没有好的解决方法。


我能做的最好的事情就是在我的控制下将禁区垂直居中。这是代码:

protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
    base.OnLayout(changed, left, top, right, bottom);

    if (changed && Element != null)
    {
        const int maxHeightDp = 200; // Google restricts exclusion rects to 200dp because reasons
        int maxHeightPx = (int)(maxHeightDp * DeviceDisplay.MainDisplayInfo.Density);
        int controlHeightPx = bottom - top;
        int heightOffsetToVerticallyCenter = Math.Max((controlHeightPx - maxHeightPx) / 2, 0);
        int newTop = top + heightOffsetToVerticallyCenter;
        int newBottom = Math.Min(bottom, newTop + maxHeightPx);
        Android.Graphics.Rect exclusionRect = new Android.Graphics.Rect(left, newTop, right, newBottom);
        SystemGestureExclusionRects = new[] { exclusionRect };
    }
}

-1
投票

我不确定我是否理解您正在尝试做什么,但是使用“预测后退手势”一词令人困惑,因为它是为 Android 13 设备可以显示的动画保留的,而 Android 14 将在应用程序退出时显示,以防止应用程序过早退出因为滑动期间的动画将应用程序抽屉显示为预期目的地。

© www.soinside.com 2019 - 2024. All rights reserved.