简体   繁体   中英

Long Press Detector with c# in xamarin.forms

I am making a simple calculator app where the DEL button removes the last digit in the viewport, but if held down for one second it deletes everything in the viewport. Right now If I press the button once, it deletes the last digit whether I hold it down or not. If I press it down again (held down or just a regular tap) it clears everything in the viewport and I cannot type anything else in.

Firstly I create the timer like so:

public System.Timers.Timer timer = new System.Timers.Timer(1);

and then I have a function for when the 'DEL' button is pressed:

private void DeletePressed(object sender, EventArgs args)
    {
        timer.Stop();
        timer.Start();
        timer.Elapsed += AllClear;
    }

a function to stop the timer and delete the last character from the viewport when the button is released:

        private void DeleteReleased(object sender, EventArgs args)
    {
        timer.Stop();
        if (CalculatorString.Length > 0)
        {
            CalculatorString = CalculatorString.Remove(CalculatorString.Length - 1);
        }
        viewport.Text = CalculatorString;
    }

and finally the procedure that is called when the timer finishes:

private void AllClear(Object Source, System.Timers.ElapsedEventArgs e)
    {
        timer.Stop();
        CalculatorString = "";
        viewport.Text = CalculatorString;
    }

however none of what I expected tohas happened. I appreciate any help in advance :)

There is no direct way to listen long press in Xamarin.Forms(PCL). You have to write separate custom renderers for each platform to Listen long press and communicate it to the PCL. You can refer this link for code example for doing the same.

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