tags:

views:

149

answers:

2

Hi guys i'm very very very new to Rx and trying to put together a simple test app. It basically subscribes to the window click event using Rx and sets the text on a textbox to "Clicked". It's a wpf app. Here's the xaml:

<Window x:Class="Reactive.MainWindow"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
      Title="MainWindow" Height="350" Width="525">  
   <Grid>  
      <Canvas>  
        <TextBlock Name="txtClicked" Text="Rx Test"/>            
    </Canvas>  
 </Grid>  

and here's the code behind:

using System;  
using System.Linq;  
using System.Windows;  
using System.Windows.Input;  

namespace Reactive  
{
  /// <summary>  
  /// Interaction logic for MainWindow.xaml  
  /// </summary>  
public partial class MainWindow : Window  
{  
    /// <summary>  
    /// Initializes a new instance of the <see cref="MainWindow"/> class.  
    /// </summary>  
      public MainWindow()  
      {  
        InitializeComponent();  

        var xs = from evt in Observable.FromEvent<MouseEventArgs>(this, "MouseDown")
                 select evt;

        xs.ObserveOnDispatcher().Subscribe(value => txtClicked.Text = "Clicked");
    }
}
}

But for some reason the code doesn't run. i get the message:

The invocation of the constructor on type 'Reactive.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9

InnnerException message:

The event delegate must be of the form void Handler(object, T) where T : EventArgs.

Please help!!!

+1  A: 

I can't check right now, but I believe the problem is you're using the wrong EventArgs class. The Window.MouseDown event is of type MouseButtonEventHandler, so you should use MouseButtonEventArgs:

var xs = Observable.FromEvent<MouseButtonEventArgs>(this, "MouseDown");

(Your query expression wasn't really doing anything in this case - you can put it back if you want to add where clauses etc.)

Jon Skeet
A: 

Wow Jon thanks a lot!!!!!! Life's hard when you are a nuub. That totally solved the problem. Quite a silly error to make too. I'm sooo grateful, i was close to pulling my hair out(there wasn't much hair to pull).

Thanks again.

Anthony
Hope microsoft improve their error messages, it will be much appreciated!!
Anthony