【问题标题】:WPF - ReactiveUI InvokeCommand not workingWPF - ReactiveUI InvokeCommand 不起作用
【发布时间】:2017-09-25 21:39:38
【问题描述】:

我正在尝试学习 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>

视图模型:

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

这是我的问题:

【问题讨论】:

    标签: c# wpf reactiveui


    【解决方案1】:

    该命令需要 Unit:

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

    ...除非您将其定义为 ReactiveCommand&lt;string, Unit&gt;:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多