简体   繁体   English

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

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

I have a custom ViewCell with a button. 我有一个带按钮的自定义ViewCell。 When I click this button I would like to handle this click in the ContentPage which displays the ListView with the ViewCells. 当我单击此按钮时,我想在ContentPage中处理此单击,该页面显示带有ViewCells的ListView。 In iOS, I would do this with a delegate from the Cell. 在iOS中,我会使用Cell的委托来完成此操作。 How would I do this in C# / Xamarin.Forms? 我将如何在C#/ Xamarin.Forms中执行此操作?

This is my custom ViewCell: 这是我的自定义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;
    }

And my ContentPage which is using this as a ViewCell looks like this: 我将其用作ViewCell的ContentPage如下所示:

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: 好的,所以我至少得到了这个:

This is my ContentPage: 这是我的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 ());
}

And this is my cell: 这是我的手机:

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

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

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

How can I do something like this? 我怎么能这样做?


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). (而且这可能是一种更好的方法)。

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. 列表视图当前还使用列表视图中的ItemSelected事件覆盖单元格上的按钮。 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). 这样,您可以从视图单元格中触发事件,并在其他位置处理它(视图模型)。

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

相关问题 如何处理Xamarin.Forms中的代码中定义的ViewCell的Tap? - How to handle the Tap of a ViewCell defined in code 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中强制将ViewCell的图像调整为特定大小? - How to force an image of a ViewCell to be resized to a certain size in Xamarin.Forms? 如何处理Xamarin.Forms中的iOS上的附件按钮点击? - How to handle accessory button tap on iOS in Xamarin.Forms? 如何在Xamarin.Forms中添加/生成按钮单击事件 - How to Add/Generate a button click event in Xamarin.Forms Xamarin.Forms - 按钮按下和释放事件 - Xamarin.Forms - Button Pressed & Released Event 如何在 Xamarin.Forms 中处理 WCF Duplex? - How to handle WCF Duplex in Xamarin.Forms? 如何在自定义 Xamarin.Forms ViewCell 上的行之间添加分隔符空间? - How to add a separator space between rows on custom Xamarin.Forms ViewCell? Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms Xamarin.Forms - 更新ItemSource时,ViewCell内的图像闪烁 - Xamarin.Forms - Image inside ViewCell flashes when updating the ItemSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM