如何在 silverlight 中获取 IObservable.ToTask 功能
在未提供 TPL 的 Silverlight 中,如何实现使用 IObservable ToTask 方法获得的行为? 我希望异常能够将结果提升为 IEnumerable,并且能够阻止并等待 Observable 完成。
以下是我使用 ToTask 所做的示例:
int collectionSize = 200;
service.Collection.CreateSampleCollection(size: collectionSize).Wait();
var query = (QuerySource)service.Collection.CollectionTestModel.All();
var queryTask = query.ToTask();
query.Fetch();
var entities = queryTask.Result;
CollectionAssert.AreEquivalent(entities.Select(e => (int)(long)e["Number"]), Enumerable.Range(0, collectionSize));
service.Collection.DeleteTestCollection().Wait();
那么将此代码转换为其他 RX 扩展的简单方法是什么? 使用 Run 我必须添加 onNext 和 onError(?) 并且必须在 onNext 中聚合结果。有没有更简单的方法来做到这一点?
How can I achieve the behavior you get with IObservable ToTask method in Silverlight where the TPL is not provided ?
I want exceptions to get promoted up results as IEnumerable and be able to block and wait for Observable to complete.
Here is an example of what I'm doing with ToTask :
int collectionSize = 200;
service.Collection.CreateSampleCollection(size: collectionSize).Wait();
var query = (QuerySource)service.Collection.CollectionTestModel.All();
var queryTask = query.ToTask();
query.Fetch();
var entities = queryTask.Result;
CollectionAssert.AreEquivalent(entities.Select(e => (int)(long)e["Number"]), Enumerable.Range(0, collectionSize));
service.Collection.DeleteTestCollection().Wait();
So what would be a simple way to translate this code in to other RX extensions ?
Using Run I have to add onNext and onError(?) and I have to aggregate the results in onNext. Is there a simpler way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
First()
进行阻塞,直到有可用值为止(同样,Last
也会阻塞)。如果您需要所有值,可以下载最新的构建 Rx 并使用
.ToArray().First()
。You can use
First()
to block until there is a value available (likewise,Last
also blocks).If you need all the values, you can download the latest build of Rx and use
.ToArray().First()
.如果您没有最新版本的 Rx,ToList 也很容易编写:
And if you don't have the latest build of Rx, ToList is easy enough to write anyways: