简体   繁体   English

Windows Phone 8.1中的AutoSuggestBox出现奇怪的结果

[英]Strange results in AutoSuggestBox in Windows Phone 8.1

I am trying to use the standard AutoSuggestBox in a Windows Phone 8.1 XAML app, but it behaves really strangely. 我试图在Windows Phone 8.1 XAML应用程序中使用标准AutoSuggestBox ,但它的行为非常奇怪。

In a simple demo, I have collection 在一个简单的演示中,我有收集

Items = new ObservableCollection<string>
        {
            "a",
            "b",
            "c",
            "d"
        };

and he AutoSuggestBox in XAML: 和他在XAML中的AutoSuggestBox:

<AutoSuggestBox ItemsSource="{Binding Items}" />

The problem is that no matter what I write to the AutoSuggestBox , I always get all the items: 问题是,无论我写给AutoSuggestBox是什么,我总是得到所有项目:

在此输入图像描述

The documentation says next to nothing and I have not found any samples using this control. 文档说几乎没有,我没有找到任何使用此控件的示例。

Try the following code: 请尝试以下代码:

    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,
        AutoSuggestBoxTextChangedEventArgs args)
    {
            List<string> myList = new List<string>();
            foreach (string myString in PreviouslyDefinedStringArray)
            {
                if (myString.Contains(sender.Text) == true)
                {
                    myList.Add(myString);
                }
            }
            sender.ItemsSource = myList;
    }

This should work on WP 8.1 这应该适用于WP 8.1

Based on this blog post , it looks like what you're expecting (automatic filtering) isn't the case - instead, you need to hook into the TextChanged event and populate the Suggestions collection yourself. 基于这篇博文 ,它看起来像你期望的那样(自动过滤)并非如此 - 相反,你需要挂钩到TextChanged事件并自己填充Suggestions集合。

From the documentation : 文档

The app is notified when text has been changed by the user and is responsible for providing relevant suggestions for this control to display. 当用户更改文本时,应用程序会收到通知,并负责提供相关建议以显示此控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM