示例#1
0
        public ViewModelReactive2()
        {
            #region scenario 1

            //var textChangedSequence = Observable.FromEventPattern<PropertyChangedEventArgs>(this,
            //    nameof(PropertyChanged));

            //var throttledTextChangedSequence = textChangedSequence
            //    .Where(a => a.EventArgs.PropertyName == nameof(EnteredSearchQuery))
            //    .Select(a => EnteredSearchQuery)
            //    .DistinctUntilChanged()
            //    .Throttle(TimeSpan.FromSeconds(.5));

            #endregion

            #region scenario 2

            var throttledTextChangedSequence = this.GetPropertyAsObservable(() => EnteredSearchQuery)
                                               .DistinctUntilChanged()
                                               .Throttle(TimeSpan.FromSeconds(.5));

            #endregion

            var result = from qry in throttledTextChangedSequence
                         from images in SearchDataService.Search(qry).TakeUntil(throttledTextChangedSequence)
                         select images?.Select(a => a.thumb_url);

            throttledTextChangedSequence.ObserveOn(
                SynchronizationContext.Current).Subscribe(e => UpdateList(null));

            result.ObserveOn(
                SynchronizationContext.Current).Subscribe(UpdateList);
        }
        private void DoReactive()
        {
            var textChangedSequence =
                Observable.FromEventPattern <TextChangedEventArgs>(SearchBox,
                                                                   nameof(SearchBox.TextChanged));

            var throttledTextChangedSequence =
                textChangedSequence.Select(x => ((TextBox)x.Sender).Text)
                .DistinctUntilChanged()
                .Throttle(TimeSpan.FromSeconds(.5));

            var result = (from qry in throttledTextChangedSequence
                          from images in SearchDataService.Search(qry)
                          .TakeUntil(throttledTextChangedSequence)
                          select images);

            result.ObserveOn(SynchronizationContext.Current).Subscribe(
                (e) => ViewModel.Results = e?.Select(a => a.thumb_url).ToList());
        }