How do you actually implement the Search Contract? (C#)

ぐ巨炮叔叔 提交于 2019-12-10 11:49:10

问题


I'm having some trouble understanding and getting the search contract to work in my Store app. I have been unable to find any sort of documentation or guide that explains the structure of using the contract. (I've looked at the quickstarts on MSDN, the Search Contract sample and the build video, but that only really deals with javascript)

So far I've been able to run a query and get a list (of Custom Objects) into my search contract page, and from there I try to assign that to defaultviewmodel.results, but no matter what query I type nothing shows up I on the page. Is there something else I need to set? What I have so far is as follows (excerpts):

App.xaml.cs

protected override void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)
        {
            SearchCharmResultsPage.Activate(args.QueryText, args.PreviousExecutionState);
            SearchCharmResultsPage.ProcessSearchQuery(args.QueryText);
        }    


public async static void ProcessSearchQuery(string queryString)
        {
            try
            {
                StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("recipeCustomObject                  Debug.WriteLine("Database exists, connecting");
                SQLiteAsyncConnection connection = new SQLiteAsyncConnection("CustomObject_db");
                List<CustomObject> resultsList = new List<CustomObject>();
                string query = "SELECT * FROM CustomObjectDB";
                resultsList =  await connection.QueryAsync<RecipeRecord>(query);  
            }
            catch (FileNotFoundException fnfExc)
            {
                Debug.WriteLine("FNFEXC: " + fnfExc.ToString());
            }
        }

I think it is possible that here lies the problem, though I'm not sure if it is, or how to change it. the resultsList list is created here, but because the method it asynchronous, I can't return from the method. Because of this I'm guess that when I try to assign this.DefaultViewModel[Results] = resultsList; in the LoadStateMethod, the object doesn't exist (thought the program throws no error). When I try to add the same line in the ProcessSearchQuery method, i'm told that this is not valid in a static method, but I think I need the method to be static? My problem might just be a fundamental logic error?


回答1:


Finally got it! found the solution here: http://jeffblankenburg.com/2012/11/06/31-days-of-windows-8-day-6-search-contract

For those looking for an answer in the future, the key is to make sure you have your search logic within the Filter_SelectionChanged method, which was something I wasn't doing. Look at the guide within the above link to get an idea of the structure.




回答2:


Have you looked at the Search contract sample on the developer center? There's a C#/XAML version there as well.




回答3:


My open source Win8 RSS Reader framework implements Search (and Share) have a look at the source and if you still got questions, I'll be happy to help http://win8rssreader.codeplex.com/



来源:https://stackoverflow.com/questions/13195477/how-do-you-actually-implement-the-search-contract-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!