ListView SelectedItem Associated with Xamarin and ReactiveUI Formats

I have a basic list of elements displaying a name. I need to go to a new screen when I click on one of the list items. Super simple, right? I can not understand where this is happening.

The list displays its items correctly. After selecting an item in the form of a list, the corresponding item is selected, but nothing else happens. I have no mistakes. It looks like the view model and view are not connected properly or perhaps the subscription in the view model is not configured 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 using a derived list is correct. I expect that I will not give sufficient context for feedback on this.

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> 
+5
source share
1 answer

If you have not simplified your sample code, the problem should be there:

public ItemTileViewModel SelectedItemTile { get; set; }

This is a regular property that will never generate any event when it changes (for example, it is set by the view binding when selected).

You should write it like any RxUI property:

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

Link

+2
source

All Articles