3

I'm trying to learn ReactiveUI so I'm making example app, but I'm having problem with InvokeCommand. Basically every time SearchPhrase property is changed my ShowSearchPhraseCommand should be invoked.

Here is my View:

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


    <TextBox Width="100" Height="20" 
     Text="{Binding Path=SearchPhrase, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>

ViewModel:

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}");
}

And here is my problem:

enter image description here

mm8
  • 163,881
  • 10
  • 57
  • 88
Michal
  • 803
  • 2
  • 9
  • 26

1 Answers1

9

The command expects a Unit:

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

...unless you define it as a ReactiveCommand<string, Unit>:

public ReactiveCommand<string, System.Reactive.Unit> ShowSearchPhraseCommand { get; private set; }
...
ShowSearchPhraseCommand = ReactiveCommand.Create<string>(ShowSearchPhraseCommandHandler);
mm8
  • 163,881
  • 10
  • 57
  • 88