如何配置MAUI Blazor混合应用仅支持单指点击操作?
Windows触摸屏应用仅支持单指点击、禁用手势的代码实现
针对触摸屏Windows应用需仅保留单指点击、禁用缩放/返回等多指手势的需求,以下是不同主流框架的代码实现方案,仅对当前应用生效,不会影响系统全局触控功能:
WPF 实现
- 开启窗口的触摸操作支持,在窗口构造函数中设置
ManipulationMode:
public MainWindow() { InitializeComponent(); this.ManipulationMode = ManipulationModes.All; }
- 重写窗口的
OnManipulationStarting方法,过滤多触点操作:
protected override void OnManipulationStarting(ManipulationStartingEventArgs e) { // 仅允许单指触发操作,取消多触点请求 if (e.Manipulators.Count != 1) { e.Cancel(); } base.OnManipulationStarting(e); }
WinForms 实现
通过拦截Windows消息WM_TOUCH,过滤多触点输入:
protected override void WndProc(ref Message m) { const int WM_TOUCH = 0x0240; if (m.Msg == WM_TOUCH) { // 解析触点数量 int touchCount = m.WParam.ToInt32() & 0xFFFF; if (touchCount > 1) { // 忽略多触点消息,阻止后续手势触发 return; } } base.WndProc(ref m); }
UWP/WinUI 3 实现
- 在XAML页面中开启触摸操作并绑定事件:
<Page x:Class="YourApp.MainPage" ManipulationMode="All" ManipulationStarting="Page_ManipulationStarting"> <!-- 页面内容 --> </Page>
- 在后台代码中处理
ManipulationStarting事件,取消多触点操作:
private void Page_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e) { if (e.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch && e.Contacts.Count > 1) { e.Cancel = true; } }
核心逻辑:通过监听触摸操作的起始事件或系统消息,判断触点数量,当触点数大于1时直接取消操作,从而仅保留单指点击功能,同时不会影响系统其他触控设置。
内容的提问来源于stack exchange,提问作者Byorsh19
相关产品推荐
相关产品推荐

