如何在 ReactiveUI 7 中直接调用 ReactiveCommand.Execute() 正确? [英] How do I make a direct call to ReactiveCommand.Execute() in ReactiveUI 7 correct?

查看:23
本文介绍了如何在 ReactiveUI 7 中直接调用 ReactiveCommand.Execute() 正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目从 ReactiveUI 6.5 转换为版本 7.在我调用的旧版本中

//var command = ReactiveCommand.Create...;//...if(command.CanExecute(null))命令.执行(空);

为了从我后面的代码中执行命令.

现在 CanExecute 方法不再可用,取而代之的是 IObservable 属性.如果我只是调用 Execute().Subscribe() 或者我必须显式调用它,CanExecute Observable 是否会自动调用?

现在我用

替换了上面的代码

command.Execute().Subscribe();

解决方案

我找到了三种不同的解决方案来调用我的命令的 CanExecuteExecute 方法,就像我以前在 ReactiveUI 中所做的那样6.5:

选项 1

这相当于6.5版本中的调用,但我们需要将命令显式转换为ICommand:

if (((ICommand) command).CanExecute(null))命令.执行().订阅();

选项 2

if(command.CanExecute.FirstAsync().Wait())命令.执行().订阅()

或异步变体:

if(await command.CanExecute.FirstAsync())等待命令.执行()

选项 3

另一种选择是让我们使用 InvokeCommand 扩展方法.

Observable.Start(() => {}).InvokeCommand(ViewModel, vm => vm.MyCommand);

这尊重命令的可执行性,如文档中所述.

<小时>

为了让它更舒服,我编写了一个小的扩展方法来提供一个 ExecuteIfPossible 和一个 GetCanExecute 方法:

公共静态类 ReactiveUiExtensions{公共静态 IObservableExecuteIfPossible(this ReactiveCommand cmd) =>cmd.CanExecute.FirstAsync().Where(can => can).Do(async _ => await cmd.Execute());public static bool GetCanExecute(this ReactiveCommand cmd) =>cmd.CanExecute.FirstAsync().Wait();}

您可以按如下方式使用此扩展方法:

command.ExecuteIfPossible().Subscribe();

注意:最后需要Subscribe()调用,就像调用Execute()一样,否则什么都不会发生.>

或者如果你想使用异步和等待:

await command.ExecuteIfPossible();

如果你想检查一个命令是否可以执行,就调用

command.GetCanExecute()

I'm trying to convert my project from ReactiveUI 6.5 to version 7. In the old version I called

// var command = ReactiveCommand.Create...;
// ...
if(command.CanExecute(null))
    command.Execute(null);

in order to execute a command from my code behind.

Now the CanExecute method is no longer available and replaced with a property of IObservable<bool>. Is the CanExecute Observable automatically called if I just make a call to Execute().Subscribe() or must I call it explicitly?

For now I replaced the above code with

command.Execute().Subscribe();

解决方案

I found three different solutions to call my command's CanExecute and Execute methods like I could before in ReactiveUI 6.5:

Option 1

This is equal to the call in version 6.5, but we need to explicitly convert the command to an ICommand:

if (((ICommand) command).CanExecute(null))
    command.Execute().Subscribe();

Option 2

if(command.CanExecute.FirstAsync().Wait())
    command.Execute().Subscribe()

or the async variant:

if(await command.CanExecute.FirstAsync())
    await command.Execute()

Option 3

Another option is to make us of the InvokeCommand extension method.

Observable.Start(() => {}).InvokeCommand(ViewModel, vm => vm.MyCommand);

This respects the command's executability, like mentioned in the documentation.


In order to make it more comfortable I've written a small extension method to provide a ExecuteIfPossible and a GetCanExecute method:

public static class ReactiveUiExtensions
{
    public static IObservable<bool> ExecuteIfPossible<TParam, TResult>(this ReactiveCommand<TParam, TResult> cmd) =>
        cmd.CanExecute.FirstAsync().Where(can => can).Do(async _ => await cmd.Execute());

    public static bool GetCanExecute<TParam, TResult>(this ReactiveCommand<TParam, TResult> cmd) =>
        cmd.CanExecute.FirstAsync().Wait();
}

You can use this extension method as follows:

command.ExecuteIfPossible().Subscribe();

Note: You need the Subscribe() call at the end, just like you need it for the call to Execute(), otherwise nothing will happen.

Or if you want to use async and await:

await command.ExecuteIfPossible();

If you want to check if a command can be executed, just call

command.GetCanExecute()

这篇关于如何在 ReactiveUI 7 中直接调用 ReactiveCommand.Execute() 正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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