MainPage.xaml.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. using WindowsPhoneAgent7;
  15. namespace WindowsPhoneApp7_NuGet
  16. {
  17. public partial class MainPage : PhoneApplicationPage
  18. {
  19. // Constructor
  20. public MainPage()
  21. {
  22. InitializeComponent();
  23. }
  24. private void button1_Click(object sender, RoutedEventArgs e)
  25. {
  26. new ScheduledAgent();
  27. var clock = MyExtensions.GetClock().AsQbservable().Select(_ => _).AsObservable();
  28. var input = Observable.FromEventPattern<TextChangedEventArgs>(textBox1, "TextChanged").Select(evt => ((TextBox)evt.Sender).Text).Throttle(TimeSpan.FromSeconds(.5)).DistinctUntilChanged();
  29. var xs = from word in input.StartWith("")
  30. from length in Observable.Return(word.Length).Delay(TimeSpan.FromSeconds(.5))
  31. select length;
  32. var res = xs.CombineLatest(clock, (len, now) => now.ToString() + " - Word length = " + len);
  33. res.ObserveOnDispatcher().Subscribe(s =>
  34. {
  35. label1.Text = s.ToString();
  36. });
  37. }
  38. }
  39. public class MyExtensions
  40. {
  41. public static IObservable<DateTime> GetClock()
  42. {
  43. return Observable.Interval(TimeSpan.FromSeconds(1)).Select(_ => DateTime.Now);
  44. }
  45. }
  46. }