Silverlight入門:第五部分

來源:互聯網
上載者:User

在之前的章節中,我們最佳化了資料繫結,並將資料儲存在了我們的隔離儲存區 (Isolated Storage) 地區。現在讓我們整合一些其它的控制項使得使用者體驗更好一些。

自動完成輸入框

記得每次搜尋以後,我們都將搜尋條件儲存為曆史資料嗎?讓我們通過在他 們輸入時提供搜尋曆史來讓他們使用更方便。我們準備用Silverlight工具包中 的一個控制項——自動完成輸入框來做這件事。

要做到這點,我們需要添加一個到System.Windows.Controls.Input的程式集 引用,然後在你的Search.xaml檔案中添加一個xmlns:

1xmlns:input="clr- namespace:System.Windows.Controls;assembly=System.Windows.Controls.Inp ut"

我們還要在這裡將自動完成輸入框的Name屬性改為SearchTerm:

1 <input:AutoCompleteBox x:Name="SearchTerm"  FontSize="14.667" Margin="0,0,10,0" Width="275"
2      IsTextCompletionEnabled="True" />

現在我們需要為它提供資料,因此我們要在Helper.cs類中添加以下方法:

1   internal static string[] GetSearchTermHistory()
2   {
3       List<string> searchHistory =  new List<string>();
4
5       foreach (var item  in IsolatedStorageSettings.ApplicationSettings.Keys)
6        {
7           searchHistory.Add(item.ToString ());
8       }
9
10     return  searchHistory.ToArray();
11 }

之後在Search.xaml.cs中的Loaded事件處理器中,我添加了 Helper.GetSearchTermHistory()的調用:

1 void Search_Loaded(object sender, RoutedEventArgs e)
2 {
3     SearchResults.ItemsSource = pcv; // bind  the DataGrid
4     _timer.Start(); // start the  timer
5     SearchForTweetsEx(); // do the initial  search
6     SearchTerm.ItemsSource =  Helper.GetSearchTermHistory(); // populate autocomplete
7 }

這個效果就是當程式載入後,當使用者輸入搜尋條件時為他們提供提示:

很有用吧!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.