手势识别块文本框在WP8.1

本文关键字:WP8 文本 手势识别 | 更新日期: 2023-09-27 18:13:13

我正在为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处理器。当我点击RootGridTextBox时调用此方法,但当我点击按钮时不调用。object sender总是Windows.UI.Xaml.Controls.Grid,所以我不能确定它是否是TextBox

最有趣的是,同样的代码在Windows应用程序中工作,但在Windows Phone 8.1应用程序中不起作用。

你能给我任何建议如何实现手势识别而不影响控制里面?

手势识别块文本框在WP8.1

我还没有找到比为TextBox控件添加PointerPressed事件处理程序更好的解决方案:

private void TextBox_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
    e.Handled = true;
}

它阻止this.Container调用OnPointerPressed,并允许以典型的方式使用TextBox。这不是最好的解决方案,但对我来说效果很好。