win10 uwp 在笔迹开始书写拿到书写移动事件

2019-06-15 14:51:06 浏览数 (1)

在使用 InkCanvas 的过程,无法直接通过 Pointer 消息拿到书写移动,需要使用 StrokeInput 才能获取到

在 InkCanvas.InkPresenter.StrokeInput 提供了 StrokeStarted 、StrokeContinued 、StrokeEnded 这些看名字就知道是干什么用的事件,通过这些事件可以在书写的过程拿到触摸事件或鼠标事件

使用 StrokeContinued 作为例子,先在 XAML 界面放一个 InkCanvas 控件,同时修改他的属性名是 InkCanvas 在后台代码可以这样写

代码语言:javascript复制
            InkCanvas.InkPresenter.StrokeInput.StrokeContinued  = StrokeContinued;

拿到的StrokeContinued是PointerEventArgs可以拿到很多信息

代码语言:javascript复制
        public void StrokeContinued(InkStrokeInput sender, PointerEventArgs args)
        {
           
        }

在 Win10 的 Anniversary Update 1607 提供了 CoreWetStrokeUpdateSource 可以在湿笔迹的时候触发事件

在 CoreWetStrokeUpdateSource 同样提供了开始书写和书写过程中的事件

先通过 Create 方法创建 CoreWetStrokeUpdateSource 请看代码

代码语言:javascript复制
 var coreWetStrokeUpdateSource = CoreWetStrokeUpdateSource.Create(inkCanvas.InkPresenter);

这个事件有一点坑的是有加入的时机问题,请确保在所有的 InkCanvas 包括他的容器都 Loaded 完成之后才可以使用这个事件,不然是不会有触发的

通过 coreWetStrokeUpdateSource 可以拿到触摸按下和移动等事件

代码语言:javascript复制
coreWetStrokeUpdateSource.WetStrokeStarting  = CoreWetStrokeUpdateSource_WetStrokeStarting;
coreWetStrokeUpdateSource.WetStrokeContinuing  = CoreWetStrokeUpdateSource_WetStrokeContinuing;
coreWetStrokeUpdateSource.WetStrokeStopping  = CoreWetStrokeUpdateSource_WetStrokeStopping;
coreWetStrokeUpdateSource.WetStrokeCompleted  = CoreWetStrokeUpdateSource_WetStrokeCompleted;

我在这些事件里面做输出,这样可以知道在多指触摸下的事件是如何触发的

代码语言:javascript复制
        private void CoreWetStrokeUpdateSource_WetStrokeCompleted(CoreWetStrokeUpdateSource sender,
            CoreWetStrokeUpdateEventArgs args)
        {
            Debug.WriteLine($"CoreWetStrokeUpdateSource_WetStrokeCompleted{args.PointerId}");
        }

        private void CoreWetStrokeUpdateSource_WetStrokeStopping(CoreWetStrokeUpdateSource sender,
            CoreWetStrokeUpdateEventArgs args)
        {
            Debug.WriteLine($"WetStrokeStopping{args.PointerId}");
        }

        private void CoreWetStrokeUpdateSource_WetStrokeContinuing(CoreWetStrokeUpdateSource sender,
            CoreWetStrokeUpdateEventArgs args)
        {
            Debug.WriteLine($"CoreWetStrokeUpdateSource_WetStrokeContinuing {args.PointerId}");
        }

        private void CoreWetStrokeUpdateSource_WetStrokeStarting(CoreWetStrokeUpdateSource sender,
            CoreWetStrokeUpdateEventArgs args)
        {
            Debug.WriteLine($"CoreWetStrokeUpdateSource_WetStrokeStarting{args.PointerId}");
        }

运行代码可以看到下面的输出

代码语言:javascript复制
CoreWetStrokeUpdateSource_WetStrokeStarting 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
……
CoreWetStrokeUpdateSource_WetStrokeStarting 2
CoreWetStrokeUpdateSource_WetStrokeStarting 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 1
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
……

CoreWetStrokeUpdateSource_WetStrokeCompleted  1
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeCompleted2
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeContinuing 3
CoreWetStrokeUpdateSource_WetStrokeCompleted  3

可以从输出看到在多指触摸的时候,对每个手指都会触发一次完整的 Starting-Continuing-Completed 事件

那么 CoreWetStrokeUpdateSource 和 StrokeContinued 的区别在哪?就是在于触发的先后问题,在笔迹开始书写之前先触发 CoreWetStrokeUpdateSource 事件,然后在书写完成之后触发 StrokeContinued 事件,我同时输出 WetStrokeContinuing 和 StrokeContinued 事件,请看下面

代码语言:javascript复制
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
StrokeContinued
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
StrokeContinued
StrokeContinued
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
StrokeContinued
StrokeContinued
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
StrokeContinued
StrokeContinued
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
CoreWetStrokeUpdateSource_WetStrokeContinuing 9
StrokeContinued
StrokeContinued

可以看到 WetStrokeContinuing 的输出时机和频率都会比 StrokeContinued 更高

另外推荐开放的是 StrokeContinued 的事件,因为在使用 UWP 笔迹就是为了做高性能的笔,在 WetStrokeContinuing 的事件里面如果添加了业务代码,那么将会影响笔迹的书写速度

在 UWP 的笔迹书写过程,需要等待 WetStrokeContinuing 事件完成之后才能进行笔迹渲染,而调用 StrokeContinued 事件是在笔迹书写完成之后调用同时不对这个事件做任何的等待

在框架里面建议开放的是 StrokeContinued 触发的事件,减少有逗比开发者在事件里面写了不清真代码降低了笔迹的性能

0 人点赞