隐藏

Xamarin.Forms - 在Listview单元格中绑定多个按钮单击?

发布:2021/10/16 22:57:14作者:管理员 来源:本站 浏览次数:1073

您只需创建一个自定义视图单元格:

public class MyCell : ViewCell
{
    public MyCell ()
    {
        var button1 = new Button {Text = "Button 1"};
        button1.Clicked += (sender1, e1) => {
           // Action for button 1
        };
        var button2 = new Button {Text = "Button 2"};
        button2.Clicked += (sender, e) => {
          // Action for Button 2
        };
        View = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            Children = {
                 button1,
                 button2,
            }
        };
        View.GestureRecognizers.Add(new TapGestureRecognizer());
    }
} 

最后一个TapGestureRecognizer是为了避免触摸的选择(和Android显示) .

并将其集成到ListView中

public DemoPage ()
{
    var listView = new ListView
    {
        ItemsSource = new[] {"cell 1", "cell 2", "cell 3", "cell 4"},
        ItemTemplate = new DataTemplate(typeof (MyCell))
    };
    Content = listView;
} 

您还可以将按钮单击绑定到命令