gpt4 book ai didi

c# - WPF - ReactiveUI InvokeCommand 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:00 24 4
gpt4 key购买 nike

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

View 模型:

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

这是我的问题:

enter image description here

最佳答案

该命令需要一个 Unit :

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

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

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

关于c# - WPF - ReactiveUI InvokeCommand 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46378248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com