WPF - ReactiveUI InvokeCommand 不起作用

Mic*_*hal 2 c# wpf reactiveui

我正在尝试学习 ReactiveUI,所以我正在制作示例应用程序,但我在使用InvokeCommand. 基本上每次SearchPhrase更改属性时都ShowSearchPhraseCommand应该调用我。

这是我的观点:

<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal"
    VerticalAlignment="Center" >


    <TextBox Width="100" Height="20" 
     Text="{Binding Path=SearchPhrase, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

视图模型:

public ReactiveCommand ShowSearchPhraseCommand { get; private set; }

string _searchPhrase;
public string SearchPhrase
{
    get { return _searchPhrase; }
    set { this.RaiseAndSetIfChanged(ref _searchPhrase, value); }
}

public SecondViewModel(IScreen hostScreen)
{
    HostScreen = hostScreen;

    // Commands
    ShowSearchPhraseCommand = ReactiveCommand.Create(() => ShowSearchPhraseCommandHandler(SearchPhrase));

    // WhenAny
    this.WhenAnyValue(x => x.SearchPhrase).Where(x => !string.IsNullOrWhiteSpace(x)).InvokeCommand(ShowSearchPhraseCommand);
}

private void ShowSearchPhraseCommandHandler(string searchPhrase)
{
    Debug.WriteLine($"Phrase: {searchPhrase}");
}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

在此处输入图片说明

mm8*_*mm8 7

该命令需要一个Unit

this.WhenAnyValue(x => x.SearchPhrase)
    .Where(x => !string.IsNullOrWhiteSpace(x))
    .Select(_ => System.Reactive.Unit.Default) //<--
    .InvokeCommand(ShowSearchPhraseCommand);
Run Code Online (Sandbox Code Playgroud)

...除非您将其定义为ReactiveCommand<string, Unit>

public ReactiveCommand<string, System.Reactive.Unit> ShowSearchPhraseCommand { get; private set; }
...
ShowSearchPhraseCommand = ReactiveCommand.Create<string>(ShowSearchPhraseCommandHandler);
Run Code Online (Sandbox Code Playgroud)