C# 反应式UI 6:如何在版本6中实现与版本4相同的功能

C# 反应式UI 6:如何在版本6中实现与版本4相同的功能,c#,wpf,asynchronous,system.reactive,reactiveui,C#,Wpf,Asynchronous,System.reactive,Reactiveui,我的代码来自旧示例反应式UI 4: StartAsyncCommand = new ReactiveCommand(); StartAsyncCommand.RegisterAsyncAction(_ => { Progress = 0; var exe = Enumerable.Range(0, 10).Select(x => {

我的代码来自旧示例反应式UI 4:

StartAsyncCommand = new ReactiveCommand();
        StartAsyncCommand.RegisterAsyncAction(_ =>
        {
            Progress = 0;
            var exe = Enumerable.Range(0, 10).Select(x =>
                            {
                                Thread.Sleep(100);
                                return x;
                            }).ToObservable();
            exe.Subscribe(x =>Progress += 10);
        });
它工作正常,UI不阻塞,按钮被禁用,直到进度达到100%

当我迁移到版本6时,我尝试了许多方法来实现相同的功能,但都没有成功

以下是我的尝试:

1使用CreateAsyncObservable

它的工作,但按钮只禁用,直到等待结束

3基于

同样的结果

如何使用版本6实现相同的功能


我做错了什么?

这是一种非常奇怪的方式来做你想做的事情。这个怎么样

StartAsyncCommand = ReactiveCommand.CreateAsyncObservable(_ => 
    Observable.Timer(DateTimeOffset.Zero, TimeSpan.FromSeconds(1))
        .Take(10)
        .Scan(0, (acc,x) => acc + 10));

StartAsyncCommand.ToProperty(this, x => x.Progress, out progress);

这是一种非常奇怪的方式来做你想做的事情。这个怎么样

StartAsyncCommand = ReactiveCommand.CreateAsyncObservable(_ => 
    Observable.Timer(DateTimeOffset.Zero, TimeSpan.FromSeconds(1))
        .Take(10)
        .Scan(0, (acc,x) => acc + 10));

StartAsyncCommand.ToProperty(this, x => x.Progress, out progress);

你为什么要做Enumerable.Range…ToObservable而不是Observable.Range。。。?在可能的情况下,您应该始终避免从可枚举到可观察。为什么您还要订阅任务中的可观察对象?你似乎不明白如何正确使用Rx。谢谢你的建议,但问题是如何实现相同的功能。。。正如我所说,我尝试了几个代码。。。我用我也尝试过的其他代码更新了这个问题。。。我希望你能帮我举个例子!我不是在回答这个问题,我只是在评论你的代码。我的感觉是,你的麻烦源于你混合TPL、Rx和Linq可枚举的方式。你为什么要做可枚举的。范围…ToObservable而不是Observable。范围。。。?在可能的情况下,您应该始终避免从可枚举到可观察。为什么您还要订阅任务中的可观察对象?你似乎不明白如何正确使用Rx。谢谢你的建议,但问题是如何实现相同的功能。。。正如我所说,我尝试了几个代码。。。我用我也尝试过的其他代码更新了这个问题。。。我希望你能帮我举个例子!我不是在回答这个问题,我只是在评论你的代码。我的感觉是,您的麻烦源于您混合TPL、Rx和Linq可枚举项的方式。
GenerateCommand = ReactiveCommand.CreateAsyncTask(canGenerate, x => DoSomething());

public async Task<Unit> DoSomething()
{
        var execTask = Task.Factory.StartNew(() =>
        {
            var exe = Enumerable.Range(0, 10).Select(
                            x =>
                            {
                                Thread.Sleep(200);
                                return x;
                            }).ToObservable();
            exe.Subscribe(x =>
                    Progress += 10
                );
            return new Unit();
        });

        return execTask.Result; 
 }
GenerateCommand = ReactiveCommand.CreateAsyncObservable(canGenerate, x => DoSomething());
GenerateCommand.ObserveOnDispatcher().SubscribeOn(NewThreadScheduler.Default)
    .Subscribe(x => Progress += 10);

    public IObservable<int> DoSomething()
    {
        return = Observable.Range(0, 10).Select(
            x =>
            {
                Thread.Sleep(200);
                return x;
            });
    }
StartAsyncCommand = ReactiveCommand.CreateAsyncObservable(_ => 
    Observable.Timer(DateTimeOffset.Zero, TimeSpan.FromSeconds(1))
        .Take(10)
        .Scan(0, (acc,x) => acc + 10));

StartAsyncCommand.ToProperty(this, x => x.Progress, out progress);