Autocomplete 填充xlabs自动完成视图的建议列表
我在填充和显示XLabs.Forms.Control的建议列表时遇到困难:AutoCompleteView。我已经将ViewModel中的observable集合绑定到autocompleteview xaml的Suggestions属性 根据我的调试代码(即,只是一个将查询返回的内容写入调试输出的循环),我的查询返回的是项,因此我认为问题在于只显示所述项 下面是Xaml和ViewModel的代码(Store类有一个StoreName属性/字段) XAMLAutocomplete 填充xlabs自动完成视图的建议列表,autocomplete,xamarin,xamarin.forms,xlabs,Autocomplete,Xamarin,Xamarin.forms,Xlabs,我在填充和显示XLabs.Forms.Control的建议列表时遇到困难:AutoCompleteView。我已经将ViewModel中的observable集合绑定到autocompleteview xaml的Suggestions属性 根据我的调试代码(即,只是一个将查询返回的内容写入调试输出的循环),我的查询返回的是项,因此我认为问题在于只显示所述项 下面是Xaml和ViewModel的代码(Store类有一个StoreName属性/字段) XAML <ContentPage.Res
<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>
<controls:AutoCompleteView
x:Name="MyAutoComplete"
SuggestionItemDataTemplate="{StaticResource SuggestionItemTemplate}"
Placeholder="Type Product Here"
ShowSearchButton="True"
SearchBackgroundColor="White"
SearchTextColor = "Black"
SearchBorderColor = "Yellow"
SuggestionBackgroundColor="White"
SearchCommand="{Binding SearchCommand}"
Suggestions="{Binding Items, Mode=TwoWay}"
SelectedItem ="{Binding SelectedItem}"
SelectedCommand = "{Binding CellSelectedCommand}"/>
视图模型
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);
}
}
class CreateSaleViewModel
{
//查询变量
公共字符串存储查询{get;set;}
//查询建议
public observeCollection存储建议{get;private set;}
公共ICommand SearchCmd{get;set;}
公共CreateSaleViewModel()
{
SearchCmd=新命令(搜索);
}
专用异步无效搜索()
{
StoreSuggestions=wait App.AzureDataStore.SearchStoresSync(StoreQuery);
}
}
我刚刚遇到了同样的事情。我完全忽略了Search命令,并挂接到代码隐藏中的AutoCompleteView TextChanged事件,从而解决了这个问题
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>
<controls:AutoCompleteView
x:Name="MyAutoComplete"
SuggestionItemDataTemplate="{StaticResource SuggestionItemTemplate}"
Placeholder="Type Product Here"
ShowSearchButton="True"
SearchBackgroundColor="White"
SearchTextColor = "Black"
SearchBorderColor = "Yellow"
SuggestionBackgroundColor="White"
SearchCommand="{Binding SearchCommand}"
Suggestions="{Binding Items, Mode=TwoWay}"
SelectedItem ="{Binding SelectedItem}"
SelectedCommand = "{Binding CellSelectedCommand}"/>
查看模型搜索图标命令
什么都不做
public Command<string> SearchCommand
{
get
{
return _searchCommand ?? (_searchCommand = new Command<string>(
obj =>{},
obj => !string.IsNullOrEmpty(obj.ToString())));
}
}
public命令SearchCommand
{
得到
{
返回\u searchCommand???(\u searchCommand=new命令(
obj=>{},
obj=>!string.IsNullOrEmpty(obj.ToString());
}
}
ViewModel LoadProducts
在网络调用后更新ObservableCollection
public async Task LoadProducts(string term)
{
var items = await service.GetProducts(term);
Items=new ObservableCollection<string>(items);
OnPropertyChanged("Items");
}
公共异步任务LoadProducts(字符串术语)
{
var项目=等待服务。获取产品(期限);
项目=新的可观测集合(项目);
不动产变更(“项目”);
}
您找到解决方案了吗?我也在寻找解决办法。thanksI刚刚认识到这里缺少Suggestions=“{Binding StoreSuggestions}”/>Mode=Twoway。应该是这样的,Suggestions=“{Binding StoreSuggestions,Mode=TwoWay}”还没有尝试Michael Davis的答案。我们的团队决定使用一种不同的技术,而不是xamarin.forms。您知道为什么SearchCommand不会触发而不是textchange事件吗?这是干什么的?