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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    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">

            <Button Name="btnAddSettings" Content="注册 SettingsPane 的 CommandsRequested 事件" Click="btnAddSettings_Click_1" />

            <Button Name="btnShowSettings" Content="打开 SettingsPane" Click="btnShowSettings_Click_1" Margin="0 10 0 0" />

            <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap" Margin="0 10 0 0" />

        </StackPanel>
    </Grid>
</Page>
Contracts/SettingsContract/Demo.xaml.cs
/*
 * Settings Contract - 右侧边栏称之为 Charm,其中的“设置”称之为 Settings Contract
 *
 * SettingsPane - 设置面板
 *     GetForCurrentView() - 获取当前的 SettingsPane
 *     Show() - 打开 SettingsPane
 *     CommandsRequested - 每次打开 SettingsPane 时所触发的事件(两个类型为 SettingsPane 和 SettingsPaneCommandsRequestedEventArgs 的参数)
 *
 * UICommandInvokedHandler - 单击设置面板中的设置项时引发的事件的回调函数,是一个委托(一个类型为 IUICommand 的参数,SettingsCommand 实现了此接口)
 *
 * SettingsCommand - 设置面板中的设置项
 *     Id - 设置项的 ID,标识用
 *     Label - 设置项的名称,显示用
 *     Invoked - 指定单机设置项后,引发的事件的处理程序
 *
 * SettingsPaneCommandsRequestedEventArgs - CommandsRequested 事件中的事件参数
 *     Request - 返回 SettingsPaneCommandsRequest 类型的数据
 *
 * SettingsPaneCommandsRequest - 包含了 CommandsRequested 事件中的可用属性
 *     ApplicationCommands - SettingsCommand 集合
 */

using System;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Contracts.SettingsContract
{
    public sealed partial class Demo : Page
    {
        // 弹出自定义的详细设置页时,所用到的弹出框
        private Popup _settingsPopup = new Popup();
        // 是否注册了 SettingsPane 的 CommandsRequested 事件
        private bool _commandsRequestedRegistered = false;

        public Demo()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // 离开此页则去掉 CommandsRequested 监听
            if (this._commandsRequestedRegistered)
            {
                SettingsPane.GetForCurrentView().CommandsRequested -= onCommandsRequested;
                _commandsRequestedRegistered = false;
            }
        }

        // 添加设置项,即初始化自定义的设置项
        private void btnAddSettings_Click_1(object sender, RoutedEventArgs e)
        {
            if (!_commandsRequestedRegistered)
            {
                // 注册 SettingsPane 的 CommandsRequested 事件
                SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;
                _commandsRequestedRegistered = true;
            }
            else
            {
                lblMsg.Text += "已经为 SettingsPane 注册了 CommandsRequested 事件";
                lblMsg.Text += Environment.NewLine;
            }
        }

        // 显示 SettingsPane
        private void btnShowSettings_Click_1(object sender, RoutedEventArgs e)
        {
            SettingsPane.Show();
        }

        void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs)
        {
            // 初始化 SettingsPane 中的设置项
            UICommandInvokedHandler handler = new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand aboutCommand = new SettingsCommand("about", "关于", handler);
            eventArgs.Request.ApplicationCommands.Add(aboutCommand);

            SettingsCommand contactCommand = new SettingsCommand("contactUs", "联系我们", handler);
            eventArgs.Request.ApplicationCommands.Add(contactCommand);

            SettingsCommand flyoutCommand = new SettingsCommand("flyout", "弹出一个类“设置”风格的详细设置页", handler);
            eventArgs.Request.ApplicationCommands.Add(flyoutCommand);
        }

        // 响应 SettingsPane 中的各个自定义设置项的命令
        void onSettingsCommand(IUICommand command)
        {
            SettingsCommand settingsCommand = (SettingsCommand)command;
            lblMsg.Text += string.Format("commandId:{0} - label:{1}", settingsCommand.Id.ToString(), settingsCommand.Label);
            lblMsg.Text += Environment.NewLine;

            // 通过 SettingsCommand.Id 来判断用户单机了哪个设置项
            if (settingsCommand.Id.ToString() == "flyout")
            {
                // 详细设置页的宽度
                double settingsPageWidth = 600;

                // 设置用于携带详细设置页的 Popup 的基本属性
                _settingsPopup.IsLightDismissEnabled = true;
                _settingsPopup.Width = settingsPageWidth;
                _settingsPopup.Height = Window.Current.Bounds.Height;

                // 为弹出框增加 PaneThemeTransition 的效果
                _settingsPopup.ChildTransitions = new TransitionCollection();
                _settingsPopup.ChildTransitions.Add(new PaneThemeTransition()
                {
                    Edge = EdgeTransitionLocation.Right
                });

                // 实例化自定义的详细设置页,并将其放到 Popup 内
                CustomSettingsPage mySettingsPage = new CustomSettingsPage();
                mySettingsPage.Width = settingsPageWidth;
                mySettingsPage.Height = Window.Current.Bounds.Height;
                _settingsPopup.Child = mySettingsPage;

                // 指定 Popup 的显示位置
                _settingsPopup.HorizontalOffset = Window.Current.Bounds.Width - settingsPageWidth;
                _settingsPopup.VerticalOffset = 0;
                _settingsPopup.IsOpen = true;
            }
        }
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索windows
, 事件
, using
settings
,以便于您获取更多的相关知识。

时间: 2024-08-02 20:39:32

Windows 8 Store Apps学习(37) 契约: Settings 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学习(38) 契约: Search Contract

介绍 重新想象 Windows 8 Store Apps 之 契约 Search Contract - 右侧边栏称之为 Charm, 其 中的"搜索"称之为 Search Contract 使用 Search Contract 的搜索建议,数据源在本地,以及从输 入法编辑器中获取相关信息 使用 Search Contract 的搜索建议,数据源在服务端,以及为搜索建议增 加图标.描述等 使用 Search Contract 的基于本地文件的搜索建议,数据来源于文件的 metadata

重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

原文:重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 契约 Settings Contract - 右侧边栏称之为 Charm,其中的"设置"称之为 Settings Contract 示例演示 Settings Cont

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学习(24) Application Data和Package中的文件操作

文件系统: Application Data 中的文件操作, Package 中的文件操作 介绍 重新想象 Windows 8 Store Apps 之 文件系统 Application Data(应用程序数据存储) 中的文件操作 Application Data(应用程序数据存储) 中的"设置"操作 通过 uri 引用 Application Data(应用程序数据存储) 中的媒体(图片.视频或音频) 访问 Package 中的文件 访问可移动存储 示例 1.演示如何在 Applic

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/