介紹
重新想象 Windows 8 Store Apps 之 契約
Search Contract - 右側邊欄稱之為 Charm, 其 中的“搜尋”稱之為 Search Contract
使用 Search Contract 的搜尋建議,資料來源在本地,以及從輸 入法編輯器中擷取相關資訊
使用 Search Contract 的搜尋建議,資料來源在服務端,以及為搜尋建議增 加表徵圖、描述等
使用 Search Contract 的基於本地檔案的搜尋建議,資料來源於檔案的 metadata
樣本
1、示範 Search Contract 的基本應用
Contracts/SearchContract/Demo.xaml
<Page x:Class="XamlDemo.Contracts.SearchContract.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Contracts.SearchContract" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" Text="直接通過鍵盤輸入即可啟用 SearchPane" /> <Button Name="btnShowSearch" Content="開啟 SearchPane" Click="btnShowSearch_Click_1" Margin="0 10 0 0" /> </StackPanel> </Grid></Page>
Contracts/SearchContract/Demo.xaml.cs
/* * 本例示範 Search Contract 的基本應用 * * Search Contract - 右側邊欄稱之為 Charm,其中的“搜尋”稱之為 Search Contract * * 1、在 Package.appxmanifest 中新增一個“搜尋”聲明 * 2、在 App.xaml.cs 中 override void OnSearchActivated(SearchActivatedEventArgs args),如果 app 是由搜尋啟用的,則可以在此擷取相關的搜尋資訊 * * SearchActivatedEventArgs - 當 app 由搜尋啟用時的事件參數 * QueryText - 搜尋文本 * PreviousExecutionState - 此 app 被搜尋啟用前的執行狀態(ApplicationExecutionState 枚舉:NotRunning, Running, Suspended, Terminated, ClosedByUser) * SplashScreen - 啟動畫面對象 * * SearchPane - 搜尋面板 * GetForCurrentView() - 擷取當前的 SearchPane * Show() - 顯示搜尋面板,需要的話可以指定初始查詢字串 * PlaceholderText - 當搜尋方塊沒有輸入焦點且使用者未輸入任何字元時,搜尋方塊中的提示文本 * SearchHistoryEnabled - 是否啟用搜尋建議的記錄功能,預設值是 true * SearchHistoryContext - 如果啟用了搜尋建議的記錄功能,則此值用於指定曆史紀錄的上下文,即記錄會在此上下文中儲存和擷取,也就是說一個 app 的搜尋建議記錄可以有多套 * ShowOnKeyboardInput - 如果發現鍵盤輸入,是否啟用搜尋面板,預設值是 false * Visible - 搜尋面板是否是開啟狀態,唯讀 * QueryChanged - 搜尋面板的搜尋方塊中的文本發生變化時所觸發的事件 * QuerySubmitted - 提交搜尋面板的搜尋方塊中的文本時所觸發的事件 * VisibilityChanged - 開啟或關閉搜尋面板時所觸發的事件 */ using System;using Windows.ApplicationModel.Activation;using Windows.ApplicationModel.Search;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation; namespace XamlDemo.Contracts.SearchContract{ public sealed partial class Demo : Page { private SearchPane _searchPane; public Demo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 擷取當前的 SearchPane,並註冊相關事件 _searchPane = SearchPane.GetForCurrentView(); _searchPane.QueryChanged += _searchPane_QueryChanged; _searchPane.QuerySubmitted += _searchPane_QuerySubmitted; // 當搜尋方塊沒有輸入焦點且使用者未輸入任何字元時,搜尋方塊中的提示文本 _searchPane.PlaceholderText = "請輸入"; // 是否啟用搜尋建議的記錄 _searchPane.SearchHistoryEnabled = true; // 指定搜尋建議的記錄的上下文 _searchPane.SearchHistoryContext = "abc"; // 如果有鍵盤輸入,則直接啟用 SearchPane _searchPane.ShowOnKeyboardInput = true; // 通過搜尋啟用應用程式時(參見 App.xaml.cs 中的 OnSearchActivated() 方法) SearchActivatedEventArgs searchActivated = (SearchActivatedEventArgs)e.Parameter; if (searchActivated != null) ShowSearchPane(searchActivated.QueryText); } protected override void OnNavigatedFrom(NavigationEventArgs e) { // 取消相關事件的監聽 _searchPane.QueryChanged -= _searchPane_QueryChanged; _searchPane.QuerySubmitted -= _searchPane_QuerySubmitted; _searchPane.ShowOnKeyboardInput = false; } private void btnShowSearch_Click_1(object sender, RoutedEventArgs e) { ShowSearchPane(); } // 顯示 Search 面板 private void ShowSearchPane(string queryText="") { _searchPane.Show(queryText); lblMsg.Text = queryText; } void _searchPane_QueryChanged(SearchPane sender, SearchPaneQueryChangedEventArgs args) { lblMsg.Text = args.QueryText; } void _searchPane_QuerySubmitted(SearchPane sender, SearchPaneQuerySubmittedEventArgs args) { lblMsg.Text = args.QueryText; } }}
App.xaml.cs
// 通過搜尋啟用應用程式時所調用的方法protected override void OnSearchActivated(SearchActivatedEventArgs args){ if (args.PreviousExecutionState == ApplicationExecutionState.Running) return; var rootFrame = new Frame(); rootFrame.Navigate(typeof(MainPage), args); Window.Current.Content = rootFrame; Window.Current.Activate();}
2、本例示範如何使用 Search Contract 的搜尋建議,資料來源在本地。同時示範如何從IME編輯 器中擷取相關資訊
Contracts/SearchContract/LocalSuggestion.xaml
<Page x:Class="XamlDemo.Contracts.SearchContract.LocalSuggestion" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Contracts.SearchContract" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" Text="本例示範如何使用 Search Contract 的搜尋建議,資料來源在本地。同時示範如何從輸入法中擷取相關資訊" /> </StackPanel> </Grid></Page>