从 MVVM 属性 调用异步方法

Calling async method from MVVM property

我正在我的 Xamarin Forms 5 应用程序的视图模型中实现我自己的自动完成版本。

我需要从 Keyword MVVM 属性 调用 async 查找函数。不确定我是否处理正确。见下文:

string keyword { get; set; }
ObservableRangeCollection<string> suggestions = new ObservableRangeCollection<string>();
public string Keyword
{
   get => keyword;
   set
   {
      if(keyword == value)
         return;

      keyword = value;
      OnPropertyChanged();

      // If keyword has at least 3 characters, get suggestions
      if(keyword.length > 2)
         GetSuggestions(keyword).Wait();
   }
}

ObservableRangeCollection<string> Suggestions
{
   get => suggestions;
   set
   {
      if(sugggestions == value)
         return;

      suggestions = value;
      OnPropertyChanged();
   }
}

async Task GetSuggestions(string searchKeyword)
{
   var result = await _myApiService.GetSuggestions(searchKeyword);
   if(result != null)
   {
      Suggestions = new ObservableRangeCollection(result);
      OnPropertyChanged(Suggestions);
   }
}

如有任何更正或建议,我将不胜感激。谢谢

你肯定 don't want to block on async code 在这里(如我的博客中所述)。更一般地说,您不想阻止 UI,尤其是当您的用户正在与之交互(即打字)时。

相反,使用类似 async data binding 的模式(同步)开始 操作,然后 更新 你的 UI 当结果出来时。

例如,使用 here 中的 NotifyTask<T>

string keyword { get; set; }
NotifyTask<List<string>> suggestions;
public string Keyword
{
   get => keyword;
   set
   {
      if (keyword == value)
         return;

      keyword = value;
      OnPropertyChanged();

      // If keyword has at least 3 characters, get suggestions
      if (keyword.length > 2)
         suggestions = NotifyTask.Create(GetSuggestionsAsync(keyword));
   }
}

NotifyTask<List<string>> Suggestions
{
   get => suggestions;
}

async Task<List<string>> GetSuggestionsAsync(string searchKeyword)
{
   return await _myApiService.GetSuggestions(searchKeyword);
}

然后,不是data-binding到Suggestions作为string的集合,而是data-bind到Suggestions.Result。您还可以 data-bind 到 Suggestions.IsCompleted(或 Suggestions.IsNotCompleted)显示 loading/busy 指标,Suggestions.IsFaulted 显示错误消息。