MainWindow.xaml.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using PortableLibraryProfile78_NuGet;
  17. namespace WpfApp45_NuGet
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. private void button1_Click(object sender, RoutedEventArgs e)
  29. {
  30. var clock = MyExtensions.GetClock().AsQbservable().Select(_ => _).AsObservable();
  31. var input = Observable.FromEventPattern<TextChangedEventArgs>(textBox1, "TextChanged").Select(evt => ((TextBox)evt.Sender).Text).Throttle(TimeSpan.FromSeconds(.5)).DistinctUntilChanged();
  32. var xs = from word in input.StartWith("")
  33. from length in Task.Run(async () => { await Task.Delay(250); return word.Length; })
  34. select length;
  35. var res = xs.CombineLatest(clock, (len, now) => now.ToString() + " - Word length = " + len);
  36. res.ObserveOnDispatcher().Subscribe(s =>
  37. {
  38. label1.Text = s.ToString();
  39. });
  40. }
  41. }
  42. }