public ProductEditWindowViewModel(IContainer container, ILogger <ProductEditWindowViewModel> logger) { _container = container; _logger = logger; LoadedCommand.Subscribe(OnLoaded); SubmitCommand.Subscribe(OnSubmit); }
public SampleViewModel() { theModel = new SampleItemObservable { Id = 101, Name = "name 101", UpdateDateTime = new DateTime(), Flag = true }; ItemViewModel = new SampleItemViewModel(theModel); SubmitCommand.Subscribe(() => Submit()); }
public ValidationWindowViewModel() { WithDataAnnotations = new ReactiveProperty <string>() // 1) ReavtivePropertyを生成する .SetValidateAttribute(() => WithDataAnnotations) // 2) WithDataAnnotationsに割り当たっている属性を1)で作ったインスタンスにセットする .AddTo(Disposables); // 3) まとめてDisposeできるように登録 WithDataAnnotationsErrMessage = WithDataAnnotations // 1) 情報ソースから .ObserveValidationErrorMessage() // 2) ValidationエラーメッセージをObserveする .ToReadOnlyReactivePropertySlim() // 3) ReactivePropertyに変換 .AddTo(Disposables); // 4) まとめてDisposeできるように登録 WithCustomValidationLogic = new ReactiveProperty <string>() .SetValidateNotifyError(x => !string.IsNullOrEmpty(x) && x.Contains("-") ? null : "Require '-'") .AddTo(Disposables); WithCustomValidationLogicErrMessage = WithCustomValidationLogic .ObserveValidationErrorMessage() .ToReadOnlyReactivePropertySlim() .AddTo(Disposables); HasValidationErrors = new[] { WithDataAnnotations.ObserveHasErrors, WithCustomValidationLogic.ObserveHasErrors, }.CombineLatest(x => x.Any(y => y)) .ToReactiveProperty() .AddTo(Disposables); //HasValidationErrors = WithDataAnnotations.ObserveHasErrors // .CombineLatest(WithCustomValidationLogic.ObserveHasErrors, (x, y) => x | y) // .ToReactiveProperty() // .AddTo(Disposables); ////https://blog.okazuki.jp/entry/20120219/1329663635 SubmitCommand = new[] { WithDataAnnotations.ObserveHasErrors.Inverse(), // WithDataAnnotationsにエラーがないこと WithCustomValidationLogic.ObserveHasErrors.Inverse(), // WithCustomValidationLogicにエラーがないこと }.CombineLatestValuesAreAllTrue() // どちらかのイベントが発行されたときに、どちらもTrueであること .ToReactiveCommand() // コマンドに変更 .AddTo(Disposables); // コマンド処理処理の登録 SubmitCommand.Subscribe(_ => Trace.WriteLine("SubmitCommand")); Message = WithDataAnnotations.CombineLatest(WithCustomValidationLogic, (x, y) => $"You submitted \'{x} & {y}\'") //.Throttle(TimeSpan.FromMilliseconds(1000)) // 1000ms間値が変化しなかったら、それまでの値を流す。 .Throttle(_ => SubmitCommand) // SubmitCommandが実行されたときに、それまでの値を流す。。。?? .ToReadOnlyReactivePropertySlim(); // 最初のValidationエラーを無視してくれる IgnoreInitialValidationError = new ReactiveProperty <string>(mode: ReactivePropertyMode.Default | ReactivePropertyMode.IgnoreInitialValidationError) .SetValidateAttribute(() => IgnoreInitialValidationError) .AddTo(Disposables); // ModelとシンクしているプロパティのValidation FirstName = poco.ToReactivePropertyAsSynchronized(x => x.FirstName, ignoreValidationErrorValue: true) .SetValidateAttribute(() => FirstName) // ↑ ignoreValidationErrorValue: エラー時にModelに値を反映させるかどうかのフラグ .AddTo(Disposables); }
public EnquetePageViewModel(INavigationService navigationService, IOpenWebPageService webService, IAnalyticsService analyticsService, ILocalConfigService configService, IConstUrls constUrls) { _navigationService = navigationService; _webService = webService; _analyticsService = analyticsService; _configService = configService; Ages = AGE.Keys.ToList(); Members = MEMBER.Keys.ToList(); Residences = RESIDENCE.Keys.ToList(); Wheres = WHERE.Keys.ToList(); Accesses = ACCESS.Keys.ToList(); AgeValidation = AgeSelected.SetValidateNotifyError((val) => string.IsNullOrEmpty(val) ? "年齢が選択されていません" : null).AddTo(this.Disposable); MemberValidation = MemberSelected.SetValidateNotifyError((val) => string.IsNullOrEmpty(val) ? "所属が選択されていません" : null).AddTo(this.Disposable); ResidenceValidation = ResidenceSelected.SetValidateNotifyError((val) => string.IsNullOrEmpty(val) ? "居住地が選択されていません" : null).AddTo(this.Disposable); WhereValidation = WhereSelected.SetValidateNotifyError((val) => string.IsNullOrEmpty(val) ? "どこで知ったのかが選択されていません" : null).AddTo(this.Disposable); AccessValidation = AccessSelected.SetValidateNotifyError((val) => string.IsNullOrEmpty(val) ? "どのように来たのかが選択されていません" : null).AddTo(this.Disposable); AgeSelected.AddTo(this.Disposable); MemberSelected.AddTo(this.Disposable); ResidenceSelected.AddTo(this.Disposable); WhereSelected.AddTo(this.Disposable); AccessSelected.AddTo(this.Disposable); SubmitCommand = new[] { AgeValidation.ObserveHasErrors, MemberValidation.ObserveHasErrors, ResidenceValidation.ObserveHasErrors, WhereValidation.ObserveHasErrors, AccessValidation.ObserveHasErrors, } .CombineLatestValuesAreAllFalse() .ToReactiveCommand() .AddTo(this.Disposable); SubmitCommand.Subscribe(async() => { await Submit(); await SaveUserProfile(); await _navigationService.NavigateAsync("/AppNavigationRootPage/NavigationPage/HomePage"); }).AddTo(this.Disposable); SkipCommand = new DelegateCommand(async() => { SetValueSecret(); await Submit(); await SaveUserProfile(); await _navigationService.NavigateAsync("/AppNavigationRootPage/NavigationPage/HomePage"); }); TermsOfUseCommand = new DelegateCommand(async() => { await _webService.OpenUri(constUrls.TermsOfUseUrl); }); }