我正在为 Windows 通用应用程序实现一个手势交互控件。但是我发现了一个问题,如果我为容器定义手势设置而不是父 TextBox
控件之后将无法点击。
这是一个简化的布局代码:
<Page x:Class="App.MainPage">
<Grid x:Name="RootGrid" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" />
<Button Grid.Row="1" Content="Click" />
</Grid>
</Page>
这是一个简化的代码,可以重现该行为:
public sealed partial class MainPage : Page
{
private GestureRecognizer _gr = new GestureRecognizer();
public FrameworkElement Container { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.Container = this.RootGrid;
this.Container.PointerCanceled += OnPointerCanceled;
this.Container.PointerPressed += OnPointerPressed;
this.Container.PointerMoved += OnPointerMoved;
this.Container.PointerReleased += OnPointerReleased;
_gr.CrossSlideHorizontally = true;
_gr.GestureSettings = GestureSettings.ManipulationTranslateRailsX;
}
private void OnPointerCanceled(object sender, PointerRoutedEventArgs e)
{
_gr.CompleteGesture();
e.Handled = true;
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_gr.ProcessDownEvent(e.GetCurrentPoint(null));
this.Container.CapturePointer(e.Pointer);
e.Handled = true;
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
_gr.ProcessMoveEvents(e.GetIntermediatePoints(null));
e.Handled = true;
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
_gr.ProcessUpEvent(e.GetCurrentPoint(null));
e.Handled = true;
}
}
Debuggig 告诉我这种行为的主要原因是 OnPointerPressed
处理程序。当我单击 RootGrid
和 TextBox
时会调用此方法,但当我单击按钮时不会调用此方法。 object sender
始终是 Windows.UI.Xaml.Controls.Grid
所以我无法确定它是否是 TextBox
。
最有趣的是,相同的代码可以按预期用于 Windows 应用程序,但不适用于 Windows Phone 8.1 应用程序。
你能给我一些建议,如何在不影响内部控件的情况下实现手势识别吗?
最佳答案
我还没有找到比为 TextBox
控件添加 PointerPressed
事件处理程序更好的解决方案:
private void TextBox_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
e.Handled = true;
}
它阻止为 this.Container
调用 OnPointerPressed
并允许以典型方式使用 TextBox
。不是最好的解决方案,但对我来说效果很好。
关于c# - WP8.1 中的手势识别 block 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647871/