图像 - 长按

时间:2017-03-26 18:33:36

标签: xamarin xamarin.android xamarin.forms

有没有办法检测Xamarin Forms中图像控件的长按?

我使用轮播视图来显示图像,并希望通过长按选择来删除它们。

1 个答案:

答案 0 :(得分:1)

根据您在评论中的建议,这就是我所做的: (控件的目的是能够选择带有LongTap的图像)

我在PCL中定义了自己的Image控件:

  • IsSelected BindableProperty。
  • LongTap事件。

     public class MyImage:Image
    {
            private BindableProperty IsSelectedProperty = BindableProperty.Create("IsSelected", typeof(bool), typeof(MyImage), false);
    
        public bool IsSelected {
            get {
                return (bool)GetValue(IsSelectedProperty);
            }
            set {
                SetValue(IsSelectedProperty, value);
            }
        }
    
    
        public event EventHandler LongClick;
    
        public void OnLongClick()
        {
            IsSelected = !IsSelected;
    
            if(IsSelected)
            {
                Opacity = 0.5;
            }
            else
            {
                Opacity = 1;
            }
    
            if (LongClick != null)
            {
                LongClick(this, EventArgs.Empty);
            }
        }
    }
    

这是我的自定义渲染器:(在Android项目中定义)

[assembly: ExportRenderer(typeof(MyImage), typeof(MyImageRenderer))]
namespace PRISMCarouselView.Droid.Renderes
{
    public class MyImageRenderer : ImageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                ImageView androidSource = Control as ImageView;
                MyImage myImage = e.NewElement as MyImage;

                androidSource.LongClick += (object sender, LongClickEventArgs ee) =>
                {
                    myImage.OnLongClick();
                };
            }
        }
    }
}

修改1:

这是一个稍微更新的版本,我使用BindingPropertyChangedDelegate来更改图像的不透明度:

 public class SelectableImage : Image
    {
        public SelectableImage()
        {

        }

        private static BindableProperty IsSelectedProperty = BindableProperty.Create("IsSelected",
        typeof(bool),
        typeof(SelectableImage),
        false, BindingMode.Default, null, (sender, o1, o2) => {

            SelectableImage imageControl = sender as SelectableImage;
            if(imageControl != null)
            {
                if(imageControl.IsSelected)
                {
                    imageControl.Opacity = 0.5;
                }else
                {
                    imageControl.Opacity = 1;
                }
            }
        });

        public bool IsSelected {
            get {
                return (bool)GetValue(IsSelectedProperty);
            }
            set {
                SetValue(IsSelectedProperty, value);
            }
        }


    }

渲染器:

[assembly: ExportRenderer(typeof(SelectableImage), typeof(SelectableImageRenderer))]
namespace Muserma.Apps.Droid.Renderer
{
    public class SelectableImageRenderer : ImageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                ImageView androidSource = Control as ImageView;
                SelectableImage selectableImage = e.NewElement as SelectableImage;

                androidSource.LongClick += (object sender, LongClickEventArgs ee) =>
                {
                    selectableImage.IsSelected = !selectableImage.IsSelected;
                };
            }
        }

    }
}