SearchTask is a very important task in many Windows Phone tasks. It mainly executes search tasks and internally calls services related to Bing search engine. SearchTask is simple and easy to use, the following is a class diagram of SearchTask:
SearchQuery: Used to set search keywords
Show: Call the search service to display the search results
When developing a Windows Phone Application, you can use SearchTask to search for services. The following example shows how to use the MVVM mode.
XMAL:
<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
x:Class="SearchTask.MainPage"
xmlns:viewModel="clr-namespace:SearchTask.ViewModel"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
<viewModel:MainPageView x:Key="ViewModel"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="My First Application" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Search Page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" DataContext="{StaticResource ViewModel}" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<TextBox x:Name="SearchBox" Text="{Binding SearchQuery, Mode=TwoWay}"/>
<Button x:Name="SearchBut" Content="Search" HorizontalAlignment="Left" VerticalAlignment="Top" Height="81" Margin="4,8,0,0" Grid.Row="1" Width="196" Background="#00C07777">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ec:CallMethodAction MethodName="Search" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
ViewModel:
public class MainPageView : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private string searchQuery;
public string SearchQuery
{
get
{
return searchQuery;
}
set
{
searchQuery = value;
PropertyChanged(this, new PropertyChangedEventArgs("SearchQuery"));
}
}
public void Search()
{
Microsoft.Phone.Tasks.SearchTask task = new Microsoft.Phone.Tasks.SearchTask();
task.SearchQuery = SearchQuery;
task.Show();
}
}
Run the application:
In the text box, enter windows phone:
Click Search:
The above is the use demonstration of SearchTask. Source code: http://files.cnblogs.com/PerfectSoft/SearchTask.zip