Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Extension - problem use method from old version in new version

I am newbie in .NET my previous job was PLC programmer. I have old application in which I used Reactive Extension for .NET.

I don’t maintain this app a long time. Now I downloaded new version for Reactive Extension but I have problem with using old code.

I know that some parts in Rx was changed.

Here is a problem old code:

    Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
    .Where(e => e.EventArgs.PropertyName == "Nick")
    .Select(_ => this.Nick)
    .Where(text => text.Length > 3)
    .Do(LoadUser)
    .Throttle(TimeSpan.FromSeconds(3000))
    .Subscribe(LoadUser);

I got this exceptions:

Error   3   Argument 1: cannot convert from 'Spirit.ViewModels.AddFriendViewModel' to 'System.Action<System.Action<System.ComponentModel.PropertyChangedEventArgs>>'    E:\C#_Projects\Pokec_Messenger\Spirit_Caliburn_Micro_v1.0\ViewModels\AddFriendViewModel.cs  123 60  Spirit_Caliburn_Micro_v1.0
Error   4   Argument 2: cannot convert from 'string' to 'System.Action<System.Action<System.ComponentModel.PropertyChangedEventArgs>>'  E:\C#_Projects\Pokec_Messenger\Spirit_Caliburn_Micro_v1.0\ViewModels\AddFriendViewModel.cs  123 65  Spirit_Caliburn_Micro_v1.0
Error   2   The best overloaded method match for 'System.Reactive.Linq.Observable.FromEvent<System.ComponentModel.PropertyChangedEventArgs>(System.Action<System.Action<System.ComponentModel.PropertyChangedEventArgs>>, System.Action<System.Action<System.ComponentModel.PropertyChangedEventArgs>>)' has some invalid arguments E:\C#_Projects\Pokec_Messenger\Spirit_Caliburn_Micro_v1.0\ViewModels\AddFriendViewModel.cs  123 13  Spirit_Caliburn_Micro_v1.0

I don’t know which method I must use in new version for same funcionality.

Thank for advice.

like image 395
zelq Avatar asked Jun 09 '11 15:06

zelq


2 Answers

In the latest version (1.1.10425.0), you need to use FromEventPattern()

like image 155
Scott Weinstein Avatar answered Oct 22 '22 11:10

Scott Weinstein


The official Rx Forums post here http://social.msdn.microsoft.com/Forums/en-US/rx/thread/527002a3-18af-4eda-8e35-760ca0006b98 has a write-up of the changes that they made in 1.1.10425.0. Lee Campbell has a nice write up of the impact of some of these changes at http://leecampbell.blogspot.com/2011/06/rx-v1010425breaking-changes.html. I shared my experience upgrading my old samples at http://www.thinqlinq.com/Post.aspx/Title/Updating-Reactive-Samples-to-10425-build as well.

In your case, you're using the FromEvent method with the string of the event name. This signature was moved to FromEventPattern. You can probably do a global search and replace on FromEvent( and change it to FromEventPattern( without having much problems.

Additionally, you appear to be calling LoadUser twice in this example (in Do and then again in Subscribe). You may want to make sure you need to do that twice.

like image 35
Jim Wooley Avatar answered Oct 22 '22 10:10

Jim Wooley