手势识别阻止WP8.1中的文本框

时间:2015-09-18 08:57:17

标签: c# xaml windows-phone-8 uigesturerecognizer

我正在为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处理程序。当我点击RootGridTextBox时会调用此方法,但是当我点击按钮时则不会。 object sender始终为Windows.UI.Xaml.Controls.Grid,因此无法确定TextBox是否为<img>

最有趣的是,相同代码的工作方式与Windows应用程序的预期相同,但不适用于Windows Phone 8.1应用程序。

你能否给我任何建议如何在不影响内部控制的情况下实现手势识别?

1 个答案:

答案 0 :(得分:0)

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

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

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