Quickly build Windows 8 style apps 13-searchcontract Build

Source: Internet
Author: User

Original: Build Windows 8 style app 13-searchcontract quickly

This blog post focuses on how to build searchcontract in your application, and the principles are clearly articulated in the blog "Quick Build Windows 8 style Application 12-searchcontract Overview and principles."

How to build searchcontract in your application

Here is a common example of building the search contract function, as follows:

set up App search Permissions:

Before we build searchcontract in our app, we need to confirm that the application has the appropriate search capabilities and can view the settings in the app manifest file.

Add standard search contract page:

Add the Search contract page to your app's solution.

View Search Contract page Default generated control template:

When you add a Searchresultpage.xaml page, you automatically build a control template for page display search information. For example:

The corresponding XAML designer display style:

Search Contract page corresponding CS file LoadState method to add the search data code:

The LoadState method of the SearchResultPage.xaml.cs file is searched in the app data based on the search text and added to the data collection. The code can resemble the following:

Change Search Contract the page needs to be bound to the data template:

Change the Standardsmallicon300x70itemtemplate data template for the Standardstyles.xaml file under the Common folder in the solution and set the field controls that we want to display in the data template. For example:

Run the app to see the search results:

Here we can use search contract for searching information and display the search results on the page that we added. Effect

The above demonstration is to search when the application is running, but the application can be launched and searched through search contract in the closed state.

External launch of the application:

In the App.xaml.cs file, you need to override the onsearchactivated method to receive the search activation events sent by the system. the onsearchactivated method triggers in two cases: click Search in the Search contract panel button and click on an app in the list.

onsearchactivated method to write code that the application searches for when it is launched externally. The code is as follows:

The code first determines whether the current application state is "notrunning", "Closedbyuser", "Terminated", if one of these three states, load data first, register suggestionrequested (search suggestions) events, Finally, activate the Searchresultpage page.

Note: You can customize the hint text in the Search Contract panel input box by setting the Placeholdertext property value of the Searchpane class Getforcurrentview () method. You can tell the user what to search for, and when the text is entered, the text will automatically disappear. The code can be as follows;

Implementation results:

Asynchronous Search Suggestions:

The last blog post mentions that in order to improve the user experience, developers should have to provide search suggestions when users enter search information. So how do you implement search suggestions in your app?

Register for search contract The panel's search suggestion event:

Search suggestions for matching related onsuggestionsrequested methods.

The appendquerysuggestion () method implementation in the Code provides search suggestions.

Note: You can add an array of information to your search suggestions by using appendquerysuggestion () , but the current search panel displays up to 5 search suggestions, This needs to be noticed by developers during the development process.

Achieve results

We can also use the appendsearchseparator () method to provide the necessary columns in search suggestions. The code is as follows:

Implementation results:

In addition, search contract provides a form of searching recommendations that allow users to jump directly to a detailed page of the corresponding information.

Implementing a search recommendation requires the use of the Searchsuggestioncollection class of appendresultsuggestion(string text, string Detailtext, string tag, irandomaccessstreamreference image, String imagealternatetext) method to implement. The code can be as follows:

The parameters in the appendresultsuggestion() method in the code are:

    1. Text and Detaitext are search suggestions to display the information;
    2. Tag is the only tag, is the user click resultsuggestion and trigger Reslutionsuggestionchosen event, the program received information, so that the page can display the corresponding results;
    3. Image is the search for the recommended images to be displayed;
    4. Imagealternatetext is an additional information for the image and is not displayed in the search panel;

Implementation results:

The main differences between the appendquerysuggestion () method and the appendresultsuggestion() method are:

    1. Appendquerysuggestion mainly tells users to search for information suggestions, appendresultsuggestion mainly tell the user may search out the results;
    2. Appendquerysuggestion contains only text information, Appendresultsuggestion contains image, title, Detailtitle and other information;
    3. Appendresultsuggestion allows users to navigate directly to a detailed page of a piece of information;

Local Search Suggestions :

Set the Searchpane localcontentsuggestionsettings, and cancel the onsuggestionsrequested event Listener, You can provide search suggestions for local files. Where the localcontentsuggestionsettings class contains the search scope, path, and file format. The code can be as follows:

localcontentsuggestionsettings class methods and properties can refer to Msdn:localcontentsuggestionsettings class.

Note: To search for a local file, you need to turn on read access to the data in the manifest file . For example:

Set Search History:

Long-term use of our app will result in a very verbose system history, and you can turn off history with the following code:

There is also a property about search history:searchhistorycontext.

The first thing we need to know is that an app can contain multiple sets of search histories, which can be really used by the Searchhistorycontext property.

Quickly build Windows 8 style apps 13-searchcontract Build

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.