使用Xamarin表单从页面上删除后向滑动手势

时间:2019-02-21 15:40:19

标签: xamarin xamarin.forms

有没有办法在我的项目的一页上禁用iOS的向后滑动到上一页的选项?

2 个答案:

答案 0 :(得分:1)

您可以通过实现自定义渲染器并为此设置正确的属性来实现。您可以在下面看到一个示例实现。在这种情况下,正确的属性是InteractivePopGestureRecognizer,您需要将其设置为false。

ViewWillAppear中执行此操作,以初始化NavigationController

using DisableSwipe.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ContentPage), typeof(NoBackSwipeRenderer))]
namespace DisableSwipe.iOS
{
    public class NoBackSwipeRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            if (ViewController != null && ViewController.NavigationController != null)
                ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = false;
        }
    }
}

答案 1 :(得分:1)

@Symorp 您可以这样做:

public class YourCustomPageRenderer : PageRenderer
{
    private YourCustomPage _yourCustomPage;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        _yourCustomPage = e.NewElement as YourCustomPage;

        if (_yourCustomPage != null)
        {            
            _yourCustomPage.PropertyChanged += YourCustomPagePropertyChangedEventHandler;
        }
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        SetInteractivePopGestureRecognizerEnabled(isEnabled: false);
    }

    private void YourCustomPagePropertyChangedEventHandler(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        if (propertyChangedEventArgs.PropertyName == nameof(YourCustomPage.IsInteractivePopGestureRecognizerEnabled))
        {
            SetInteractivePopGestureRecognizerEnabled(_yourCustomPage.IsInteractivePopGestureRecognizerEnabled);
        }
    }

    private void SetInteractivePopGestureRecognizerEnabled(bool isEnabled)
    {
        var interactivePopGestureRecognizer = ViewController?.NavigationController?.InteractivePopGestureRecognizer;

        if (interactivePopGestureRecognizer != null)
        {
            //Prevents the back-swipe-gesture when the user wants to swipe a page away (from left edge of the screen)
            interactivePopGestureRecognizer.Enabled = isEnabled;
        }
    }
}

public class YourCustomPage : ContentPage
{
    /// <summary>
    /// If you need it as bindable property, feel free to create a <see cref="BindableProperty"/>.
    /// </summary>
    public bool IsInteractivePopGestureRecognizerEnabled { get; set; }
}

随时调整以适应您的需求! :-)

为简单起见,我省略了export renderer属性等。