SearchTask是Windows Phone 眾多任務中一個非常重要的任務,主要執行搜尋任務,在其內部是調用Bing搜尋引擎的相關服務,SearchTask簡單便於使用,下面是SearchTask的類圖:
SearchQuery:用於設定查詢搜尋索引鍵
Show:調用搜尋服務,顯示搜尋結果
在開發Windows Phone Application的時候,如果需要搜尋服務,即可使用SearchTask。下面舉例說明,採用MVVM模式。
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();
}
}
運行應用程式:
在文字框中輸入:windows phone:
單擊Search:
以上就是SearchTask的使用示範。原始碼:http://files.cnblogs.com/PerfectSoft/SearchTask.zip