Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

noemata

noemata noemata Noemata

Niner since 2010

yec (yet another coder)
  • Writing your first Rx Application

    For the March 2010 release of Rx, the code needs to be adjusted as follows:

     

            public Window1()
            {
                InitializeComponent();

                var xs = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(-1);

                var mouseDown = from evt in Observable.FromEvent<MouseButtonEventArgs>(this, "MouseDown")
                                select evt.EventArgs.GetPosition(this);

                var mouseUp = from evt in Observable.FromEvent<MouseButtonEventArgs>(this, "MouseUp")
                                select evt.EventArgs.GetPosition(this);

                var mouseMove = from evt in Observable.FromEvent<MouseEventArgs>(this, "MouseMove")
                                select evt.EventArgs.GetPosition(this);

                var q = from start in mouseDown
                        from delta in mouseMove.StartWith(start).TakeUntil(mouseUp)
                        .Let(mm => mm.Zip(mm.Skip(1), (prev, cur) =>
                            new { X = cur.X - prev.X, Y = cur.Y - prev.Y }))
                        select delta;

                q.ObserveOnDispatcher().Subscribe(value =>
                    {
                        Canvas.SetLeft(image, Canvas.GetLeft(image) + value.X);
                        Canvas.SetTop(image, Canvas.GetTop(image) + value.Y);
                    });
            }

  • What's different about the 3 versions of Rx? Part 1: Silverlight 3

    For the March 2010 version of Rx, the code needs to be adjusted as follows:

     

     Observable.Start(() =>
                            {
                                Thread.Sleep(3000);
                                //return "Hello World";
                            }).ObserveOnDispatcher().Subscribe(_ => button.Content = "Hello World");

    Note the addition of ObserveOnDispatcher above as compared to its absence in the video.

     

    Lastly, the original code:

     

    ThreadPool.QueueUserWorkItem(_ =>
                            {
                                Thread.Sleep(3000);
                                button.Dispatcher.BeginInvoke(() =>
                                    {
                                        button.Content = "Hello World!";
                                    });
                            }, null);

    Is quite terse as well.  Compared to my revised version, I'm not sure the character count is all that different.

     

    Nice Rx sample.  Qualifies as the "Hello World!" of Rx for Silverlight and other ilks ... here's a console app version:

     

    namespace HelloWorld
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Creating ...");

                var o = Observable.Start(() =>
                {
                    Console.WriteLine("Calculating...");
                    Thread.Sleep(3000);

                }).Subscribe(_ => Console.WriteLine("Hello World!"));

                Console.WriteLine("Created ...");

                Console.WriteLine("Starting ...");

                Console.ReadKey();
            }
        }
    }

     

    For WPF its:

    <!-- Window1.xaml -->

    <Window x:Class="HelloWorld.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
         <Button x:Name="button" Margin="63,47,77,0" VerticalAlignment="Top" Height="60" Content="Button"/>
        </Grid>
    </Window>

     

    // Window1.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Threading;

    namespace HelloWorld
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
       
       button.Click += (o, e) =>
       {
        // button.Content = "Hello World";
        
        Observable.Start(() =>
        {
         Thread.Sleep(3000);
        }).ObserveOnDispatcher().Subscribe(_ => button.Content = "Hello World!");
       };
            }
        }
    }

     

    All very similar.  Now all we need is the Winforms variations.