在Windows Phone 8.1中操纵枢轴
本文关键字:操纵 Windows Phone | 更新日期: 2023-09-27 18:06:56
我有一个有2个PivotItems的Pivot。我试图检测用户是向左还是向右滑动。我认为我可以通过检查ManipulationStarted和ManipulationCompleted点之间的差异来检测到这一点。但无论我做什么,这些事件都不会被触发。
<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>
我也尝试检测指针。PointerEntered事件会被触发,但是PointerExited/PointerReleased/pointercancelled不会…
就像yasen说的- Pivot拦截触摸事件。我认为解决方案之一可能是禁用Pivot并使用Grid的ManipulationCompleted
事件(在这种情况下,您必须手动更改PivotItems):
<Grid x:Name="LayoutRoot" Background="Transparent">
<Pivot Name="myPivot">
<PivotItem Header="One"/>
<PivotItem Header="Two"/>
<PivotItem Header="Three"/>
</Pivot>
</Grid>
后面的代码:
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;
}
您所谈论的事件是由Pivot本身处理的,因此您自己捕捉它们可能比较棘手。
也许你可以使用Pivot的SelectionChanged事件?或者是Loading/Loaded/unload/unloaddpivotitem事件?
很抱歉晚给你答复。
你是对的,WP 8的一些系统控制。X,拦截触摸事件。原因是系统控制的性能优化和用户体验的改善。但是操作系统提供了一种通过FrameworkElement的UseOptimizedManipulationRouting属性来控制这种行为的方法。