【问题标题】:MvvmCross Android MvxListView DrawableName not refreshingMvvmCross Android MvxListView DrawableName 不刷新
【发布时间】:2014-12-19 14:04:48
【问题描述】:

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

数据模型

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”和“off”都是 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 刷新的东西,但我不知道是什么。任何想法将不胜感激。

【问题讨论】:

    标签: android xamarin xamarin.android mvvmcross


    【解决方案1】:

    试试:

    • MvxNotifyPropertyChanged继承VehicleStatus
    • VehicleStatus 内使用RaisePropertyChanged() 调用通知更改,因为更改发生

    为了获得奖励,最好使用转换器将Selected 属性自动更改为DrawableName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多