简体   繁体   中英

C# adding extra tap for user interaction with visual studio

I want to add an two extra taps to a gestural interaction on a manipulation in c#. The user can tap once on a screen which brings up a message.. how can I make it so that the user has to tap three times on the screen to bring the same message up? Im new to C#, any help will be appreciated.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Input;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;


    namespace GesturesWP8._1
    {
     /// <summary>
    /// An empty page that can be used on its own or navigated to within a         Frame.
    /// </summary>
    public sealed partial class MainPage : Page
   {
     GestureRecognizer gestureRecognizer = new    Windows.UI.Input.GestureRecognizer();
    Windows.UI.Xaml.UIElement element;
    public MainPage()
    {
        this.InitializeComponent();
        this.Loaded += Page_Loaded;
        this.Unloaded += Page_Unloaded;
    }
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        //For making gestures operations on Grid named as 'LayoutRoot'
        GestureInputProcessor(gestureRecognizer, LayoutRoot);
    }
    private void Page_Unloaded(object sender, RoutedEventArgs e)
    {
        //Remove event handlers of gesture recognizer events
        gestureRecognizer.Tapped -= gestureRecognizer_Tapped;  
    }
    public void GestureInputProcessor(Windows.UI.Input.GestureRecognizer gr, Windows.UI.Xaml.UIElement target)
    {
        this.gestureRecognizer = gr;
        //Targeted Ui element to be performing gestures on it.
        this.element = target;

        //Enable gesture settings for Tap
        this.gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.Tap;
        // Set up pointer event handlers. These receive input events that are used by the gesture recognizer.
        this.element.PointerCanceled += OnPointerCanceled;
        this.element.PointerPressed += OnPointerPressed;
        this.element.PointerReleased += OnPointerReleased;
        this.element.PointerMoved += OnPointerMoved;

        // Set up event handlers to respond to gesture recognizer output

        gestureRecognizer.Tapped += gestureRecognizer_Tapped;   
    }

    //Pointer Events
    void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
    {
        // Route teh events to the gesture recognizer
        this.gestureRecognizer.ProcessDownEvent(args.GetCurrentPoint(this.element));
        // Set the pointer capture to the element being interacted with
        this.element.CapturePointer(args.Pointer);
        // Mark the event handled to prevent execution of default handlers
        args.Handled = true;
    }

    void OnPointerCanceled(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
    {
        this.gestureRecognizer.CompleteGesture();
        args.Handled = true;
    }

    void OnPointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
    {
        this.gestureRecognizer.ProcessUpEvent(args.GetCurrentPoint(this.element));
        args.Handled = true;
    }

    void OnPointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
    {
        this.gestureRecognizer.ProcessMoveEvents(args.GetIntermediatePoints(this.element));
    }   

    //Gesture Events

    void gestureRecognizer_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
    {
        TxtGestureNotes.Text = "Tap gesture recognized";

    }

}

}

You can keep track of how many times the user has tapped inside your Tapped event. Log the time of the last tap and if the new tap is close enough to that tap then update the count. If not then restart the count at zero.

Something like the following:

DateTime lastTap;
int numTaps;
TimeSpan tapRate; // initialize from Windows.UI.ViewManagement.UISettings.DoubleClickTime

void gestureRecognizer_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
{
    if (DateTime.Now < lastTap + tapRate)
    {
        numTaps++;
    }
    else
    {
        numTaps = 1;
    }
    lastTap = DateTime.Now;
    TxtGestureNotes.Text = string.Format("Tap gesture {0} recognized", numTaps);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM