alfah
alfah

Reputation: 2085

Holding event for Button in C# for Win 8 Metro App

I have a published app in the windows phone marketplace, which I'm trying to port to Win 8. I'm using Windows 8 Release Preview and Visual Studio Express RC 2012 for Win 8 and the code is C#-XAML.

I have created a custom 6x7 calendar. The first 7 buttons are put into the first StackPanel , the next into another panel and so forth. So there are 6 StackPanels holding 42 buttons. All these StackPanels are put into a Grid for the easy positioning.

Every button has is associated with a Holding EventHandler named OnLongPress. So the problem I'm facing is that when a button is pressed, the OnLongPress function is being called twice. On debugging I found that first time, the Holding state is Started and the next time it is called, the Holding state id Completed. I cannot figure out why it is being called twice.

Is it because the event is bubbled up?? :(

    private void OnLongPress(object sender, HoldingRoutedEventArgs e)
    {

            Button butClicked = (Button)sender;
            int iNumClicked = Convert.ToInt32(butClicked.Content.ToString());

            CycleManager pCycMan = CycleManager.Instance;

            string iVal, jVal;
            int iRow, jCol;
            string butName = butClicked.Name;
            iVal = butName.Substring(1, 1);
            jVal = butName.Substring(2, 1);
            iRow = Convert.ToInt32(iVal);
            jCol = Convert.ToInt32(jVal);

            DateTime dtSelDate = new DateTime(m_yearBuffer[iRow, jCol], m_monthBuffer[iRow, jCol], iNumClicked);

            int trackingStatus = pCycMan.IsDateOkForHistory(dtSelDate);
            // setting or resetting few colors based on few checks
    }

It would be helpful if someone can shed some light since I'm new to Win 8 dev.

Upvotes: 2

Views: 2774

Answers (2)

Yoyo
Yoyo

Reputation: 11

If you just want the event to fire only once when holding state is complete or cancel, try to use RightTapped.

Holding is intended for informational UI, but for interactions like displaying a context menu you should use RightTapped instead. You might handle Holding first to display a hint that a menu will appear, but to display the menu itself, use a RightTapped handler. See Touch interaction design or Guidelines for common user interactions for more info on how to use a Hold interaction in your app design.

http://msdn.microsoft.com/en-us/library/windows.ui.xaml.uielement.holding.aspx

RightTapped for a touch action results from processing an action that remains in one place for a certain amount of time. If it's a touch action, a Holding event from the same element always precedes this, but RightTapped won't fire until the touch point is released. If the time the pointer is pressed is too short and Tapped fires instead of Holding, or if the Hold action ends with HoldingState as Canceled, RightTapped won't fire.

Upvotes: 1

alfah
alfah

Reputation: 2085

I have solved the issue holding event being called twice, once on handling state is started and once on completed by including the following check. I'm still not sure if it is the right method.

if (e.HoldingState == Windows.UI.Input.HoldingState.Started)

Upvotes: 6

Related Questions