Tutorial :Desperately need help on Rx.Net



Question:

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!!!


Solution:1

Probably too late, but I suggest that you use the strongly-typed FromEventPattern method whenever you want an observable from an event.

IObservable<IEvent<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(      Func<EventHandler<TEventArgs>, TDelegate> conversion,      Action<TDelegate> addHandler,      Action<TDelegate> removeHandler)      where TEventArgs: EventArgs  

In your code you would use it like this:

public partial class MainWindow : Window    {        /// <summary>        /// Initializes a new instance of the <see cref="MainWindow"/> class.        /// </summary>        public MainWindow()        {            InitializeComponent();              var xs = Observable              .FromEventPattern<MouseButtonEventHandler, MouseButtonEventArgs>(                  h => (s, ea) => h(s, ea),                  h => this.MouseDown += h,                  h => this.MouseDown -= h);            _subscription = xs              .ObserveOnDispatcher()              .Subscribe(_ => txtClicked.Text = "Clicked");      }        private IDisposable _subscription = null;  }  

Also you should use a subscription variable (or subscriptions list) to hold the IDisposable returned from the Subscribe call. And just like removing an event handler when you're closing down your form, you should also dispose of your subscription too when you're done.


Solution:2

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.)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »