ViewCell中的Xamarin.Forms按钮.如何处理事件? [英] Xamarin.Forms button in ViewCell. How to handle the event?

查看:85
本文介绍了ViewCell中的Xamarin.Forms按钮.如何处理事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的自定义ViewCell.当我单击此按钮时,我想在ContentPage中处理此单击,该ContentPage显示带有ViewCells的ListView.在iOS中,我将使用Cell的委托来执行此操作.我将如何在C#/Xamarin.Forms中做到这一点?

I have a custom ViewCell with a button. When I click this button I would like to handle this click in the ContentPage which displays the ListView with the ViewCells. In iOS, I would do this with a delegate from the Cell. How would I do this in C# / Xamarin.Forms?

这是我自定义的ViewCell:

This is my custom ViewCell:

    public CustomCell ()
    {
        button = new Button ();

        button.Text = "Add";
        button.VerticalOptions = LayoutOptions.Center;

        button.Clicked += [WHAT DO DO HERE???];

        var nameLabel = new Label ();
        nameLabel.SetBinding (Label.TextProperty, "name");
        nameLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
        nameLabel.VerticalOptions = LayoutOptions.Center;

        var viewLayout = new StackLayout () {
            Padding = new Thickness (10, 0, 10, 0),
            Orientation = StackOrientation.Horizontal,
            Children = { nameLabel, button }
        };

        View = viewLayout;
    }

我将其用作ViewCell的ContentPage看起来像这样:

And my ContentPage which is using this as a ViewCell looks like this:

ListView.ItemTemplate = new DataTemplate (typeof(CustomCell));

那么,如何捕捉按钮按下的声音?

So, how do I catch the button press?

请询问您是否需要澄清.

Please ask if you need clarification.

好,所以我至少要这样:

Ok, so I got this up atleast:

这是我的ContentPage:

This is my ContentPage:

    ...
    ListView.ItemTemplate = new DataTemplate (() => new CustomCell (CustomCellButtonClicked));
}

void CustomCellButtonClicked (CustomCell m, EventArgs e)
{
    //How do I get my associated object here?!

    System.Diagnostics.Debug.WriteLine ("Pressed cell " + m.ToString ());
}

这是我的手机:

    public EventArgs e = null;
    public event ButtonClickEvent ClickListener;
    public delegate void ButtonClickEvent (AddCardCell m, EventArgs e);

    public CustomCell (ButtonClickEvent Listener)
    {
        ClickListener += Listener;
        customButton = new Button ();

        customButton.Text = "Add";
        customButton.VerticalOptions = LayoutOptions.Center;

        customButton.Clicked += AddButtonClicked;

        ...
    }

    void AddButtonClicked (object sender, EventArgs e)
    {
        if (ClickListener != null) {
            ClickListener (this, e);
        }
    }

因此,现在剩下的就是获取与单元格关联的实际对象.在ListView的ItemSelected中,它是这样完成的:

So, all that is left now is to get the actual object associated with the cell. In ItemSelected for ListView, it is done like this:

ListView.ItemSelected += async (object sender, SelectedItemChangedEventArgs e) => {
            var obj = (HSObject)e.SelectedItem;
        };

我该怎么做?

知道了!这也许不是一个好的解决方案,但它似乎可以工作:

Got it! This is perhaps not a good solution, but it seems to work:

void CustomCellButtonClicked (CustomCell m, EventArgs e)
{
    var myObject = m.BindingContext;
}

由于它将我推向正确的方向,因此我将接受唯一的答案. (这可能是一种更好的方法).

I will accept the only answer due to the fact that it pushed me in the right direction. (And that it's probably a better way to do this).

推荐答案

如果使用的是视图模型或其他类,则可以将其传递到单元格中并从那里进行调用.当前,列表视图还使用列表视图中的ItemSelected事件覆盖单元格上的按钮.幸运的是,他们在数据模板上放了一个替代.

If you are using a view model or some other class, you can pass it into your cell and make your calls from there. The list view also currently overrides the buttons on your cell with the ItemSelected event from the list view. Luckily they put in an override on the data template.

private readonly ViewModel _model;
public CustomCell (ViewModel model)
{
    _model = model; <--- Your DI foo
    BindingContext = model; <---

    button = new Button ();

    button.Text = "Add";
    button.VerticalOptions = LayoutOptions.Center;

    button.Clicked += (sender, args) => _model.DoSomething(); <---- Your action

    var nameLabel = new Label ();
    nameLabel.SetBinding (Label.TextProperty, "name");
    nameLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
    nameLabel.VerticalOptions = LayoutOptions.Center;

    var viewLayout = new StackLayout () {
        Padding = new Thickness (10, 0, 10, 0),
        Orientation = StackOrientation.Horizontal,
        Children = { nameLabel, button }
    };

    View = viewLayout;
}

然后,您可以使用匿名函数将类/视图模型传递到单元格中.

Then you can pass your class/view model into your cell with an anonymous function.

ListView.ItemTemplate = new DataTemplate (() => new CustomCell(viewModel));

这样,您可以从视图单元触发事件,并在其他地方(视图模型)进行处理.

This way you can trigger your event from the view cell, and handle it somewhere else (view model).

这篇关于ViewCell中的Xamarin.Forms按钮.如何处理事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆