2016-09-01 9 views
1

私はLabelコントロールにジェスチャー機能を適用しています。私はこのリンクを使用しました - http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/Xamarin.FormsのLongPressGestureRecognizer

ラベルコントロールを長押ししたときにLongPressGestureRecognizerイベントを取得できました。このイベントはレンダラーファイルで呼び出されています。

LongPressGestureRecognizerイベントの共有コードで何らかの操作を実行したいとします。どうすれば共有コードでこのイベントを検出できますか?私の共有コードでこのlongpressイベントを取得するためのイベントハンドラを処理するには?カスタムコントロールの宣言コマンドで

答えて

0

:その後、

public class TappedGrid : Grid 
{ 
    public static readonly BindableProperty TappedCommandProperty = 
     BindableProperty.Create(nameof(TappedCommand), 
         typeof(ICommand), 
         typeof(TappedGrid), 
         default(ICommand)); 

    public ICommand TappedCommand 
    { 
     get { return (ICommand)GetValue(TappedCommandProperty); } 
     set { SetValue(TappedCommandProperty, value); } 
    } 

    public static readonly BindableProperty LongPressCommandProperty = 
     BindableProperty.Create(nameof(LongPressCommand), 
           typeof(ICommand), 
           typeof(TappedGrid), 
           default(ICommand)); 

    public ICommand LongPressCommand 
    { 
     get { return (ICommand)GetValue(LongPressCommandProperty); } 
     set { SetValue(LongPressCommandProperty, value); } 
    } 
} 

レンダラからこのコマンドを上げる:

public class TappedGridRenderer : ViewRenderer 
{ 
    UITapGestureRecognizer tapGesturesRecognizer; 
    UILongPressGestureRecognizer longPressGesturesRecognizer; 

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

     tapGesturesRecognizer = new UITapGestureRecognizer(() => 
     { 
      var grid = (TappedGrid)Element; 
      if (grid.TappedCommand.CanExecute(Element.BindingContext)) 
      { 
       grid.TappedCommand.Execute(Element.BindingContext); 
      } 
     }); 

     longPressGesturesRecognizer = new UILongPressGestureRecognizer(() => 
     { 
      var grid = (TappedGrid)Element; 
      if (longPressGesturesRecognizer.State == UIGestureRecognizerState.Ended && 
        grid.LongPressCommand.CanExecute(Element.BindingContext)) 
      { 
       grid.LongPressCommand.Execute(Element.BindingContext); 
      } 
     }); 

     this.RemoveGestureRecognizer(tapGesturesRecognizer); 
     this.RemoveGestureRecognizer(longPressGesturesRecognizer); 

     this.AddGestureRecognizer(tapGesturesRecognizer); 
     this.AddGestureRecognizer(longPressGesturesRecognizer); 
    } 
} 
+0

パーフェクトバディ。ありがとう:)本当に感謝 –