[英]How to handle the Tap of a ViewCell defined in code in Xamarin.Forms?
我在TableView中有多个ViewCell和ImageCell子类。
当用户点击单元格时,我想执行一些代码。 只要触摸它,整个单元格就会突出显示,但我看不到要使用的任何事件处理程序。
是否没有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"
}
}
}
};
}
我确定必须有一些API可以处理此问题。 也许我只是找不到合适的位置?
谢谢!
这可以通过在单元格的视图布局中添加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属性更好的选择,这样就不会破坏“ Tap Animation”:
Tapped += new EventHandler((e, s) => {
if (command.CanExecute(null))
{
command.Execute(null);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.