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:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With