public MainWindowViewModel() { this.SimpleAddCommand = new ReactiveCommand(); this.SimpleAddCommand.Subscribe(_ => { this.source.OnNext(random.Next()); }); this.SimpleResetCommand = new ReactiveCommand(); // IObservaboe<T>からコレクションへ変換。オプションとしてコレクションをリセットするIO<Unit>を渡せる this.SimpleCollection = this.source.ToReadOnlyReactiveCollection(this.SimpleResetCommand.ToUnit()); this.CollectionChangedCollection = this.collectionChangedSource.ToReadOnlyReactiveCollection(); this.CollectionChangedAddCommand = new ReactiveCommand(); this.CollectionChangedAddCommand.Subscribe(_ => { this.collectionChangedSource.OnNext( CollectionChanged <int> .Add(0, random.Next())); }); this.CollectionChangedClearCommand = new ReactiveCommand(); this.CollectionChangedClearCommand.Subscribe(_ => { this.collectionChangedSource.OnNext( CollectionChanged <int> .Reset); }); this.CollectionChangedRemoveCommand = new ReactiveCommand(); this.CollectionChangedRemoveCommand.Subscribe(_ => { this.collectionChangedSource.OnNext( CollectionChanged <int> .Remove(0)); }); // ObservableCollection<T>からReadOnlyObservableCollection<U>への変換 this.ObservableCollectionToReadOnlyReactiveCollection = this.sourceCollection .ToReadOnlyReactiveCollection(x => x + " value."); this.SourceCollectionAddCommand = new ReactiveCommand(); this.SourceCollectionAddCommand.Subscribe(_ => { this.sourceCollection.Add(random.Next().ToString()); }); this.SourceCollectionRemoveCommand = new ReactiveCommand(); this.SourceCollectionRemoveCommand.Subscribe(_ => { this.sourceCollection.RemoveAt(this.sourceCollection.Count - 1); }); this.SourceCollectionResetCommand = new ReactiveCommand(); this.SourceCollectionResetCommand.Subscribe(_ => { this.sourceCollection.Clear(); }); }
public HidariueService() { this.AddCommand = new ReactiveCommand <Hidariue>(); this.ResetCommand = new ReactiveCommand(); //this.AddCommand = new ReactiveCommand<Hidariue>(); this.DeleteCommand = new ReactiveCommand <Hidariue>(); this.Hidariues = Observable.Merge( this.AddCommand .Select(h => CollectionChanged <Hidariue> .Add(0, h)), this.ResetCommand .Select(_ => CollectionChanged <Hidariue> .Reset), //this.UpdateCommand // .Select(h => CollectionChanged<Hidariue>.Replace(Hidariues.IndexOf(h), h)), this.DeleteCommand .Select(h => CollectionChanged <Hidariue> .Remove(Hidariues.IndexOf(h), h)) ).ToReadOnlyReactiveCollection <Hidariue>(); }
public void CollectionChangedTest() { var s = new Subject <CollectionChanged <int> >(); var target = s.ToReadOnlyReactiveCollection(); target.Count.Is(0); s.OnNext(CollectionChanged <int> .Add(0, 10)); s.OnNext(CollectionChanged <int> .Add(1, 2)); target.Is(10, 2); s.OnNext(CollectionChanged <int> .Remove(0)); target.Is(2); s.OnNext(CollectionChanged <int> .Add(1, 3)); target.Is(2, 3); s.OnNext(CollectionChanged <int> .Replace(1, 100)); target.Is(2, 100); s.OnNext(CollectionChanged <int> .Reset); target.Count.Is(0); }
public void ToCollectionChangedTest() { var source = new ObservableCollection <string>(); var target = source.ToReadOnlyReactiveCollection(); var values = new List <CollectionChanged <string> >(); target.ToCollectionChanged().Subscribe(x => values.Add(x)); source.Add("abc"); source.Add("def"); source.Remove("abc"); source.Clear(); values.Is( new[] { CollectionChanged <string> .Add(0, "abc"), CollectionChanged <string> .Add(1, "def"), CollectionChanged <string> .Remove(0, "abc"), CollectionChanged <string> .Reset, }, (x, y) => x.Action == y.Action && x.Value == y.Value); }
public static IObservable <CollectionChanged <T> > ObserveHistory <T>(this IObservable <T> observable, int maxHistoryCount) { int count = 0; return(observable.SelectMany(value => { if (count < maxHistoryCount) { return new[] { CollectionChanged <T> .Add(count++, value) }; } else { return new[] { CollectionChanged <T> .Remove(0, default(T)), CollectionChanged <T> .Add(maxHistoryCount - 1, value), }; } })); }