I'm implementing auto complete feature in my .NET MAUI app and I'm using CommunityToolkit.Mvvm
code generators in my view model to handle observable properties.
I have the following code and I'm trying call GetSuggestions()
method when the SearchText
changes.
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(GetSuggestions))]
string searchText;
[ObservableProperty]
bool showSuggestions;
ObservableCollection<string> Suggestions { get; } = new();
private async Task GetSuggestions()
{
if(string.IsNullOrEmpty(SearchText) || SearchText.Length < 3)
return;
var data = await _myApiService.GetSuggestions(SearchText.Trim());
if(data != null && data.Count > 0)
{
Suggestions.Clear();
foreach(var item in data)
Suggestions.Add(item);
ShowSuggestions = true;
}
}
This is giving me the following error:
The target(s) of [NotifyCanExecuteChangedFor] must be an accessible IRelayCommand property, but "GetSuggestions" has no matches in type MyViewModel.
What am I doing wrong here?
GetSuggestions()
is anasync
method. If I make itpartial void OnSearchTextPropertyChanged()
it works but it complains that I'm not awaitingGetSugestions()
. It works fine though. Do I not need toawait
the call toGetSuggestions()
method inside theOnSearchTextPropertyChanged()
method? – Spermatic