【问题标题】:MvvmCross MvxListView ItemClick Enabled Based on propertyMvvmCross MvxListView ItemClick 启用基于属性
【发布时间】:2017-02-23 04:14:20
【问题描述】:

所以我有一个 MvxListView

<Mvx.MvxListView
    android:id="@+id/receptionsListView"
    android:divider="@drawable/divider"
    android:scrollbars="vertical"
    android:choiceMode="singleChoice"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    local:MvxItemTemplate="@layout/item_supplier"
    local:MvxBind="ItemsSource ReceptionSuppliersList;  ItemClick SelectReceptionCommand;" />

我想根据列表中模型的值启用/禁用一些项目。像

  local:MvxBind="ItemsSource ReceptionSuppliersList; ItemClick SelectReceptionCommand; Enabled ReceptionSuppliersList.IsValid" />

根据我的测试,这只是禁用了我的所有列表项,因为没有这样的属性 ReceptionSuppliersList.IsValid (它是 ReceptionSuppliersList[i].IsValid )。我怎样才能做到这一点。

我也尝试像这样在 item_supplier 上添加 Enabled 属性

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="0dp"
    style="@style/ExtendProTheme.ReceptionItem"
    local:MvxBind="Enabled IsValid" >

但还是不行。

任何想法如何根据属性禁用列表中的某些项目?

PS: 我的物品看起来像这样

public string Username { get; set; }

public string DeviceId { get; set; }

public bool IsValid { get; set; }

【问题讨论】:

  • 你的输出中有什么东西吗?可能有些绑定错误?
  • 我的绑定没有看到任何错误。
  • 您是否尝试过在项目模板的根目录上绑定Enabled 而不是MvxListView

标签: xamarin mvvmcross mvxlistview


【解决方案1】:

您可以创建一个自定义适配器来处理列表项的启用/禁用。您只需要将 Adapters ItemsSource 转换为您的 ViewModels IsEnabled 覆盖中的 ReceptionSuppliersList 类型。

public class CustomAdapter : MvxAdapter
{
    public CustomAdapter(Context context) 
        : base(context)
    {
    }

    public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) 
        : base(context, bindingContext)
    {
    }

    protected CustomAdapter(IntPtr javaReference, JniHandleOwnership transfer) :
        base(javaReference, transfer)
    {
    }

    public override bool IsEnabled(int position)
    {
        var items = ItemsSource as List<TestModel>;
        return items[position].IsValid;
    }
}

将适配器分配给您的 MvxListView:

var receptionsListView = FindViewById<MvxListView>(Resource.Id.receptionsListView);
receptionsListView.Adapter = new CustomAdapter(this, BindingContext as IMvxAndroidBindingContext);

【讨论】:

  • @CiucaS,这是否给了你想要的东西?
  • 请求已更改,应该启用并在单击时显示错误消息,因此我还没有实现它。无论如何,我想这样做以防我将来需要它来测试它,但我一直很忙。一旦我找到一些空闲时间,我会尽力去做。我没有忘记。
【解决方案2】:

这不是真正的问题解决方案,但它应该适用于您的情况:

  1. 在项目上添加灰色叠加层,使其看起来像未启用。
  2. 检查是否在 SelectReceptionCommand 的方法处理程序上启用了项目。
    
    private void OnSelectReceptionCommandExecuted(Reception item)
            {
                if (!reception.IsEnabled)
                {
                    return;
                }
                // your code here
            }

【讨论】:

  • 我认为这是一种解决方案,但我真的想要一个更优雅的解决方案。这只是一个令人讨厌的解决方法,不应该是这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多