在 Xamarin.Forms 中,我希望能够左右滑动以浏览图片列表。目前,我只想能够在每次检测到滑动时触发一个事件。

要在渲染器中使用的子类:

public class LRMasterDetailPage : ContentView
{
}

我有一个使用 LRM 类的 ContentPage,如下所示:
    public class ImagePage : ContentPage
    {

    public ImagePage(Photo photo)
        {
            _image = new WebImage
                {
                    Url = photo.Url,
                    Placeholder = "placeHolder2.png"
                };

            var imageView = new LRMasterDetailPage {
                Content = _image
            };

            this.Content = imageView;
           }
      }

这是我的渲染器:
    [assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Project.iOS
{
    public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
    {
        UISwipeGestureRecognizer swipe;

        protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
        {
            base.OnElementChanged (e);
            // Do someting else, init for example
            swipe = new UISwipeGestureRecognizer();
            this.AddGestureRecognizer (swipe);

            if (swipe.Direction == UISwipeGestureRecognizerDirection.Left)
            {
                UpdateLeft ();
            }

            if (swipe.Direction == UISwipeGestureRecognizerDirection.Right)
            {
                UpdateRight ();
            }
        }

        protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Renderer")
                return;
            base.OnElementPropertyChanged (sender, e);

        }

        private void UpdateLeft(){
            // Insert view of DetailLeft element into subview
            // Add button to open Detail to parent navbar, if not yet there
            // Add gesture recognizer for left swipe
            Console.WriteLine ("Left swipe");

        }
        private void UpdateRight(){
            // same as for left, but flipped
            Console.WriteLine ("Right swipe");

        }
    }
}

当 ContentPage 显示时,向右滑动事件被激活,但是当我尝试在图像顶部滑动时没有任何 react 。我猜我在渲染器上的逻辑是错误的?

最佳答案

经过一番痛苦并在网上搜索后,我找到了解决方案。

您需要做的很简单:在渲染器中声明并添加手势。通过声明方向,确保为左右各创建一个滑动手势。从那里使用 lambda 调用要为特定滑动激活的函数:

滑动手势的占位符类。

public class LRMasterDetailPage : ContentView
    {

    }

一次保存一张图像的图像页面
public class ImagePage : ContentPage
{

    //view holding the image
    LRMasterDetailPage imageView;

    //collection of images using the photo.Url
    ObservableCollection<Image> images;

    //current image index
    int index = 0;

public ImagePage(){

       images = new ObservableCollection<Image> ();
       imageView = new LRMasterDetailPage {
            Content = this.images [index]
        };

        this.Content = imageView;
}

 //Subscribe to the swipeLeft and swipeRight message
  protected override void OnAppearing ()
    {
        base.OnAppearing ();

        MessagingCenter.Subscribe <string> (this,"LeftSwipe", (sender) => {

            //Do something
            if(index < images.Count-1){
                index++;
            }

            imageView.Content = this.images[index];


        });
        MessagingCenter.Subscribe <string> (this, "RightSwipe", (sender) => {

            //Do something
            if(index > 0){
                index--;
            }

            imageView.Content = this.images[index];


        });
   }

     protected override void OnDisappearing()
    {
        base.OnDisappearing();

        //this._image = null;

        images = null;

        MessagingCenter.Unsubscribe<string>(this,"LeftSwipe");
        MessagingCenter.Unsubscribe<string>(this, "RightSwipe");
        MessagingCenter.Unsubscribe<string>(this, "LongPress");

        //GC.Collect();
    }

}

LRMasterDetailPage 的渲染器
    [assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Manager.iOS
{
    public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
    {

        UILongPressGestureRecognizer longPressGestureRecognizer;
        UIPinchGestureRecognizer pinchGestureRecognizer;
        //UIPanGestureRecognizer panGestureRecognizer;
        UISwipeGestureRecognizer swipeRightGestureRecognizer;
        UISwipeGestureRecognizer swipeLeftGestureRecognizer;
        UIRotationGestureRecognizer rotationGestureRecognizer;

        protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
        {
            base.OnElementChanged (e);

            longPressGestureRecognizer = new UILongPressGestureRecognizer (() => Console.WriteLine ("Long Press"));
            pinchGestureRecognizer = new UIPinchGestureRecognizer (() => Console.WriteLine ("Pinch"));
            //panGestureRecognizer = new UIPanGestureRecognizer (() => Console.WriteLine ("Pan"));

            swipeRightGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateRight()){Direction = UISwipeGestureRecognizerDirection.Right};
            swipeLeftGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateLeft()){Direction = UISwipeGestureRecognizerDirection.Left};
            rotationGestureRecognizer = new UIRotationGestureRecognizer (() => Console.WriteLine ("Rotation"));

            if (e.NewElement == null) {
                if (longPressGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (longPressGestureRecognizer);
                }
                if (pinchGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (pinchGestureRecognizer);
                }

                /*
                if (panGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (panGestureRecognizer);
                }
                */

                if (swipeRightGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (swipeRightGestureRecognizer);
                }
                if (swipeLeftGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (swipeLeftGestureRecognizer);
                }

                if (rotationGestureRecognizer != null) {
                    this.RemoveGestureRecognizer (rotationGestureRecognizer);
                }
            }

            if (e.OldElement == null) {
                this.AddGestureRecognizer (longPressGestureRecognizer);
                this.AddGestureRecognizer (pinchGestureRecognizer);
                //this.AddGestureRecognizer (panGestureRecognizer);
                this.AddGestureRecognizer (swipeRightGestureRecognizer);
                this.AddGestureRecognizer (swipeLeftGestureRecognizer);
                this.AddGestureRecognizer (rotationGestureRecognizer);
            }
        }

        private void UpdateLeft(){
            MessagingCenter.Send ("Swiped to the left", "LeftSwipe");

        }
        private void UpdateRight(){
            // same as for left, but flipped
            MessagingCenter.Send ("Swiped to the Right", "RightSwipe");

        }
    }
}

关于c# - Xamarin UISwipeGestureRecognizer 渲染器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29972220/

10-12 01:18