Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - ReactiveUI InvokeCommand not working

Tags:

c#

wpf

reactiveui

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

like image 948
Michal Avatar asked Oct 15 '25 14:10

Michal


1 Answers

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);
like image 145
mm8 Avatar answered Oct 18 '25 05:10

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!