添加项目到ObservableCollection时单击时不执行ICommand

时间:2014-06-05 20:11:47

标签: android mvvmcross

下面是.axml,用于显示绑定到名为EventDetailList的对象列表的MvxListView,每个ItemClick绑定到DoEventDetailCommand

EventDetailView.axml

<?xml version="1.0" encoding="utf-8"?>
<Mvx.MvxListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    local:MvxBind="ItemsSource EventDetailList; ItemClick DoEventDetailCommand"
    local:MvxItemTemplate="@layout/event_detailitem" />

单击此列表中的一个项目后,将在ViewModel中调用一个操作,该操作会将另一个EventDetailItem添加到ObservableCollection EventDetailList<EventDetailItem>。添加此新项目后,它会立即显示在列表中。但是,单击此新添加的项不会执行ObservableCollection EventDetailList<EventDetailItem>。我必须离开屏幕并返回,以便新添加的项目在单击时执行ICommand DoEventDetailCommand

EventDetailViewModel.cs DoEventDetailCommand

    private ObservableCollection<EventDetailItem> _eventDetailList;
    public ObservableCollection<EventDetailItem> EventDetailList 
    {
        get
        {
            return _eventDetailList;
        }
        set
        {
            _eventDetailList = value;
            if(_eventDetailList == null ||
                _eventDetailList.Count == 0)
            {
               Close(this);
            }
            RaisePropertyChanged(() => EventDetailList);
        }
    } 

...

public ICommand DoEventDetailCommand
{
  get
  {
    return new MvxCommand<EventDetailItem>(item =>
    {
      switch(item.ItemAction)
      {
        case EventDetailItem.Action.Information:
          break;
        case EventDetailItem.Action.DriverSig:
          EventDetailList.Add(new EventDetailItem() { 
            ItemAction = EventDetailItem.Action.Cancel, 
            IconImageType = EventDetailItem.IconType.Cancel, 
            Label = "Cancel" });
          break;
        case EventDetailItem.Action.Cancel:
          ShowViewModel<CancelTripViewModel>(new CancelTripViewModel.Nav() {Id=_curId});
          break;
        ....

0 个答案:

没有答案