2017-04-23 75 views
1

您能否让我知道如何识别Xamarin Forms应用程序中的长按手势?如何在Xamarin Forms中长按手势?

几天我用TapGestureRecognizer

TapGestureRecognizer imageTap = new TapGestureRecognizer(); 
imageTap.Tapped += (sender, args) => this.OnClickImage; 
image.GestureRecognizers.Add(imageTap); 

但我不知道如何根据本thread from xamarin forum

它应该看起来像这样使长按压姿势,但它不工作之前。

var dumpParam = new RelayGesture((g, x) => DisplayAlert("Title", "Hello message", "Cancel")); 

        book.Cover.SetValue(Gestures.InterestsProperty, new GestureCollection() { 
         new GestureInterest 
         { 
          GestureType = GestureType.LongPress 
          GestureCommand = // what should I set? 
          GestureParameter = dumpParam 
         } 
        }); 

如何设置我的自定义处理程序方法?

+0

你可以看看这个https://forums.xamarin.com/discussion/27323/how-can-i-recognize-long-press-gesture-in- xamarin-forms –

+0

@NoorAShuvo是的。但我不知道在我的情况下实施它。我不使用'XAML'。在我用'Xamarin.Froms.Image'使用'TapGestureRecognizer'和'Tapped'事件前几天,我将它添加到'GestureRecognizers'图像,但我不知道如何设置我的句柄方法到'GestureInterest',它描述为上面的链接。你能提供一个例子吗? –

回答

2

上网冲浪我找到了解决方案。有几个步骤,你应该重现。

1)继承你需要手势控制(即,如果你想的姿态加入到Xamarin.Forms.Image,创建自己的ImageWithLongPressGesture类)。

public class ImageWithLongPressGesture : Xamarin.Forms.Image 
    { 
     public EventHandler LongPressActivated; 

     public void HandleLongPress(object sender, EventArgs e) 
     { 
      //Handle LongPressActivated Event 
     } 
    } 

2)公开所需手势的公共事件。

3)为每个平台创建一个渲染器。

4)在渲染器中,处理手势并将其展开到您的控件。

[assembly: ExportRenderer(typeof(ImageWithLongPressGesture), typeof(LongPressGestureRecognizerImageRenderer))] 
namespace App1.Droid.DroidRenderers 
{ 
    public class LongPressGestureRecognizerImageRenderer : ImageRenderer 
    { 
     ImageWithLongPressGesture view; 

     public LongPressGestureRecognizerImageRenderer() 
     { 
      this.LongClick += (sender, args) => { 
       Toast.MakeText(this.Context, "Long press is activated.", ToastLength.Short).Show(); 
      }; 
     } 

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

      if(e.NewElement != null) 
      { 
       view = e.NewElement as ImageWithLongPressGesture; 
      } 
     } 
    } 
} 

该解决方案是通过Telerik的的answer on xamarin forms forumTouch and Gestures presentation的混合体。

+0

此解决方案不适用于Android。它适用于iOS,并且我在我的项目中使用它,但是对于Android来说什么也没有发生,手势永远不会被处理。渲染器百分之百吸引了我的注意力,因为我已经能够获得替代实现来工作(但是他们不让我发送回调到框架,所以实质上是无用的)。 关于如何使这项工作的任何想法?我使用的不是图像,但我相信这并不重要。 只是要清楚这一点。LongClick'被添加到每个框架,只是从来没有被开除... – Axemasta

2

利用XLabs.Forms nuget包,它只在PCL代码中进行长按和其他手势。 XLabs.Forms包的使用将减少个别平台中自定义渲染的需要... 在.xaml文件中添加XAML代码并在.xaml.cs文件中添加附加事件处理程序.. 它在Android中正常工作..

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="MultiImage.Page1"    
     xmlns:lc="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms" 
     xmlns:lb="clr-namespace:XLabs.Forms.Behaviors;assembly=XLabs.Forms"> 
<ContentPage.Content> 
    <lc:GesturesContentView ExcludeChildren="False" GestureRecognized="GesturesContentView_GestureRecognized"> 
     <lb:Gestures.Interests> 
       <lb:GestureCollection> 
        <lb:GestureInterest GestureType="SingleTap"/> 
        <lb:GestureInterest GestureType="LongPress"/> 
        <lb:GestureInterest GestureType="DoubleTap"/> 
       </lb:GestureCollection> 
      </lb:Gestures.Interests> 
      <Image Source="Myimage.png" Aspect="AspectFit" HeightRequest="100"/> 
     </lc:GesturesContentView> 
</ContentPage.Content> 

C#后端代码:

private void GesturesContentView_GestureRecognized(object sender, GestureResult e) 
    {   
     switch (e.GestureType) 
     { 
      case GestureType.LongPress: 
       //Add code here 
       break; 

      case GestureType.SingleTap: 
       // Add code here      
       break; 
      case GestureType.DoubleTap: 
       // Add code here 
       break; 
      default: 
       break; 
     } 
+0

当我尝试使用这个例子,标签带下划线红色,我得到的错误“无效类型:预期类型是'BindableProperty',实际类型是'GestureCollection'。任何想法可能是什么问题? – lepton

+0

请确保您拥有最新版本的Xlabs.forms,并且已经定义了Xlabs.forms.controls和Xlabs.forms.behaviour的命名空间 –