【问题标题】:How to bind a Command to the ItemClick event in MvxListView?如何将命令绑定到 MvxListView 中的 ItemClick 事件?
【发布时间】:2016-12-22 18:34:42
【问题描述】:

我有以下 ViewModel:

public ObservableCollection<Channel> Series { get; private set; }

private MvxCommand<Channel> enableClicked;
public ICommand EnableClicked
{
    get
    {
        return enableClicked = enableClicked ??
            new MvxCommand<Channel>(ch => {
                ch.Enabled = !ch.Enabled;
            });
    }
}

我的 axml 看起来像:

<Mvx.MvxListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:MvxItemTemplate="@layout/serieenable"
    app:MvxBind="ItemsSource Series; ItemClick EnableClicked" />

我在return enableClicked 行上放了一个断点,它被命中了。

如果我在ch.Enabled = !ch.Enabled; 行上设置断点,当我触摸 ListView 项目时,它永远不会被命中。

如何将命令绑定到MvxListView 中的ItemClick 事件?

【问题讨论】:

    标签: android mvvmcross


    【解决方案1】:

    我无法重现您的问题。刚刚尝试了一个带字符串的小样本:

    private MvxCommand<string> _clickCommand;
    public ICommand ClickCommand
        => _clickCommand = _clickCommand ?? new MvxCommand<string>(OnClick);
    
    private void OnClick(string item)
    {
        Mvx.TaggedTrace("MyVM", $"Item {item} clicked");
    }
    

    并查看:

    <MvxListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        app:MvxItemTemplate="@layout/my_listitem"
        app:MvxBind="ItemsSource Items; ItemClick ClickCommand" />
    

    触发和工作正常。

    【讨论】:

    • 你能分享一下my_listitem吗?
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 2010-10-30
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2016-10-18
    相关资源
    最近更新 更多