当用户在具有触摸功能的屏幕上从ScrollViewer抬起手指时会发生什么事件

时间:2013-12-13 02:29:00

标签: c# xaml windows-runtime windows-store-apps winrt-xaml

我发现当我点击ScrollViewer时,PointerPressed和PointerExited事件会按预期触发。但是,如果我在触摸屏幕后向任何方向滚动并抬起我的手指,除了PointerCaptureLost之外没有任何事件会发生火灾,只要我滚动它就会过早地触发。

当我捕获指针ID并使用计时器轮询PointerPoint的状态时,即使在我滚动后抬起手指,IsInContact标志仍然为真。当我点击屏幕时,它按预期工作。

ManipulationCompleted与上面的效果相同,我不能使用ViewChanged事件,因为在我抬起手指之前会触发它。

这是一个错误还是我错过了什么?当用户将手指从屏幕上抬起时,我还能检测到另一种方法吗?这让我开始香蕉。

以下示例代码。您需要在触摸模式下使用模拟器或具有可触摸屏幕进行测试:

代码

using System;
using Windows.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace App1
{
    public sealed partial class MainPage : Page
    {

        private readonly DispatcherTimer pointerTimer = new DispatcherTimer();
        private uint? CurrentPointerID; //container for the current pointer id when user makes contact with the screeen

        public MainPage()
        {
            this.InitializeComponent();

            scrollviewer.PointerPressed += scrollviewer_PointerPressed;
            scrollviewer.PointerMoved += scrollviewer_PointerMoved;
            scrollviewer.PointerExited += scrollviewer_PointerExited;
            scrollviewer.PointerReleased += scrollviewer_PointerReleased;
            scrollviewer.PointerCaptureLost += scrollviewer_PointerCaptureLost;
            scrollviewer.PointerCanceled += scrollviewer_PointerCanceled;


            pointerTimer.Tick += pointerTimer_Tick;
            pointerTimer.Interval = TimeSpan.FromMilliseconds(300);
            pointerTimer.Start();


        }

        #region ScrollViewer Events

        void scrollviewer_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            EventCalledTextBlock.Text = "Pointer Moved";
        }

        void scrollviewer_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            EventCalledTextBlock.Text = "Pointer Exited";
        }

        void scrollviewer_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            CurrentPointerID = e.Pointer.PointerId;
            EventCalledTextBlock.Text = "Pointer Pressed";
        }

        void scrollviewer_PointerCanceled(object sender, PointerRoutedEventArgs e)
        {
            EventCalledTextBlock.Text = "Pointer Canceled";
        }

        void scrollviewer_PointerCaptureLost(object sender, PointerRoutedEventArgs e)
        {
            EventCalledTextBlock.Text = "Capture Lost";
        }

        void scrollviewer_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            EventCalledTextBlock.Text = "Pointer Released";
        }
        #endregion



        void pointerTimer_Tick(object sender, object e)
        {
            if (!CurrentPointerID.HasValue)
            {
                PollingTextBlock.Text = string.Empty;
                return;
            }

            try
            {
                var pointerPoint = PointerPoint.GetCurrentPoint(CurrentPointerID.Value);

                PollingTextBlock.Text = pointerPoint.IsInContact ? "Is In Contact" : "Not in Contact";
            }
            catch (Exception ex)
            {
                //This exception is raised when the user lifts finger without dragging.
                //assume finger is not in contact with screen
                PollingTextBlock.Text = "Not in Contact";
            }
        }

    }
}

XAML

 <Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="113*"/>
            <RowDefinition Height="655*"/>
        </Grid.RowDefinitions>
        <ScrollViewer x:Name="scrollviewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible" Grid.Row="1" >
            <Rectangle Fill="#FF3783CF" Height="100" Stroke="#FF33D851" Width="{Binding ElementName=grid, Path=ActualWidth}" Margin="100" StrokeThickness="4" />
        </ScrollViewer>
        <StackPanel Orientation="Vertical" Margin="45,25,0,0">
            <StackPanel Orientation="Horizontal">
            <TextBlock  HorizontalAlignment="Left" TextWrapping="Wrap" Text="Event Called:" VerticalAlignment="Top" FontSize="24" Margin="0,0,20,0"/>
            <TextBlock x:Name="EventCalledTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock  HorizontalAlignment="Left" TextWrapping="Wrap" Text="Polling Value:" VerticalAlignment="Top" FontSize="24" Margin="0,0,20,0"/>
            <TextBlock x:Name="PollingTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24"/>
        </StackPanel>
    </StackPanel>

    </Grid>
</Page>

2 个答案:

答案 0 :(得分:1)

我偶然发现了这个问题,因为我正在努力解决类似的问题。我有一个ScrollViewer,其中有几个图像,我想知道ScrollViewer停止移动时显示的图像......

最后我使用了ScrollViewer.ViewChanged事件。此事件会一直触发,直到完成滚动。

我实际上只对这些事件中的最后一个感兴趣,但由于没有事件仅在特定时刻触发,我需要回复此事件,并检查自己是否适合采取行动。< / p>

我希望这会有所帮助。

ScrollViewer.ViewChanged事件:https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.scrollviewer.viewchanged?f=255&MSPPError=-2147217396

答案 1 :(得分:0)

I think you need to use the PointerReleased event. Refer to the following link: https://msdn.microsoft.com/library/windows/apps/br208279