Manipulation on Pivot in Windows Phone 8.1

时光怂恿深爱的人放手 提交于 2019-12-23 06:07:39

问题


I have a Pivot with 2 PivotItems. I'm trying to detect whether the user swiped left or right. I thought I could detect this by checking the difference between the ManipulationStarted and ManipulationCompleted point. But whatever I do, those events won't get triggered.

<Pivot x:Name="albumart_pivot" Margin="0,-30,0,0" ManipulationStarted="ManipulationStartedEvent" ManipulationCompleted="ManipulationCompletedEvent" ManipulationMode="TranslateX">
    <PivotItem Margin="0">
        <Grid>
            <Rectangle Canvas.ZIndex="1" Fill="White" Opacity="0.1" Margin="20"/>
            <Image x:Name="albumart0" Stretch="UniformToFill" Canvas.ZIndex="1" Source="{Binding albumart}" Margin="20" Height="{Binding screenwidth}" Width="{Binding screenwidth}" ManipulationStarted="albumart_pivot_ManipulationStarted" ManipulationCompleted="albumart_pivot_ManipulationCompleted"/>
        </Grid>
    </PivotItem>
    <PivotItem Margin="0">
        <Grid>
            <Rectangle Canvas.ZIndex="1" Fill="White" Opacity="0.1" Margin="20"/>
            <Image x:Name="albumart1" Stretch="UniformToFill" Canvas.ZIndex="1" Source="{Binding albumart}" Margin="20" Height="{Binding screenwidth}" Width="{Binding screenwidth}" ManipulationStarted="albumart_pivot_ManipulationStarted" ManipulationCompleted="albumart_pivot_ManipulationCompleted"/>
        </Grid>
    </PivotItem>
</Pivot>

I also tried with detecting Pointer. The PointerEntered event does get fired, but the PointerExited/PointerReleased/PointerCanceled don't...


回答1:


Like yasen had said - Pivot intercepts touch events. I think one of the solutions may be to disable your Pivot and make use of a Grid's ManipulationCompleted event (in witch you will have to change PivotItems manually):

In XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Pivot Name="myPivot">
        <PivotItem Header="One"/>
        <PivotItem Header="Two"/>
        <PivotItem Header="Three"/>                
    </Pivot>
</Grid>

In code behind:

public MainPage()
{
    this.InitializeComponent();

    myPivot.IsHitTestVisible = false; // disable the Pivot 
    LayoutRoot.ManipulationMode = ManipulationModes.TranslateX;
    LayoutRoot.ManipulationCompleted+=LayoutRoot_ManipulationCompleted;
}

private void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    var velocity = e.Velocities;
    if (velocity.Linear.X < 0) // swipe to the left
    {
        if (myPivot.SelectedIndex < myPivot.Items.Count - 1) myPivot.SelectedIndex++;
        else myPivot.SelectedIndex = 0;
    }
    else if (myPivot.SelectedIndex > 0) myPivot.SelectedIndex--; // to the right
    else myPivot.SelectedIndex = myPivot.Items.Count - 1;
}



回答2:


The events you're talking about are handled by the Pivot itself, so it might be tricky to catch them yourself.

Maybe you can use the Pivot's SelectionChanged event? Or maybe the Loading/Loaded/Unloading/UnloadedPivotItem events?




回答3:


I'm sorry for the late answer.

You are right some system controls of WP 8.X, intercept touch events. The cause is the performance optimization of the system controls and improvement of the user experience. But the OS provides a way to control this behavior by the UseOptimizedManipulationRouting property of the FrameworkElement.



来源:https://stackoverflow.com/questions/24043556/manipulation-on-pivot-in-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!