简体   繁体   English

如何处理Xamarin.Forms中的代码中定义的ViewCell的Tap?

[英]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.

相关问题 ViewCell中的Xamarin.Forms按钮。 如何处理这个事件? - Xamarin.Forms button in ViewCell. How to handle the event? 如何处理Xamarin.Forms中的iOS上的附件按钮点击? - How to handle accessory button tap on iOS in Xamarin.Forms? 如何在Xamarin.Forms中强制将ViewCell的图像调整为特定大小? - How to force an image of a ViewCell to be resized to a certain size in Xamarin.Forms? 如何在 Xamarin.Forms 中处理 WCF Duplex? - How to handle WCF Duplex in Xamarin.Forms? 如何在 xamarin.forms 中的 viewcell 中设置按钮以从内容页面 viewmodel 调用命令? - How do I set a button in viewcell in xamarin.forms to call a command from the content page viewmodel? Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms 如何在自定义 Xamarin.Forms ViewCell 上的行之间添加分隔符空间? - How to add a separator space between rows on custom Xamarin.Forms ViewCell? 将ViewCell Disclosure Indicator附件添加到Xamarin.Forms Android和Iphone - Adding ViewCell Disclosure Indicator Accessory to Xamarin.Forms Android and Iphone Xamarin.Forms - 更新ItemSource时,ViewCell内的图像闪烁 - Xamarin.Forms - Image inside ViewCell flashes when updating the ItemSource Xamarin.Forms MVVM TapGestureRecognizer 到 ListView 的 ViewCell 中的标签(在部分文件中) - Xamarin.Forms MVVM TapGestureRecognizer to a Label in a ViewCell of ListView (in partial files)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM