I'm creating a Xamarin.Forms application with a list. The itemSource is a reactiveList. Adding new items to the list however does not update the UI. What is the correct way of doing this?
List Definition
_listView = new ListView();
var cell = new DataTemplate(typeof(TextCell));
cell.SetBinding(TextCell.TextProperty, "name");
cell.SetBinding(TextCell.DetailProperty, "location");
_listView.ItemTemplate = cell;
Binding
this.OneWayBind(ViewModel, x => x.monkeys, x => x._listView.ItemsSource);
this.OneWayBind(ViewModel, x => x.save, x => x._button.Command); //save adds new items
View Model
public class MyPageModel : ReactiveObject
{
public MyPageModel()
{
var canSave = this.WhenAny(x => x.username, x => !String.IsNullOrWhiteSpace(x.Value) && x.Value.Length>5);
save = ReactiveCommand.CreateAsyncTask(canSave, async _ =>
{
var monkey = new Monkey { name = username, location = "@ " + DateTime.Now.Ticks.ToString("X"), details = "More here" };
monkeys.Add(monkey);
username = "";
});
monkeys = new ReactiveList<Monkey>{
new Monkey { name="Baboon", location="Africa & Asia", details = "Baboons are Africian and Arabian Old World..." }
};
_monkeys.ChangeTrackingEnabled = true;
}
private string _username = "";
public string username
{
get { return _username; }
set { this.RaiseAndSetIfChanged(ref _username, value); }
}
private double _value = 0;
public double value
{
get { return _value; }
set { this.RaiseAndSetIfChanged(ref _value, value); }
}
public ReactiveCommand<Unit> save { get; set; }
public ReactiveList<Monkey> _monkeys;
public ReactiveList<Monkey> monkeys
{
get { return _monkeys; }
set { this.RaiseAndSetIfChanged(ref _monkeys, value); }
}
}
public class Monkey
{
public string name { get; set; }
public string location { get; set; }
public string details { get; set; }
}
Tried it with the ReactiveList property being a normal auto-property as well as the one in the code above where I used the RaiseAndSetIfChanged method.