public ReactiveCollection1ViewModel() { AddCommand = new ReactiveCommand().AddTo(CompositeDisposable); // ReactiveCommand(正確には IObservable<T>) から ReactiveCollection<T> を作成 // この場合やったら、ReadOnlyReactiveCollection<T> の方がスッキリする。我ながら例が悪い。 SourceValues = AddCommand .Select(_ => DateTime.Now.ToString("hh:mm:ss.ff")) .ToReactiveCollection() .AddTo(CompositeDisposable); // ReactiveCollection<T> から ReactiveCommand を作成 RemoveCommand = SourceValues .CollectionChangedAsObservable() .Select(_ => SourceValues.Any()) .ToReactiveCommand(initialValue: false) .WithSubscribe(() => SourceValues.RemoveAt(0), CompositeDisposable.Add); ClearCommand = SourceValues .CollectionChangedAsObservable() .Select(_ => SourceValues.Any()) .ToReactiveCommand(initialValue: false) .WithSubscribe(() => SourceValues.Clear(), CompositeDisposable.Add); // Remove 達の CanExecute を false にするため、コレクションを操作する // ◆もっと良い実装ない? ⇒ initialValue: false の実装に変えた。 //SourceValues.Clear(); }
public Func <TTarget, TSource, TTarget> GetMapperFunc() { ValidateMapping(); var targetParam = Expression.Parameter(typeof(TTarget)); var sourceParam = Expression.Parameter(typeof(TSource)); var setterActions = TargetValues.OrderBy(x => x.PropertyName) .Zip(SourceValues.OrderBy(x => x.PropertyName), (tgt, src) => tgt.CreateSetter(src.CreateGetter())) .Concat(CustomMappings) .Select(x => EnsureReturnsTarget(x)) .ToArray() ; if (!setterActions.Any()) { return((tgt, src) => tgt); } var accumulatedLambda = Expression.Invoke(setterActions.First(), targetParam, sourceParam); foreach (var setterExpr in setterActions.Skip(1)) { accumulatedLambda = Expression.Invoke(setterExpr, accumulatedLambda, sourceParam); } var mapperFunc = Expression.Lambda <Func <TTarget, TSource, TTarget> >(accumulatedLambda, targetParam, sourceParam); return(mapperFunc.Compile()); }
public IFilteredReadOnlyObservableCollection1ViewModel() { AddCommand = new ReactiveCommand().AddTo(CompositeDisposable); ClearCommand = new ReactiveCommand().AddTo(CompositeDisposable); SourceValues = AddCommand .Select(_ => new ValueHolder(SourceValues.Count)) .ToReadOnlyReactiveCollection(onReset: ClearCommand.ToUnit()) .AddTo(CompositeDisposable); FilteredValues = SourceValues .ToFilteredReadOnlyObservableCollection(x => (x.Value % 3) == 0) .AddTo(CompositeDisposable); }
public void FillAllInfo() { IsValid = LineHelper.IsValid(Source); if (!IsValid) { return; } SourceValues = LineHelper.Separate(Source); if (SourceValues.Any()) { ParsedValues = LineHelper.Parse(SourceValues).ToList(); } if (ParsedValues.Any()) { Sum = ParsedValues.Sum(); } }
private void ValidateMapping() { var targetNames = TargetValues.Select(x => x.PropertyName); var sourceNames = SourceValues.Select(x => x.PropertyName); var unmatchedTargets = targetNames.Except(sourceNames); foreach (var targetProperty in GetUnmappedTargetValues()) { ThrowUnmatchedTarget(targetProperty); } var unmatchedSources = sourceNames.Except(targetNames); foreach (var sourceProperty in unmatchedSources) { ThrowUnmatchedSource(SourceValues.First(x => x.PropertyName == sourceProperty)); } var mismatchedTypes = SourceValues.OrderBy(x => x.PropertyName) .Zip(TargetValues.OrderBy(x => x.PropertyName), (src, tgt) => new { src, tgt }) .Where(x => !x.tgt.ValueType.IsAssignableFrom(x.src.ValueType)); foreach (var mismatch in mismatchedTypes) { var msg = string.Format( "Cannot map [{0}] from [{1}].", mismatch.tgt.Description, mismatch.src.Description ); throw new Exception(msg); } }
public IEnumerable <SourceValue <TSource> > GetUnmappedSourceValues() { var targetValuesByName = TargetValues.ToDictionary(x => x.PropertyName); return(SourceValues.Where(x => !targetValuesByName.ContainsKey(x.PropertyName))); }
public IEnumerable <ITargetValue <TTarget> > GetUnmappedTargetValues() { var sourcesByName = SourceValues.ToDictionary(x => x.PropertyName); return(TargetValues.Where(x => !sourcesByName.ContainsKey(x.PropertyName))); }