0

i have button in my windows phone silverlight 8.1 app i created two event one click event and second hold event, i want when user hold such button then click event should not fire, but currently when i hold button and then leave that button click button also fires, so how to handle that.

 private void ExtraButton_Click(object sender, RoutedEventArgs e)
 {

 }

 private void ExtraButton_Hold(object sender, GestureEventArgs e)
 {

 }

so how to cancel click event when hold event performed.

Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34
  • i already try create two button (2nd button over 1st button using stack panel and collapsed visibility and visible programmatically) but both event still fire, also try mouse enter but both event still fires – Shubham Sahu Dec 13 '16 at 13:37

3 Answers3

1

You can achieve your requirement by unwire the button click event while holding and again wire the click event in PointerExited event. Refer the below code snippet.

Button btn = new Button();

btn.Holding += Btn_Holding;

btn.Click += Btn_Click;

btn.PointerExited += Btn_PointerExited;


        private void Btn_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            btn.Click += Btn_Click;
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Btn_Holding(object sender, HoldingRoutedEventArgs e)
        {
            btn.Click -= Btn_Click;
        }

In PointerExited event, you can hook the click event only if it is not already hooked using some conditions. It will improve the performance.

Divakar
  • 514
  • 1
  • 9
  • 25
  • Thanks' i will try tomorrow and if work, i will definitely Mark your answer as answered. And by the way can you answer another question, http://stackoverflow.com/questions/40845803/automatically-filter-order-listbox-items-windows-phone – Shubham Sahu Dec 10 '16 at 17:58
  • is not work because when i leave finger from button click event fires.. :( – Shubham Sahu Dec 11 '16 at 08:52
  • This solution actually seems quite functional, does it really not work in your case? Could you use `Debug.WriteLine` to see which events are fired and when? – Martin Zikmund Dec 12 '16 at 19:42
  • yes, but i used many tricks create button above first button using stack panel, and collapse first button when hold and first button and mouse enter on second button, and also use is enabled = true/false; for e.g on hold event make click event disable but on mouse leave event i need to enable it again but on mouse leave click event fires, – Shubham Sahu Dec 13 '16 at 13:40
  • 1
    I don't understand how could the click event fire on mouse leave, because this is actually a built in behavior of buttons, that they don't fire click if you move the pointer away from them. I will try to recreate this in UWP and get back to you – Martin Zikmund Dec 14 '16 at 07:53
1

I suggest you can replace the Button Click event with Button Tap event, because when you hold this button, the Button Hold event will be triggered firstly.

        private void button1_Hold(object sender, System.Windows.Input.GestureEventArgs e)
    {
        e.Handled = true;  
        MessageBox.Show("button have been holded");

    }

    private void button1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        MessageBox.Show("button have been tapped");
        //your code goes here
    }

After you set Handled property as true, then the Button Click event will not handle your gesture.

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
1

Use Button_Hold and Button_Tap events to achieve your requirement.