MvvmCross Android MvxListView DrawableName没有刷新

时间:2014-12-19 14:04:48

标签: android xamarin xamarin.android mvvmcross

我正在使用MvvmCross 3.2.2作为Android / iOS应用程序,并且遇到了一个没有刷新的MvxListView和使用DrawableName绑定的ImageView,除非相关项目在屏幕上滚动并返回。

的DataModel

public class VehicleStatus
{
    public string VehicleRegistration { get; set; }
    public bool Selected { get; set; }
    public string DrawableName
    {
        get
        {
            string res = (Selected) ? "on" : "off";
            return  res;
        }
    }
}

两者" on"和"关"是Drawable文件夹中的png资源。

MvxListView布局摘录

       <Mvx.MvxListView
            android:id="@+id/listItems"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:dividerHeight="2dp"
            android:divider="@drawable/list_divider"
            local:MvxBind="ItemsSource Units; ItemClick VehicleSelectedCommand"
            local:MvxItemTemplate="@layout/list_unit" />

MvxItemTemplate

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:local="http://schemas.android.com/apk/res/xxxxxxx.Droid"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:background="@color/white"
   android:layout_height="62dp">
   <TextView
       android:id="@+id/unitsName"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:textSize="18dp"
       android:gravity="center"
       android:layout_margin="7dp"
       android:textColor="@color/verydarknavy"
       local:MvxBind="Text VehicleRegistration" />
    <ImageView
       android:id="@+id/notificationUnitImage"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginRight="21dp"
       android:layout_centerVertical="true"
       android:layout_alignParentRight="true"
       local:MvxBind="DrawableName DrawableName "
       android:tint="@color/verydarknavy" />
</RelativeLayout>

视图模型

    private ObservableCollection<VehicleStatus> _units;
    public ObservableCollection<VehicleStatus> Units
    {
        get { return _units; }
        set { _units = value;
            RaisePropertyChanged (() => Units); }
    }

和命令

    public void VehicleSelected (VehicleStatus item)
    {
        if (item != null)
        {
            foreach (var unit in _units)
            {
                if (unit.Id == item.Id)
                {
                    unit.Selected = !unit.Selected;
                    break;
                }
            }
            RaisePropertyChanged (() => Units);
        }
    }

    private MvxCommand<VehicleStatus> _vehicleSelectedCommand;
    public System.Windows.Input.ICommand VehicleSelectedCommand 
    {
        get
        {
            _vehicleSelectedCommand = _vehicleSelectedCommand ?? new MvxCommand<VehicleStatus>(VehicleSelected);
            return _vehicleSelectedCommand;
        }
    }

我认为这是所有相关的来源。我假设我错过了让ImageView刷新的东西,但我不知道是什么。任何想法都将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试:

  • VehicleStatus
  • 继承MvxNotifyPropertyChanged
  • VehicleStatus内的
  • 在发生更改时使用RaisePropertyChanged()调用发出更改

对于奖励积分,使用转换器自动将Selected属性更改为DrawableName属性也可能不错。