2016-12-05 32 views
0

我有一個TableView內的多個ViewCell和ImageCell子類。如何處理Xamarin.Forms代碼中定義的ViewCell的Tap?

我想在用戶點擊單元格時執行一些代碼。整個單元格在觸摸時突出顯示,但我沒有看到在此使用任何事件處理程序。

是不是有一個XAML Tapped等價物,但僅在代碼中?

enter image description here

private void SetTableView() 
    { 
     Content = new TableView 
     { 
      HasUnevenRows = true, 
      Intent = TableIntent.Menu, 
      Root = new TableRoot() 
      { 
       new TableSection() 
       { 
        new ProfileCell(ImageSource.FromFile("profile_placeholder.png"), "Casa de Férias") 
       }, 
       new TableSection() 
       { 
        new InfoCell() 
        { 
         Type = InfoCellTypes.Pending, 
         Text = "Avaliação do Imóvel", 
         Detail = "Estado do Processo" 
        } 
       } 
      } 

     }; 

    } 

我敢肯定,必須有一些API來處理這一點。也許我只是沒有找到正確的地方?

謝謝!

回答

2

這可以通過將TapGestureRecognizer添加到單元格的視圖佈局來完成。

var absoluteLayout = new AbsoluteLayout 
{ 
    Children = 
    { 
     photo, 
     editImage, 
     label 
    } 
}; 

var tapGestureRecognizer = new TapGestureRecognizer(); 
tapGestureRecognizer.NumberOfTapsRequired = 1; 
tapGestureRecognizer.Tapped += (s, e) => { 
    // handle the tap 
}; 

absoluteLayout.GestureRecognizers.Add(tapGestureRecognizer); 

View = absoluteLayout; 

編輯:

或者,一個更好的選擇,使用ViewCell的螺紋屬性,所以它不會打破 「點擊動畫」:

Tapped += new EventHandler((e, s) => { 

    if (command.CanExecute(null)) 
    { 
     command.Execute(null); 
    } 

}); 
相關問題