Mvvmcross ListView SelectedItem setter not reached

I am creating an Android app via Xamarin using MvvmCross.

I have been struggling with this problem for some time and I have found a workaround, but I am pretty sure this is the wrong way to do it.

This refers to the selected item in the MvxListView:

<Mvx.MvxListView
  android:id="@+id/productSearchResultListView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  local:MvxBind="ItemsSource ProductSearchResultLst; SelectedItem SelectedProductSearchResult"
  local:MvxItemTemplate="@layout/productsearchresult_row" />
      

Run codeHide result


When an item in this list is selected, the user is taken to the page where this item is processed. Once this is done, the user will click "confirm" and return to this list. At this point, the selected item is highlighted (which I like), but it cannot be clicked again. That is, the item does not open if you click on it again. I understand why. No item change selected. However, I am unable to deselect the viewmodel of the selected SelectedProductSearchResult. "I put it to null on the OnRestart event coming from the view. However, the selected item's setter will not be reached when the item is reselected.

I've also played around with ClearChoices and SetItemChecked in the view directly, but that doesn't deselect my selected VM item and therefore doesn't help me.

Finally I realized that the selected item hadn't changed (same object) and therefore the setter was not reached. I have now included the following part of the OnRestart method in the viewmodel, which updates the list in the sense that all items are new objects:

var productLst = new List<WHM_PRODUCT> ();
foreach (var item in ProductSearchResultLst)
     productLst.Add (item.Clone());

ProductSearchResultLst = new ObservableCollection<WHM_PRODUCT> (productLst);

      

This works, but I think it is very silly because it would mean that I have to do it for each object separately.

There must be a better way to do this.

Any feedback is greatly appreciated!

Stefaan

+3


source to share


1 answer


You didn’t provide your view mode code, but I do believe that you are doing the navigation logic in your property setting SelectedProductSearchResult

. This is not a standard approach and you are better off using Command .

First, create a Command property in your view model:

public ICommand NavigateToDetailCommand
        {
            get
            {
                return new MvxCommand<WHM_PRODUCT>(item =>
                {
                    //Perform navigation logic here
                    ShowViewModel<DetailViewModel>(new { id = item.Id });
                });
            }
        }

      



Then bind it to ItemClick on your MvxListView:

local:MvxBind="ItemsSource ProductSearchResultLst; ItemClick NavigateToDetailCommand"

      

Now when the user requests a list item, your command is executed and the associated item is passed as a parameter.

+4


source







All Articles