如何使用ReactiveList,以便在添加新项目时更新UI

时间:2014-12-22 01:17:48

标签: c# xamarin.forms reactiveui

我正在创建一个带有列表的Xamarin.Forms应用程序。 itemSource是一个reactiveList。但是,向列表中添加新项目不会更新UI。这样做的正确方法是什么?

列表定义

_listView = new ListView();
var cell = new DataTemplate(typeof(TextCell));
cell.SetBinding(TextCell.TextProperty, "name");
cell.SetBinding(TextCell.DetailProperty, "location");
_listView.ItemTemplate = cell;

结合

this.OneWayBind(ViewModel, x => x.monkeys, x => x._listView.ItemsSource);
this.OneWayBind(ViewModel, x => x.save, x => x._button.Command); //save adds new items

查看模型

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; }
}

尝试将ReactiveList属性作为普通的自动属性以及上面代码中使用RaiseAndSetIfChanged方法的属性。

2 个答案:

答案 0 :(得分:2)

您的问题是您正在更改非UI线程上的monkeys。在其他框架中,这将抛出异常,但在AppKit / UIKit中这只是奇怪的东西(通常没什么)。

    save = ReactiveCommand.Create(canSave);
    save.Subscribe(_ =>
    {
        // Do the part that modifies UI elements (or things bound to them)
        // in the RxCmd's Subscribe. This is guaranteed to run on the UI thread
        var monkey = new Monkey { name = username, location = "@ " + DateTime.Now.Ticks.ToString("X"), details = "More here" };
        monkeys.Add(monkey);
        username = "";
    });

答案 1 :(得分:1)

这种技术看起来非常复杂。这让我觉得你想做一些我认为更复杂的事情。

这是一个干净的解决方案,可以通过以纯MVVM方式绑定到ReactiveList<T>来将对象添加到列表框中。

首先,xaml

<Window x:Class="ReactiveUIListBox.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:ReactiveUIListBox.ViewModel"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <viewModel:MainWindowViewModel x:Key="MainWindowViewModel"/>    
    </Window.Resources>

    <Window.DataContext>
        <StaticResource ResourceKey="MainWindowViewModel"/>
    </Window.DataContext>

    <Grid DataContext="{StaticResource MainWindowViewModel}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Background="Azure">
            <ListBox ItemsSource="{Binding Model.TestReactiveList}"></ListBox>
        </Grid>

        <Grid Grid.Column="1">
            <Button x:Name="button" Command="{Binding TestCommand}" Content="Button" HorizontalAlignment="Left" Margin="78,89,0,0" VerticalAlignment="Top" Width="75"/>
        </Grid>

    </Grid>
</Window>

这是ViewModel

using System.Windows.Input;
using ReactiveUIListBox.Model;
using SecretSauce.Mvvm.ViewModelBase;

namespace ReactiveUIListBox.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            Model = new ReactiveModel<string>();
        }
        public ReactiveModel<string> Model
        {
            get;
            set;
        }

        public ICommand TestCommand
        {
            get { return new RelayCommand(ExecuteTestCommand); }
        }

        private void ExecuteTestCommand(object obj)
        {
            Model.TestReactiveList.Add("test string");
        }
    }
}

最后,这是模特。

using ReactiveUI;

namespace ReactiveUIListBox.Model
{
    public class ReactiveModel<T> : ReactiveObject
    {
        public ReactiveModel()
        {
            TestReactiveList= new ReactiveList<T>();
        }

        public ReactiveList<T> TestReactiveList
        {
            get;
            set;
        }
    }
}

按下按钮将填充ListBox。希望我还没有完全简化你想要做的事情,但你的代码似乎确实存在。

我无法区分适合View,Model或ViewModel的代码。

干杯。

相关问题