Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView SelectedItem Binding with Xamarin Forms and ReactiveUI Issue

I have a basic list of items that display a name. I need to navigate to a new screen upon one of the list items being clicked. Super simple, right? I can't figure out where this is going wrong.

The list is displaying its items properly. Upon selecting an item the list view highlights the appropriate item, but nothing else occurs. I am getting no errors. It is as though the view and view model aren't hooked up properly or maybe the subscribe in the view model is not set up properly. I am very new to ReactiveUI.

Below are dummy examples of my code.

ItemListViewModel.cs

public IReactiveDerivedList<ItemTileViewModel> ItemTiles { get; protected set; }
public ItemTileViewModel SelectedItemTile { get; set; }

private void Initialize(){
     ItemTiles = LoadedItems.CreateDerivedCollection(item => new ItemTileViewModel(item));
     this.WhenAnyValue(x => x.SelectedItemTile)
            .Where(tile => tile != null)
            .Select(tile => tile.Model)
            .Subscribe(item =>
            {
                HostScreen.Router.Navigate.ExecuteAsync(new ItemViewModel(item));
                SelectedItemTile = null;
            });

     // We load the items
}

I'm not sure if it is appropriate to be using a derived list. I expect I'm not giving enough context for feedback on that.

ItemListView.xaml.cs - contructor

this.OneWayBind(ViewModel, vm => vm.ItemTiles, v => v.ItemTiles.ItemsSource);
this.Bind(ViewModel, vm => vm.SelectedItemTile, v => v.ItemTiles.SelectedItem);

ItemListView.xaml

<ListView x:Name="ItemTiles">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <views:ItemTileView ViewModel="{Binding}" />
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>
like image 533
Magnetic Llama Avatar asked Oct 31 '22 21:10

Magnetic Llama


1 Answers

Unless you simplified your sample code, issue should be there:

public ItemTileViewModel SelectedItemTile { get; set; }

This is a regular property which will never generate any event when modified (e.g. set by the view binding when selected).

You should write it like any RxUI property instead:

string selectedItemTitle;
public string SelectedItemTile {
    get { return selectedItemTitle; }
    set { this.RaiseAndSetIfChanged(ref selectedItemTitle, value); }
}

Reference

like image 110
Gluck Avatar answered Nov 17 '22 00:11

Gluck