What's different about the 3 versions of Rx? Part 1: Silverlight 3
- Posted: Nov 17, 2009 at 12:22 PM
- 6,302 Views
- 1 Comment
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
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.
Remove this comment
Remove this thread
close