Xamarin.Android 中的 HackyViewPager

HackyViewPager in Xamarin.Android

我正在尝试将这个用于 PhotoView 的 HackyViewPager 转换为 Xamarin.Android 的版本,但我现在不知道如何去做。

这是扩展 ViewPager

的 HackyViewPager class
public class HackyViewPager extends ViewPager {

    private boolean isLocked;

    public HackyViewPager(Context context) {
        super(context);
        isLocked = false;
    }

    public HackyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        isLocked = false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!isLocked) {
            try {
                return super.onInterceptTouchEvent(ev);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isLocked) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    public void toggleLock() {
        isLocked = !isLocked;
    }

    public void setLocked(boolean isLocked) {
        this.isLocked = isLocked;
    }

    public boolean isLocked() {
        return isLocked;
    }

}

一个简单的解决方案是将 java 转换为 C#,如下所示

using System;
using Android.Support.V4.View;
using Android.Content;
using Java.Lang;
using Android.Views;
using Android.Util;

public class HackyViewPager : ViewPager
{

    private bool IsLocked { get; set; }

    public HackyViewPager (Context context)
        : base (context)
    {
        IsLocked = false;
    }

    public HackyViewPager (Context context, IAttributeSet attrs)
        : base (context, attrs)
    {
        IsLocked = false;
    }

    public override bool OnInterceptTouchEvent (MotionEvent ev)
    {
        if (!IsLocked) {
            try {
                return base.OnInterceptTouchEvent (ev);
            } catch (IllegalArgumentException e) {
                return false;
            }
        }
        return false;
    }

    public override bool OnTouchEvent (MotionEvent ev)
    {
        if (!IsLocked) {
            return base.OnTouchEvent (ev);
        }
        return false;
    }

    public void ToggleLock ()
    {
        IsLocked = !IsLocked;
    }

}