【问题标题】:Populating suggestion list of xlabs Autocomplete view填充 xlabs 自动完成视图的建议列表
【发布时间】:2016-01-04 04:13:17
【问题描述】:

我在填充和显示 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);
        }
    }

【问题讨论】:

  • 您找到解决方案了吗?我也在寻找解决方案。谢谢
  • 我刚刚认识到这里缺少 Suggestions="{Binding StoreSuggestions}" /> Mode=Twoway。应该是 Suggestions="{Binding StoreSuggestions, Mode=TwoWay}"
  • 还没试过迈克尔戴维斯的回答。我们的团队决定使用不同的技术来代替 xamarin.forms

标签: autocomplete xamarin xamarin.forms xlabs


【解决方案1】:

我刚刚遇到了同样的事情。我通过完全忽略搜索命令并在代码隐藏中挂钩 AutoCompleteView TextChanged 事件来解决它。

XAML

<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 AddStockItem()
{
    BindingContext = Model;
    InitializeComponent();
    MyAutoComplete.TextChanged += MyAutoComplete_TextChanged;
}
async void MyAutoComplete_TextChanged(object sender, TextChangedEventArgs e)
{
    await Model.LoadProducts(e.NewTextValue);
}

查看模型搜索 ICommand 什么都不做

public Command<string> SearchCommand
    {
        get
        {
            return _searchCommand ?? (_searchCommand = new Command<string>(
                obj =>{},
                obj => !string.IsNullOrEmpty(obj.ToString())));
        }
    }

ViewModel 加载产品 网络调用后更新 ObservableCollection。

public async Task LoadProducts(string term)
    {
        var items = await service.GetProducts(term);
        Items=new ObservableCollection<string>(items);
        OnPropertyChanged("Items");

    }

【讨论】:

  • 你知道为什么 SearchCommand 没有触发而不是 textchange 事件吗?这是为了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 2010-12-22
  • 1970-01-01
  • 2017-10-02
  • 2014-11-26
  • 1970-01-01
  • 2011-10-03
相关资源
最近更新 更多