2015-01-21 45 views
0

我想在我的Windows Phone 8.1應用程序中使用某些功能,即我的用戶輸入一些字符,並且我建議他一些字。在Windows Phone 8.1中的自動完成文本框

public IEnumerable AutoCompletions = new List<string>() 
{ 
"Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "Nullam", "felis", "dui", "gravida", "at"}; 

例如用戶輸入「a」和我建議「阿梅特」,「在」和「adipiscing」,進一步的用戶輸入「AM」和我建議「阿梅特」。 請幫助我

+0

據我所知,它仍然不可用於'WP8.1'! – Kulasangar 2015-01-21 14:11:50

+0

真的嗎? 在市場上我的許多應用程序使用此功能 – 2015-01-21 14:16:15

+0

看也許你不明白 例如 https://i-msdn.sec.s-msft.com/dynimg/IC737492.png – 2015-01-21 14:16:57

回答

4

你想要做的是隻顯示適用於給定輸入的建議。並非所有可能的字符串

假設您有以下AutoSuggestBox:

<AutoSuggestBox 
    TextChanged="AutoSuggestBox_TextChanged" 
    SuggestionChosen="AutoSuggestBox_SuggestionChosen"> 
    <AutoSuggestBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding}" /> 
      </StackPanel> 
     </DataTemplate> 
    </AutoSuggestBox.ItemTemplate> 
</AutoSuggestBox> 

這是事件處理程序:

private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) 
    { 
     if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) 
     { 
      // You can set a threshold when to start looking for suggestions 
      if (sender.Text.Length > 3) 
      { 
       sender.ItemsSource = getSuggestions(sender.Text); 
      } 
      else { 
       sender.ItemsSource = new List<String> { }; 
      } 
     } 
    } 

所有你需要做的就是寫一個getSuggestions(String text)方法返回一個給定的合理建議輸入。

+0

也許幫助其他用戶 HTTP: //stackoverflow.com/questions/26637192/strange-results-in-autosuggestbox-in-windows-phone-8-1 – 2015-01-21 15:05:38