我在MVVM應用程序上工作,並試圖使用文本框文本更改註冊表來運行一個命令,每當此文本框發生新的更改時都可以取消該命令。 在無擴展它與交換機的方法是這樣完成的:ReactiveUI與RestApi的CancelableCommand
Observable.FromEventPattern<PropertyChangedEventArgs>(this, nameof(PropertyChanged))
.Where(a => a.EventArgs.PropertyName == nameof(Item))
.Select(a => Item)
.DistinctUntilChanged()
.Throttle(TimeSpan.FromSeconds(0.5))
.ObserveOn(SynchronizationContext.Current)
.Do(x => { Items= null; })
.ObserveOn(Scheduler.Default)
.Select(item => { return Observable.FromAsync(cancellationToken => _itemService.GetItemsAsyncWithCancelation(item, cancellationToken)); })
.Switch()
.ObserveOn(SynchronizationContext.Current)
.Subscribe(items => Items = items);
當我嘗試做同樣的事情ReactiveUi這就是我想出迄今:
public ReactiveCommand GetItemsCommand { get; set; }
GetTowaryCommand = ReactiveCommand
.CreateFromTask<string>(async (x,cancelationToken) => Items = await _itemService.GetItemsAsyncWithCancelation(x.ToString(),cancelationToken));
this.WhenAnyValue(vm => vm.Item)
.Throttle(TimeSpan.FromSeconds(0.5))
.Do(_=> GetTowaryCommand.Dispose())
.ObserveOn(SynchronizationContext.Current)
.InvokeCommand(GetItemsCommand);
這是工作,但我不知道如果RestApi電話被正確取消。 我試圖遵循ReactiveUI文檔例如: https://docs.reactiveui.net/en/user-guide/commands/canceling.html 但不知道如何字符串參數(X)傳遞給RESTAPI方法:
GetItemsCommand
.CreateFromObservable<string>(() => Observable.StartAsync(async (token) => Items = await _itemService.GetItemsAsyncWithCancelation(x.ToString(), token)))
我想我現在明白了。非常感謝。 – mdziadowiec