OpenSilver手机端Tapped事件未触发,求兼容点击触摸的事件方案
OpenSilver中TextBlock点击/触摸事件兼容方案
在OpenSilver开发场景下,Tapped事件在移动端普通浏览模式下存在触发失效的问题,推荐使用**PointerPressed事件**来统一处理PC端点击和移动端触摸操作,该事件能兼容鼠标点击、触摸屏幕等多种交互行为。
修改后的XML代码
<TextBlock Text="Se connecter" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="50" PointerPressed="TextBlock_PointerPressed" />
修改后的C#代码
private void TextBlock_PointerPressed(object sender, PointerRoutedEventArgs e) { MessageBox.Show("test"); }
额外优化建议
- 确保TextBlock有足够的交互区域:移动端触摸操作建议元素最小尺寸为48x48px,可通过添加
Padding属性扩大点击范围,比如:<TextBlock Text="Se connecter" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="50" PointerPressed="TextBlock_PointerPressed" Padding="10" />
内容的提问来源于stack exchange,提问作者Rayane Staszewski
相关产品推荐
相关产品推荐

