UWP开发砸手机系列(二)—— “讲述人”识别自定义控件Command

原文:UWP开发砸手机系列(二)—— “讲述人”识别自定义控件Command

  上一篇我们提到如何让“讲述人”读出自定义的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[0].GetValue(EventTriggerBehavior.ActionsProperty) as ActionCollection;
            var command = actions[0].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,逼的俺自由发挥啊。特地写了这篇造福全人类,各位不要吝啬点个推荐哦,虽然“讲述人”并没有什么卵用……

 

 

 

 

 

 

 

 

时间: 2024-08-03 13:32:32

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

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

原文:UWP开发砸手机系列(一)-- Accessibility 因为今天讨论的内容不属于入门系列,所以我把标题都改了.这个啥Accessibility说实话属于及其蛋疼的内容,即如何让视力有障碍的人也能通过声音来使用触屏手机--也许你这辈子也不会接触,但如果有一天你遇到了,碰巧你又看了我这一篇,你就可以挺起胸膛大声说:这个逼我装定了! 首先我们来看下Accessibility在Windows 10 Mobile上原生支持的情况,点击"设置"->"轻松使用"-

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

原文:UWP开发入门(十二)--神器Live Visual Tree 很久以前,我们就有Snoop这样的工具实时修改.查看正在运行的WPF程序,那时候调个样式,修改个模板,相当滋润.随着历史的车轮陷进WP的泥潭中,无论WP7的Silverlight还是WP8.1的runtime,偶们都不能方便快捷的查看APP的可视化树(Visual Tree)了,呜呼哉,是可忍孰不可忍放下筷子就骂微软.没想到Visual Studio 2015倒是给了我们一个惊喜,自带了一套非常强大的调试工具Live Visu

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

原文:Win10 UWP开发:摄像头扫描二维码/一维码功能 这个示例演示整合了Aran和微软的示例,无需修改即可运行. 支持识别,二维码/一维码,需要在包清单管理器勾选摄像头权限. 首先右键项目引用,打开Nuget包管理器搜索安装:ZXing.Net.Mobile BarcodePage.xmal页面代码 <Page x:Class="SuperTools.Views.BarcodePage" xmlns="http://schemas.microsoft.com/wi

【Qt编程】基于Qt的词典开发系列&amp;lt;十二&amp;gt;调用讲述人

       我们知道,win7系统自带有讲述人,即可以机器读出当前内容,具体可以将电脑锁定,然后点击左下角的按钮即可.之前在用Matlab写扫雷游戏的时候,也曾经调用过讲述人来进行游戏的语音提示.具体的Matlab脚本文件如下: sp=actxserver('SAPI.SpVoice');sp.Speak('你好,欢迎来到西安电子科技大学!Hello,Welcome to XD University!')        Qt调用讲述人,需要使用专门的类,具体可以参考http://lynxlin

Win10 UWP 开发系列:使用多语言工具包让应用支持多语言

原文:Win10 UWP 开发系列:使用多语言工具包让应用支持多语言 之前我在一篇blog中写过如何使用多语言工具包,见http://www.cnblogs.com/yanxiaodi/p/3800767.html 在WinEcos社区也发布过一篇详细的文章介绍多语言工具包的使用,但因社区改版那篇文章已经找不到了. 当时写的时候还没有出Win10的SDK,都是基于UAP框架写的.微软早已经发布了Win10的SDK,相应的项目结构也发生了变化,以前分为两个项目通过Share项目共享代码的方式被抛弃

Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

原文:Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App 安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneGap.ionic.AngularJS这些框架或库的关系,我个人理解是这样,PhoneGap是一个商业项目,用来实现HTML5式的跨平台开发,后来Adobe公司将其中的核心代码开源,就是Co

Win10 UWP 开发系列:使用SplitView实现汉堡菜单及页面内导航

原文:Win10 UWP 开发系列:使用SplitView实现汉堡菜单及页面内导航 在Win10之前,WP平台的App主要有枢轴和全景两种导航模式,我个人更喜欢Pivot即枢轴模式,可以左右切换,非常方便.全景视图因为对设计要求比较高,自己总是做不出好的效果.对于一般的新闻阅读类App来说,Pivot更适合多个频道的展示,因为内容基本都是一样的. 到了Win10,微软模仿其他平台也推出了汉堡菜单,但并没有提供现成的控件,而是需要开发者通过一个名为SplitView的控件来实现.我个人并不觉得左上

Win10 UWP 开发系列:使用SQLite

原文:Win10 UWP 开发系列:使用SQLite 在App开发过程中,肯定需要有一些数据要存储在本地,简单的配置可以序列化后存成文件,比如LocalSettings的方式,或保存在独立存储中.但如果数据多的话,还是需要本地数据库的支持.在UWP开发中,可以使用SQLite.本篇文章说一下如何在UWP中使用SQLite.因为SQLite是跨平台的,版本众多,我刚开始用的时候不知道要装哪个,什么WP8的.WP8.1的.Win RT的--简直摸不着头脑.希望这篇文章能让大家少走点弯路. 其实这篇文

Win 10 UWP开发系列:设置AppBarButton的图标

原文:Win 10 UWP开发系列:设置AppBarButton的图标 在WP8以前,页面最下面的四个小圆按钮是不支持绑定的,WP8.1 RT之后,系统按钮升级成了AppBarButton,并且支持绑定了.在Win10 UWP开发中,按钮的样式发生了变化,外面的圆圈没有了.不过个人还是更喜欢之前的圆按钮的样子^_^ 很喜欢Metro Studio这个程序,有数百个好看的按钮可以用,并且可以导出成png图片.但现在不建议采取png图片的方式了,主要是因为png在缩放后有可能会失真,最好使用字体.p