c# - WPF Stylus 不触发事件,而 Touch 确实触发了手写笔事件
问题描述
我正在使用带有 Windows 10 和 Visual Studio 2019 的 Windows Surface。我在这里完全遵循本指南:
我遇到了一个问题,我无法将触控笔向下/向上/移动/等。使用 windows 表面笔时触发的事件,但我可以在使用触摸时触发。
我看过以下内容:
- 触摸事件未触发 C# WPF
- 手写笔在 Windowss 10 中没有为 InkCanvas 触发事件, 但这些似乎并没有为我修复它。
我已经在油漆和其他应用程序中使用表面笔进行了测试,以确保笔工作正常,是的,笔工作正常。我发现,使用笔时,我无法单击 WPF 应用程序上的关闭按钮或通过在标题栏上拖动来移动应用程序。
任何想法如何成功捕获手写笔向下/向上/移动/等。在 WPF 中使用表面笔时的事件?
编辑:在第一个链接中找到的最小可复制示例,如前所述,完全遵循。
MainWindow.xaml:
<Window x:Class="TestApp.MainWindow"
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"
xmlns:local="clr-namespace:TestApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:InkControl/>
</Grid>
</Window>
墨水控制.cs
using System;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Input.StylusPlugIns;
namespace TestApp
{
// Taken from https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/creating-an-ink-input-control?view=netframeworkdesktop-4.8#CollectingStylusPointDataAndCreatingInkStrokes
// A control for managing ink input
// A control for managing ink input
class InkControl : Label
{
InkPresenter ip;
DynamicRenderer dr;
// The StylusPointsCollection that gathers points
// before Stroke from is created.
StylusPointCollection stylusPoints = null;
public InkControl()
{
// Add an InkPresenter for drawing.
ip = new InkPresenter();
this.Content = ip;
// Add a dynamic renderer that
// draws ink as it "flows" from the stylus.
dr = new DynamicRenderer();
ip.AttachVisuals(dr.RootVisual, dr.DrawingAttributes);
this.StylusPlugIns.Add(dr);
}
static InkControl()
{
// Allow ink to be drawn only within the bounds of the control.
Type owner = typeof(InkControl);
ClipToBoundsProperty.OverrideMetadata(owner,
new FrameworkPropertyMetadata(true));
}
protected override void OnStylusDown(StylusDownEventArgs e)
{
// Capture the stylus so all stylus input is routed to this control.
Stylus.Capture(this);
// Allocate memory for the StylusPointsCollection and
// add the StylusPoints that have come in so far.
stylusPoints = new StylusPointCollection();
StylusPointCollection eventPoints =
e.GetStylusPoints(this, stylusPoints.Description);
stylusPoints.Add(eventPoints);
}
protected override void OnStylusMove(StylusEventArgs e)
{
if (stylusPoints == null)
{
return;
}
// Add the StylusPoints that have come in since the
// last call to OnStylusMove.
StylusPointCollection newStylusPoints =
e.GetStylusPoints(this, stylusPoints.Description);
stylusPoints.Add(newStylusPoints);
}
protected override void OnStylusUp(StylusEventArgs e)
{
if (stylusPoints == null)
{
return;
}
// Add the StylusPoints that have come in since the
// last call to OnStylusMove.
StylusPointCollection newStylusPoints =
e.GetStylusPoints(this, stylusPoints.Description);
stylusPoints.Add(newStylusPoints);
// Create a new stroke from all the StylusPoints since OnStylusDown.
Stroke stroke = new Stroke(stylusPoints);
// Add the new stroke to the Strokes collection of the InkPresenter.
ip.Strokes.Add(stroke);
// Clear the StylusPointsCollection.
stylusPoints = null;
// Release stylus capture.
Stylus.Capture(null);
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
// If a stylus generated this event, return.
if (e.StylusDevice != null)
{
return;
}
// Start collecting the points.
stylusPoints = new StylusPointCollection();
Point pt = e.GetPosition(this);
stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// If a stylus generated this event, return.
if (e.StylusDevice != null)
{
return;
}
// Don't collect points unless the left mouse button
// is down.
if (e.LeftButton == MouseButtonState.Released ||
stylusPoints == null)
{
return;
}
Point pt = e.GetPosition(this);
stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
// If a stylus generated this event, return.
if (e.StylusDevice != null)
{
return;
}
if (stylusPoints == null)
{
return;
}
Point pt = e.GetPosition(this);
stylusPoints.Add(new StylusPoint(pt.X, pt.Y));
// Create a stroke and add it to the InkPresenter.
Stroke stroke = new Stroke(stylusPoints);
stroke.DrawingAttributes = dr.DrawingAttributes;
ip.Strokes.Add(stroke);
stylusPoints = null;
}
}
}
这是一个使用 .NET Framework 4.7.2 的新 WPF 应用程序。
第二次编辑:
当我在 Visual Studio 之外独立运行测试应用程序时,我偶然发现了这一点。现在它可以很好地响应手写笔事件。现在的问题是:为什么 Visual Studio 2019 调试不允许手写笔事件,我该如何更改?
解决方案
推荐阅读
- javascript - 如何使用 Redux 动态添加输入?
- javascript - 如何在复选框和文本输入中使用“其他”选项
- python - 如何将具有重复索引的 DataFrame 转换为 MultiIndex DataFrame?
- r - 创建一个在其他列中查找值的新列
- sql - 将转换后的格式更改为 ISO 日期 - SQL Server
- javascript - 使用 javascript 创建带有图像标签的数组
- angular - 组件组成 - 验证问题
- asp.net-core - favicon.ico 503 文本/html
- c++ - 使用指针和向量时没有运算符匹配“*”错误
- docker - 为什么我必须定期使用 `docker network prune` 来重新启用容器内的 docker 映射 ip 连接?