反应式扩展 - 新版本中旧版本使用方法的问题
我是 .NET 的新手,我之前的工作是 PLC 程序员。我有一个旧应用程序,其中使用了 .NET 的响应式扩展。
我维护这个应用程序的时间不长。现在我下载了反应式扩展的新版本,但使用旧代码时遇到问题。
我知道 Rx 中的某些部分已更改。
这是一个问题旧代码:
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);
我得到了这个例外:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在最新版本(1.1.10425.0)中,需要使用
FromEventPattern()
In the latest version (1.1.10425.0), you need to use
FromEventPattern()
官方 Rx 论坛在此发布 http://social.msdn.microsoft.com/Forums/en-US/rx/thread/527002a3-18af-4eda-8e35-760ca0006b98 有一篇关于更改的文章他们在 1.1.10425.0 中制作。 Lee Campbell 在 http 上写了一篇很好的文章,介绍了其中一些更改的影响://leecampbell.blogspot.com/2011/06/rx-v1010425writing-changes.html。我在 http: //www.thinqlinq.com/Post.aspx/Title/Updating-Reactive-Samples-to-10425-build 以及。
在您的例子中,您使用的是 FromEvent 方法和事件名称的字符串。此签名已移至 FromEventPattern。您可以进行全局搜索并替换 FromEvent( 并将其更改为 FromEventPattern( ,不会有太多问题。
此外,您似乎在本示例中调用了 LoadUser 两次(在 Do 中,然后在 Subscribe 中再次调用)。您可能想要当然你需要这样做两次。
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.