CreateAsyncObservable 不引发 OnComplete [英] CreateAsyncObservable not raising OnComplete

查看:42
本文介绍了CreateAsyncObservable 不引发 OnComplete的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在玩弄 ReactiveUi.

Still playing around with ReactiveUi.

我目前无法确定序列何时结束.我有以下代码:

I'm currently having trouble determining when a sequence has ended. I have the following code :

    public RxTest()
    {
        DownloadDocument = ReactiveCommand.CreateAsyncObservable(_ => GetItems().ToObservable());
        DownloadDocument.Subscribe(Console.WriteLine, () => Console.WriteLine("Done"));
    }

    public ReactiveCommand<int> DownloadDocument { get; set; }

    public void RunCommand()
    {
        DownloadDocument.Execute(new object());
    }

    IEnumerable<int> GetItems()
    {
        for (int i = 0; i < 20; i++)
        {
            Thread.Sleep(100);
            yield return i;
        }
    }

数字 1 到 19 被打印出来,但Done"没有被打印出来(即 OnComplete 没有被提升).

The numbers 1 to 19 are printed, but the "Done" does not get printed (ie, OnComplete isn't raised).

相同(相似)的代码在纯 Rx 中工作得很好(我使用 Linqpad 来测试这个,但在我的测试应用程序中也能正常工作)......

The same (similar) code works just fine in pure Rx (I used Linqpad to test this, but also worked when shoe horned into my test application) ...

{
    IObservable<int> range = GetItems().ToObservable(Scheduler.NewThread);
    range.ObserveOn(Scheduler.Default).Subscribe (Console.WriteLine, () => Console.WriteLine("Done"));
}

推荐答案

ReactiveCommands 从不 OnComplete 因为在任何时候,您都可以再次单击按钮 :) 如果您愿意要捕获 ReactiveCommand 的单个调用结果,请使用 ExecuteAsync 方法.

ReactiveCommands never OnComplete because at any point, you could click the button again :) If you want to capture the result of a single invocation of ReactiveCommand, use the ExecuteAsync method.

这篇关于CreateAsyncObservable 不引发 OnComplete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆