Windows Phone 之 SearchTask

來源:互聯網
上載者:User

  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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.