我正在通过Windows Universal应用程序的手势交互实现控件。但是我发现了一个问题,即如果我为容器定义手势设置,则父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应用程序。
您能给我什么建议,如何在不影响内部控件的情况下实现手势识别吗?
最佳答案
我没有找到比为PointerPressed
控件添加TextBox
事件处理程序更好的解决方案:
private void TextBox_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
e.Handled = true;
}
它可以防止为
OnPointerPressed
调用this.Container
,并允许以典型方式使用TextBox
。不是最好的解决方案,但对我来说效果很好。关于c# - WP8.1中的手势识别块文本框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32647871/