繁体   English   中英

ViewCell中的Xamarin.Forms按钮。 如何处理这个事件?

[英]Xamarin.Forms button in ViewCell. How to handle the event?

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

这是我的自定义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如下所示:

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

那么,我如何抓住按钮按下?

请询问您是否需要澄清。


好的,所以我至少得到了这个:

这是我的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的ListSelected中,它是这样完成的:

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

我怎么能这样做?


得到它了! 这可能不是一个好的解决方案,但似乎有效:

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

我会接受唯一的答案,因为它把我推向了正确的方向。 (而且这可能是一种更好的方法)。

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

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;
}

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

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

这样,您可以从视图单元格中触发事件,并在其他位置处理它(视图模型)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM