上一篇我们提到如何让“讲述人”读出自定义的CanReadGrid,但“讲述人”仍然无法识别CanReadGrid上绑定的Command。XAML代码如下:

    <StackPanel>
<TextBlock Text="{x:Bind Title,Mode=OneWay}" Foreground="White"></TextBlock>
<local:CanReadGrid Background="Red" AutomationProperties.Name="Can read gird" Height="100">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Tapped">
<Core:InvokeCommandAction Command="{x:Bind ChangeTitleCommand}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</local:CanReadGrid>
</StackPanel>

  我们可以看到通过Behaviors绑定了Command,在Tapped事件发生时触发ChangeTitleCommand。

  我们再来对比一下系统控件Button的写法:

        <Button Command="{x:Bind ChangeTitleCommand}">I am Button</Button>

  在“讲述人”模式下,点击上面这个Button按钮,“讲述人”除了会念出“I am Button Button.”这句话以外,还会补充一句“Double tap to activate.”这时双击Button将会触发ChangeTitleCommand。

  其中不一样的地方无非就是Button自带有名为“Command”的,类型为ICommand的依赖属性(Dependency Property):

public System.Windows.Input.ICommand Command { get; set; }
Member of Windows.UI.Xaml.Controls.Primitives.ButtonBase

  而我们自定义的CanReadGrid,则是通过附加属性(Attached Property)来获得绑定Command的能力。

  附件属性也是一种特殊的依赖属性,二者殊归同路。既然Button通过依赖属性可以做到的事情,附加属性一样可以完成。

  想要弄明白Button的Command是如何被调用的,最简单的办法就是去查看源码呗:

    public class ButtonAutomationPeer : ButtonBaseAutomationPeer, IInvokeProvider
    {
        /// <summary>Initializes a new instance of the <see cref="T:System.Windows.Automation.Peers.ButtonAutomationPeer" /> class.</summary>
        /// <param name="owner">The element associated with this automation peer.</param>
        public ButtonAutomationPeer(Button owner) : base(owner)
        {
        }         /// <summary>Gets the name of the control that is associated with this UI Automation peer.</summary>
        /// <returns>A string that contains "Button".</returns>
        protected override string GetClassNameCore()
        {
            return "Button";
        }         /// <summary>Gets the control type of the element that is associated with the UI Automation peer.</summary>
        /// <returns>
        ///   <see cref="F:System.Windows.Automation.Peers.AutomationControlType.Button" />.</returns>
        protected override AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.Button;
        }         /// <summary>Gets the object that supports the specified control pattern of the element that is associated with this automation peer.</summary>
        /// <returns>If <paramref name="patternInterface" /> is <see cref="F:System.Windows.Automation.Peers.PatternInterface.Invoke" />, this method returns a this pointer, otherwise this method returns null.</returns>
        /// <param name="patternInterface">A value in the enumeration.</param>
        public override object GetPattern(PatternInterface patternInterface)
        {
            if (patternInterface == PatternInterface.Invoke)
            {
                return this;
            }
            return base.GetPattern(patternInterface);
        }         /// <summary>This type or member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.</summary>
        void IInvokeProvider.Invoke()
        {
            if (!base.IsEnabled())
            {
                throw new ElementNotEnabledException();
            }
            base.Dispatcher.BeginInvoke(DispatcherPriority.Input, new DispatcherOperationCallback(delegate(object param)
            {
                ((Button)base.Owner).AutomationButtonBaseClick();
                return null;
            }), null);
        }
    }

  果不其然发现了上一篇我们提到的GetClassNameCore,GetAutomationControlTypeCore,GetPattern三个方法。另外还有一个奇怪的void IInvokeProvider.Invoke()。这货看名字也能猜出来是干啥的啦,这货竟然去调了Button类里的Click方法……

  知道真相的我眼泪流出来……搞啥呢,那我在这里调一下Command.Execute不就成了!

  首先我们给CanReadGrid添加ExecuteCommand方法,该方法通过DependencyObject的GetValue方法一层层拿到Command,然后执行Execute。

    public class CanReadGrid : Grid
{
protected override AutomationPeer OnCreateAutomationPeer()
{
return new GridAutomationPeer((this));
} public void ExecuteCommand()
{
var behaviors = Interaction.GetBehaviors(this);
var actions = behaviors[].GetValue(EventTriggerBehavior.ActionsProperty) as ActionCollection;
var command = actions[].GetValue(InvokeCommandAction.CommandProperty) as ICommand;
command.Execute(null);
}
}

  第二步就是完善GridAutomatioPeer,这里需要注意的是IInvokeProvider这个接口,通过Button的源码推测具有Action的控件需要实现这个接口的Invoke方法来执行操作。我们也是在Invoke方法里来调用CanReadGrid类里的ExecuteCommand方法。

    public class GridAutomationPeer : FrameworkElementAutomationPeer, IInvokeProvider
{
public GridAutomationPeer(Grid owner)
: base(owner)
{ } public async void Invoke()
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
((CanReadGrid)base.Owner).ExecuteCommand();
});
} protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Invoke)
{
return this;
} return null;
}
}

  大功告成!开启“讲述人”模式来验证成果吧!

  结尾插播个广告,本篇内容100%原创,当时俺翻烂了Google的搜索页面、中英文各种blog也木有讲如何让“讲述人”调用Command,逼的俺自由发挥啊。特地写了这篇造福全人类,各位不要吝啬点个推荐哦,虽然“讲述人”并没有什么卵用……

UWP开发砸手机系列(二)—— “讲述人”识别自定义控件Command的更多相关文章

  1. UWP开发砸手机系列(一)—— Accessibility

    因为今天讨论的内容不属于入门系列,所以我把标题都改了.这个啥Accessibility说实话属于及其蛋疼的内容,即如何让视力有障碍的人也能通过声音来使用触屏手机……也许你这辈子也不会接触,但如果有一天 ...

  2. 使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form

    使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form 开发环境: Wing IDE 6.1 步骤1: 打开 Wing IDE,创建一个新的 pr ...

  3. MUI框架开发HTML5手机APP(二)--页面跳转传值&底部选项卡切换

      概 述 JRedu 在上一篇博客中,我们学习了如何使用Hbuilder创建一个APP,同时如何使用MUI搭建属于自己的第一款APP,没有学习的同学可以戳链接学习: http://www.cnblo ...

  4. [UWP开发]处理手机后退事件

    众所周知,uwp程序是一套代码,可以run在不同的平台上.但是不同的设备肯定有其独特之处,所以针对这些独特之处,必须用“独特的代码”来处理. 所以微软提供了一系列的拓展类库来实现这种特殊处理. 如上图 ...

  5. Win10 UWP开发:摄像头扫描二维码/一维码功能

    这个示例演示整合了Aran和微软的示例,无需修改即可运行. 支持识别,二维码/一维码,需要在包清单管理器勾选摄像头权限. 首先右键项目引用,打开Nuget包管理器搜索安装:ZXing.Net.Mobi ...

  6. UWP开发入门(十二)——神器Live Visual Tree

    很久以前,我们就有Snoop这样的工具实时修改.查看正在运行的WPF程序,那时候调个样式,修改个模板,相当滋润.随着历史的车轮陷进WP的泥潭中,无论WP7的Silverlight还是WP8.1的run ...

  7. 步步为营 SharePoint 开发学习笔记系列总结

    转:http://www.cnblogs.com/springyangwc/archive/2011/08/03/2126763.html 概要 为时20多天的sharepoint开发学习笔记系列终于 ...

  8. 使用 PySide2 开发 Maya 插件系列 总览

    使用 PySide2 开发 Maya 插件系列 总览 使用 PySide2 开发 Maya 插件系列一:QT Designer 设计GUI, pyside-uic 把 .ui 文件转为 .py 文件 ...

  9. 【Qt编程】基于Qt的词典开发系列<十二>调用讲述人

    我们知道,win7系统自带有讲述人,即可以机器读出当前内容,具体可以将电脑锁定,然后点击左下角的按钮即可.之前在用Matlab写扫雷游戏的时候,也曾经调用过讲述人来进行游戏的语音提示.具体的Matla ...

随机推荐

  1. 【转】C# 调用 C++ 数据转换

    原文:https://www.cnblogs.com/82767136/articles/2517457.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返 ...

  2. ios 7.1企业证书无线安装

    ios升级到7.1时,企业证书http服务器发布无线安装不能使用,需要使用https服务器. 测试了几种方式 1.测试时自签名https证书测试无效,提示不能连接到服务器 2.使用dropbox共享连 ...

  3. 使用matplotlib画双纵轴坐标

    一.前言 本文主要使用matplotlib,实现双纵轴坐标的图表绘制.笔者python版本为2.7.15. 二.实践及效果 1. 需求 某个有这么一个成绩表,分别是名字,本次成绩以及进步幅度,现在需要 ...

  4. 探究算子find_shape_model中参数MaxOverlap的准确意思

    基于形状的模板查找算子: find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxO ...

  5. Python深度学习之安装theano(windows)

    前方预警:windows的坑太多了,抛弃用linux吧 安装theano,提前清空自己的python环境吧,坑太多了,anaconda会自动安装path 一,首先安装python包管理anaconda ...

  6. java同一个类中,构造器如何调用另一个重载的构造器?

    构造器里面调用其它构造器,格式方法如下:1.使用this调用另一个重载构造器,只能在构造器中使用:2.必须写在构造器执行体的第一行语句: 示例如下: import static java.lang.S ...

  7. loadrunner12--学习中遇到疑问及解释

    1.analysis里面,平均事务响应时间,平均事务响应时间+运行vuser,两个图的数据有区别是什么原因? 答: 请仔细查看以下两张图,其实两张图的数据是没有区别的. 之所以我们认为他们二者的数据有 ...

  8. Java 设计模式系列(四)生成器模式

    Java 设计模式系列(四)生成器模式 生成器模式也称之为建造者模式.将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.demo 1. 生成器模式原理 1.1 生成器模式结构 ...

  9. MySQL redo log及recover过程浅析

    写在前面:作者水平有限,欢迎不吝赐教,一切以最新源码为准. InnoDB redo log 首先介绍下Innodb redo log是什么,为什么需要记录redo log,以及redo log的作用都 ...

  10. 一次清理Hbase的oldWALs的过程

    HBase版本:1.3.1 Hadoop版本:2.7.1 Zookeeper版本:3.4.6 --------------------------------- 检查线上业务系统的磁盘空间, 发现HD ...