Windows 8 Store Apps学习(38) 契约: Search Contract

介绍

重新想象 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 的搜索建议,数据源在本地。同时演示如何从输入法编辑 器中获取相关信息

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>

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索搜索
, 输入
, 激活
, search
, 输入框历史记录获取
, 建议
, search框
面板
,以便于您获取更多的相关知识。

时间: 2024-08-22 14:44:07

Windows 8 Store Apps学习(38) 契约: Search Contract的相关文章

Windows 8 Store Apps学习(39) 契约: Share Contract

介绍 重新想象 Windows 8 Store Apps 之 契约 Share Contract - 右侧边栏称之为 Charm,其 中的"共享"称之为 Share Contract 示例 1.演示如何开发共享源 Contracts/ShareContract/ShareSource.xaml <Page x:Class="XamlDemo.Contracts.ShareContract.ShareSource" xmlns="http://sche

Windows 8 Store Apps学习(37) 契约: Settings Contract

介绍 重新想象 Windows 8 Store Apps 之 契约 Settings Contract - 右侧边栏称之为 Charm, 其中的"设置"称之为 Settings Contract 示例 演示 Settings Contract 的应用 Contracts/SettingsContract/Demo.xaml <Page x:Class="XamlDemo.Contracts.SettingsContract.Demo" xmlns="

重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

原文:重新想象 Windows 8 Store Apps (38) - 契约: Search Contract [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 契约 Search Contract - 右侧边栏称之为 Charm, 其中的"搜索"称之为 Search Contract 使用 Search Contract 的搜索建议,

Windows 8 Store Apps学习(41) 打印

介绍 重新想象 Windows 8 Store Apps 之 打印 示例 1.需要打印的文档 Print/PrintPage.xaml <Page x:Class="XamlDemo.Print.PrintPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam

Windows 8 Store Apps学习(21) 动画: ThemeTransition(过渡效果)

介绍 重新想象 Windows 8 Store Apps 之 动画 ThemeTransition 的概述 EntranceThemeTransition - 页面间跳转时的过渡效果 ContentThemeTransition - 内容改变时的过渡效果 RepositionThemeTransition - 位置改变时的过渡效果 PopupThemeTransition - 弹出时的过渡效果 AddDeleteThemeTransition - 添加项或删除项时的过渡效果 ReorderThe

Windows 8 Store Apps学习(71)

作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之 其它 C# 中调用 Windows Runtime Component(C++) 让 Windows Runtime Component(C++) 作为代理以调用 DLL(C++) 通过 C++ 和 D3D 获取屏幕分辨率 示例 一.演示如何在 C# 中调用 Windows Runtime Component(C++),以及 Windows Runtime Component(C++) 如何作为代理调用 DLL(

Windows 8 Store Apps学习70) 其它: 文件压缩和解压缩

重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期 作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之 其它 文件压缩和解压缩 与 Windows 商店相关的操作 app 与 web 几个 Core 的应用 页面的生命周期和程序的生命周期 示例 1.演示如何压缩和解压缩文件 Feature/Compress

Windows 8 Store Apps学习(69) 其它: 自定义启动屏幕,

重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之 其它 自定义启动屏幕 检查当前呈现的应用程序是运行在本地还是运行在远程桌面或模拟器 保持屏幕的点亮状态 MessageDialog - 信息对话框 PopupMenu - 上下文菜单 示例 1.演示如何自定义启动屏幕 Feature/

Windows 8 Store Apps学习(68) 后台任务:控制通道(ControlChannel)

介绍 重新想象 Windows 8 Store Apps 之 后台任务 控制通道(ControlChannel) 示例 1.客户端与服务端做 ControlChannel 通信的关键代码 ControlChannelHelper/AppContext.cs /* * 本例通过全局静态变量来实现 app 与 task 的信息共享,以便后台任务可以获取到 app 中的相关信息 * * 注: * 也可以通过 Windows.ApplicationModel.Core.CoreApplication.P