Windows8 Search Contract

//Search Contract 
        private static int SearchPaneMaxSuggestions = 5;
        private SearchPane searchPane = SearchPane.GetForCurrentView();

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            searchPane.SuggestionsRequested += new TypedEventHandler(OnSearchPaneSuggestionsRequested);
            searchPane.QuerySubmitted += new TypedEventHandler(OnSearchPaneQuerySubmitted);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            searchPane.SuggestionsRequested -= new TypedEventHandler(OnSearchPaneSuggestionsRequested);
            searchPane.QuerySubmitted -= new TypedEventHandler(OnSearchPaneQuerySubmitted);
        }

        void OnSearchPaneQuerySubmitted(SearchPane sender, SearchPaneQuerySubmittedEventArgs args)
        {
           string queryResult = args.QueryText;
            //do Something ...
        }

        private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
        {
                string queryText = e.QueryText;
                SearchPaneSuggestionsRequest request = e.Request;
                foreach (string suggestion in suggestionList)
                {
                    if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                    {
                        request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);

                        if (request.SearchSuggestionCollection.Size >= SearchPaneMaxSuggestions)
                            break;
                    }
                }
        }

//SearchPane Suggestion using Windows Local StorageFiles
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SetLocalContentSuggestions(true);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            SetLocalContentSuggestions(false);
        }

        private void SetLocalContentSuggestions(bool isLocal)
        {
            //存储确定应用程序是否提供基于本地文件的建议的设置以及定义用于定位和筛选建议的条件的设置
            LocalContentSuggestionSettings settings = new LocalContentSuggestionSettings();
            settings.Enabled = isLocal;
            if (settings.Enabled)
            {
                settings.Locations.Add(KnownFolders.MusicLibrary);
                //settings.AqsFilter = "kind:Music";
            }

            //指定基于本地文件的建议是否自动显示在搜索窗格,并定义 Windows 使用的条件来定位和筛选这些建议
            SearchPane.GetForCurrentView().SetLocalContentSuggestionSettings(settings);
        }



//using Keyboard open SearchPane

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;

            //显示SearchPane
            //SearchPane.GetForCurrentView().Show();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
        }

 

posted on 2013-03-27 11:46  JackSlaterYu  阅读(271)  评论(0编辑  收藏  举报