Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Play with the Search Task in Windows Phone

DZone's Guide to

How To Play with the Search Task in Windows Phone

Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

In this short tutorial we are going to see how to use the Search task available in Windows Phone development. Search Task is one of the types of Launchers available with the Windows Phone SDK With Search Task we can provide a selected query to be searched over the web and provide the result with the web search application. We can use this task in our application development, letting the user search for some specified result or a query within the application or to search over the web. By using the available Launchers provided by Windows Phone, we can make a unique user experience to the end users. Let us see the demo on how to use the search task in our application step by step.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Search Task with a provided query to search the web.

image

Now let us add some controls to have a unique design, where we will add a button and a text box as shown in the screen below. Text box is used to enter the query which is required to be searched over the web on the button click event.

image

We are done with the UI design, now let us add code behind to achieve our requirement to handle the Search Task effectively. First let us add the assembly reference as a namespace (Microsoft.Phone.Task) in the using statement, which is required to make use of the search task as shown in the code below.

using Microsoft.Phone.Tasks;

Now we need to create an instance of the Search Launcher task and pass the query which the user inputs as shown in the code below. Basicaly we are trying to pass the value from the text box to the web browser using the search task which triggers the browser with the query specified by the end user as shown below.

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace F5debugHowto7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SearchTask st = new SearchTask();

            st.SearchQuery = textBox1.Text.ToString();
           
            st.Show();
        }
    }
}

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application opened as shown in the screens below.

image
Output:
image
image

So in this short tutorial, we have see how to use the search task effectively to search for some specified string or a query over the web with the standard way provided by the Windows Phone launchers to have unique experience across the applications and its usage. See you in the next tutorial and until then, Happy Programming!!!

 

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Published at DZone with permission of Karthikeyan Anbarasan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}