c# - WP8.1 中的手势识别 block 文本框

标签 c# xaml windows-phone-8 uigesturerecognizer

我正在为 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 应用程序。

你能给我一些建议,如何在不影响内部控件的情况下实现手势识别吗?

最佳答案

我还没有找到比为 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/

相关文章:

c# - 构建重定向查询字符串

c# - 使用 jQuery 进行 asp.net mvc 分页有哪些可用选项?

c# - 仅存储键的最佳查找数据结构(没有值的字典)

c# - 如何在不丢失原始颜色轨迹的情况下使背景颜色动画为新颜色并返回?

c# - 添加货币符号到 `TextBlock`

c# - 在 Visual C# 中,如何禁用 WiX 项目? (使构建太慢)

c# - Window 中的 ViewModel 和 UserControl 中的 ViewModel 之间的 WPF 数据绑定(bind)

xaml - FlowDocument XAML 是否有 XSD?

c# - 在 C++ 中为 Windows Phone 8 使用 C# dll

javascript - 放大时平滑自动滚动到底部 JavaScript