MainPage.xaml.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Reactive.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Animation;
  12. using System.Windows.Shapes;
  13. using Microsoft.Phone.Controls;
  14. namespace WindowsPhoneApp7_NoPlatformServices
  15. {
  16. public partial class MainPage : PhoneApplicationPage
  17. {
  18. // Constructor
  19. public MainPage()
  20. {
  21. InitializeComponent();
  22. }
  23. private void button1_Click(object sender, RoutedEventArgs e)
  24. {
  25. var clock = MyExtensions.GetClock().AsQbservable().Select(_ => _).AsObservable();
  26. var input = Observable.FromEventPattern<TextChangedEventArgs>(textBox1, "TextChanged").Select(evt => ((TextBox)evt.Sender).Text).Throttle(TimeSpan.FromSeconds(.5)).DistinctUntilChanged();
  27. var xs = from word in input.StartWith("")
  28. from length in Observable.Return(word.Length).Delay(TimeSpan.FromSeconds(.5))
  29. select length;
  30. var res = xs.CombineLatest(clock, (len, now) => now.ToString() + " - Word length = " + len);
  31. res.ObserveOnDispatcher().Subscribe(s =>
  32. {
  33. label1.Text = s.ToString();
  34. });
  35. }
  36. }
  37. public class MyExtensions
  38. {
  39. public static IObservable<DateTime> GetClock()
  40. {
  41. return Observable.Interval(TimeSpan.FromSeconds(1)).Select(_ => DateTime.Now);
  42. }
  43. }
  44. }