none
[UWP]Overriding Manipulation Events on PivotItem RRS feed

  • Question

  • I have a UWP app where I am using a pivot control with a collection of pivot items. Now, my requirement is to capture the swipe down/up gestures on the pivot item. I have done something like below:

    <Pivot>
        <PivotItem Header="jdslj" ManipulationMode="All" ManipulationStarted="UIElement_OnManipulationStarted" ManipulationDelta="UIElement_OnManipulationDelta"></PivotItem>
        <PivotItem Header="weew" ManipulationMode="All" ManipulationStarted="UIElement_OnManipulationStarted" ManipulationDelta="UIElement_OnManipulationDelta"></PivotItem>
        <PivotItem Header="332" ManipulationMode="All" ManipulationStarted="UIElement_OnManipulationStarted" ManipulationDelta="UIElement_OnManipulationDelta"></PivotItem>
    </Pivot>

    Problem is, when I do it, it overrides the behaviour of pivot control and now I am not able to swipe left/right to change the pivot item.

    Is there anything I can do to achieve both at the same time?



    Wednesday, June 1, 2016 1:56 AM

Answers

  • Hi Ashishks1987,

    >>"Problem is, when I do it, it overrides the behaviour of pivot control and now I am not able to swipe left/right to change the pivot item."

    By default we can not swipe the PivotItem in a non-touch device, in order to implement it, we can use ManipulationDeltaRoutedEventArgs.Delta property to get the most recent changes of the current manipulation, as a ManipulationDelta. After that, we can determine the direction of the swipe behavior by Translation of the ManipulationDelta and set the index to swipe the PivotItem, for example:

    private void UIElement_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
            {
                PivotItem PivotItem = sender as PivotItem;
                if(e.Delta.Translation.X>0)
                {
                    for(int i=0;i<MyPivot.Items.Count;i++)
                    {
                        if(MyPivot.Items[i].Equals(PivotItem))
                        {
                            if (i == MyPivot.Items.Count - 1)
                            {
                                MyPivot.SelectedIndex = 0;
                            }
                            else
                            {
                                MyPivot.SelectedIndex = i+1;
                            }
                            break;
                        }
                        e.Complete();
                    }
                }
                else
                {
                    for(int i = 0; i < MyPivot.Items.Count; i++)
                    {
                        if (MyPivot.Items[i].Equals(PivotItem))
                        {
                            if (i == 0)
                           {
                                MyPivot.SelectedIndex = MyPivot.Items.Count - 1;
                            }
                            else
                            {
                                MyPivot.SelectedIndex = i-1;
                            }
    
                            break;
                        }
    
                        e.Complete();
    
                    }
                }
    
            }
    


    Best Regards,

    Leon Guang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 3, 2016 9:14 AM

All replies

  • My understanding is manipulation events will be fired before swipe events so that the gesture might probably be handled and not reach to the swipe. I'm not sure but you can try following suggestions if they works.

    1, set e.IsHandled as false to let routed event continue

    2, use AddHandle() to attach to the event from C# code by setting the handledEventsToo parameter.

    Thursday, June 2, 2016 2:39 AM
  • Hi Jamles,

    I tried both but it didn't work.


    Thursday, June 2, 2016 7:46 AM
  • Hi Ashishks1987,

    >>"Problem is, when I do it, it overrides the behaviour of pivot control and now I am not able to swipe left/right to change the pivot item."

    By default we can not swipe the PivotItem in a non-touch device, in order to implement it, we can use ManipulationDeltaRoutedEventArgs.Delta property to get the most recent changes of the current manipulation, as a ManipulationDelta. After that, we can determine the direction of the swipe behavior by Translation of the ManipulationDelta and set the index to swipe the PivotItem, for example:

    private void UIElement_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
            {
                PivotItem PivotItem = sender as PivotItem;
                if(e.Delta.Translation.X>0)
                {
                    for(int i=0;i<MyPivot.Items.Count;i++)
                    {
                        if(MyPivot.Items[i].Equals(PivotItem))
                        {
                            if (i == MyPivot.Items.Count - 1)
                            {
                                MyPivot.SelectedIndex = 0;
                            }
                            else
                            {
                                MyPivot.SelectedIndex = i+1;
                            }
                            break;
                        }
                        e.Complete();
                    }
                }
                else
                {
                    for(int i = 0; i < MyPivot.Items.Count; i++)
                    {
                        if (MyPivot.Items[i].Equals(PivotItem))
                        {
                            if (i == 0)
                           {
                                MyPivot.SelectedIndex = MyPivot.Items.Count - 1;
                            }
                            else
                            {
                                MyPivot.SelectedIndex = i-1;
                            }
    
                            break;
                        }
    
                        e.Complete();
    
                    }
                }
    
            }
    


    Best Regards,

    Leon Guang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 3, 2016 9:14 AM