Reactive Framework in Silverlight 4

Posted by psoszynski | Posted in Reactive Extensions, Silverlight | Posted on 12-09-2010-05-2008

0

I have just watched a highly recommended video by Wes Dyer on Channel 9 titled “Writing your first Rx Application”. In this screen cast, Wes presents the fundamental concepts behind the the Rx framework, creating a simplistic WPF application. To make my learning experience more fun I decided to follow the screencast and simultaneously “adapt” the code so that it worked in a Silverlight application. It turned out to be very easy. Here is the resulting source code:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

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

        var mouseUp = from evt in Observable.FromEvent<MouseButtonEventArgs>(this, "MouseLeftButtonUp")
                        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.Subscribe(value => Dispatcher.BeginInvoke(

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

            ));
    }
}

 

Alternatively,  instead of Despatcher.BeginInvoke(…) we can use the IObservable<T> extension method ObservableOnDispatcher<T>:

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

 

which gives the same effect:

Get Microsoft Silverlight

Switching to WordPress

Posted by psoszynski | Posted in General | Posted on 11-09-2010-05-2008

0

I have decided to change the blog engine to WordPress. It seems to have a lot more themes to choose from, has better reputation and cooperates very well with Windows Live Writer. Time will show if it was a good decision or not.

My First Windows Phone 7 Game

Posted by psoszynski | Posted in Silverlight | Posted on 10-09-2010-05-2008

1

           Initially, I just wanted to create some application to test the speed of animation on the new Phone emulator. However, once I had done some work, I wanted to add new features. After a couple of hours it looked like a small game, so I though I would just keep going until I am done. And, here it is! Of course this is just a photo-montage. I embedded a Silverlight 3.0 application object in an image of the Windows Phone. The whole game was developed with the new Visual Studio 2010 for Windows Phone and it works on the emulator. Only at the end I just moved the source files to a regular IDE so that I could build an application that could be shown in a blog entry.

           The objective of the game is to press these falling letters that correspond to the phrase written on top of the screen. Every time a correct one is pressed it is marked with a color. Mistakes are penalized with additional 10 seconds. After marking the whole phrase and when all the remaining letters have fallen off the screen you can start over by pressing the phrase. You can go to full screen anytime, by pressing the time at the bottom.

Upcoming features:

  • implement the detection of the screen/phone orientation so that the letters keep falling down instead of to the side
  • generate the phrases from a web service (amazon search for Silverlight book authors?)