我在填充和显示 XLabs.Forms.Control:AutoCompleteView 的建议列表时遇到了困难。我已经将 ViewModel 中的 observable 集合绑定到 autocompleteview xaml 的 Suggestions 属性。
根据我的调试代码(即只是一个将查询返回的内容写入调试输出的循环),我的查询正在返回项目,所以我认为问题在于只显示所述项目。
这是 Xaml 和 ViewModel 的代码(Store 类有一个 StoreName 属性/字段)
XAML
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="SugestionItemTemplate">
<ViewCell Height="60">
<ViewCell.View>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Label Text="{Binding StoreName}" VerticalOptions="Center" HorizontalOptions="Start" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout HorizontalOptions="Center" Spacing="10">
<StackLayout.BindingContext>
<vm:CreateSaleViewModel />
</StackLayout.BindingContext>
<Label Text="Store" />
<controls:AutoCompleteView Placeholder="Type a store"
SuggestionItemDataTemplate="{StaticResource SugestionItemTemplate}"
Text="{Binding StoreQuery}"
ShowSearchButton="True"
SearchBackgroundColor = "White"
SearchCommand ="{Binding SearchCmd}"
Suggestions="{Binding StoreSuggestions}" />
</StackLayout>
视图模型
class CreateSaleViewModel
{
// Query Variables
public string StoreQuery { get; set; }
// Query Suggestions
public ObservableCollection<Store> StoreSuggestions { get; private set; }
public ICommand SearchCmd { get; set; }
public CreateSaleViewModel()
{
SearchCmd = new Command(Search);
}
private async void Search()
{
StoreSuggestions = await App.AzureDataStore.SearchStoresAsync(StoreQuery);
}
}