[英]How to handle the Tap of a ViewCell defined in code in Xamarin.Forms?
I have a multiple ViewCell and ImageCell subclasses inside a TableView. 我在TableView中有多个ViewCell和ImageCell子类。
I would like to execute some code when the user taps the cell. 当用户点击单元格时,我想执行一些代码。 The whole cell highlights whenever it's touched, but I don't see any event handler to use here.
只要触摸它,整个单元格就会突出显示,但我看不到要使用的任何事件处理程序。
Isn't there a XAML Tapped equivalent for this but in code only? 是否没有XAML Tapped等效项,仅在代码中?
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"
}
}
}
};
}
I'm sure there must be some API that handles this. 我确定必须有一些API可以处理此问题。 Maybe I'm just not looking in the right place?
也许我只是找不到合适的位置?
Thank you! 谢谢!
This can be accomplished by adding a TapGestureRecognizer to the cell's view layout. 这可以通过在单元格的视图布局中添加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;
EDIT: 编辑:
Or, a better alternative, using the Tapped property of the ViewCell, so it doesn't break the "Tap Animation": 或者,使用ViewCell的Tapped属性更好的选择,这样就不会破坏“ Tap Animation”:
Tapped += new EventHandler((e, s) => {
if (command.CanExecute(null))
{
command.Execute(null);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.