Windows phone 8 pivot中滑动方向的检测
我在WindowsPhone8中工作,在pivot控件中有两个pivot项。Windows phone 8 pivot中滑动方向的检测,windows-phone-8,pivot,Windows Phone 8,Pivot,我在WindowsPhone8中工作,在pivot控件中有两个pivot项。 如何检测我是向右还是向左刷卡?步骤1:在解决方案中添加Microsoft.Phone.Controls.Toolkit 步骤2:在xaml中添加Microsoft.Phone.Controls.Toolkit引用,如下所示: xmlns:tolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" &
如何检测我是向右还是向左刷卡?步骤1:在解决方案中添加Microsoft.Phone.Controls.Toolkit 步骤2:在xaml中添加Microsoft.Phone.Controls.Toolkit引用,如下所示:
xmlns:tolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<Grid x:Name="LayoutRoot" Background="Transparent">
<tolkit:GestureService.GestureListener>
<tolkit:GestureListener Flick="GestureListener_Flick"></tolkit:GestureListener>
</tolkit:GestureService.GestureListener>
<!--Pivot Control-->
<controls:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="item1">
<Grid/>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="item2">
<Grid/>
</controls:PivotItem>
</controls:Pivot>
</Grid>
步骤3:创建手势侦听器flick事件,如下所示:
xmlns:tolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<Grid x:Name="LayoutRoot" Background="Transparent">
<tolkit:GestureService.GestureListener>
<tolkit:GestureListener Flick="GestureListener_Flick"></tolkit:GestureListener>
</tolkit:GestureService.GestureListener>
<!--Pivot Control-->
<controls:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="item1">
<Grid/>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="item2">
<Grid/>
</controls:PivotItem>
</controls:Pivot>
</Grid>
对于简单情况(如果您有两个以上的透视项目),您可以使用透视事件的SelectionChanged
提供变量,您将在其中保存上次选择的索引,并在更改后检查它是右还是左:
myPivot.SelectionChanged+=myPivot_SelectionChanged; // in your MainPage()
private int lastSelected = 0;
private void myPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((lastSelected + 1 == myPivot.SelectedIndex) ||
(myPivot.SelectedIndex == 0 && lastSelected == myPivot.Items.Count - 1))
{
// moved right
}
else
{
// moved left
}
lastSelected = myPivot.SelectedIndex;
}
对于简单的情况,它应该可以工作,对于更复杂的情况,您可以使用
触摸面板
或其他解决方案。而不是使用项。计数
,您可以使用EventArgs参数e
来确定透视项集合中的最后一个索引和所选索引,方法如下:
private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Casting e.AddedItems and e.RemovedItems to the PivotItem type to get
// the Selected Pivot and the Last Selected Pivot
var selectedPivot = (PivotItem) e.AddedItems[0];
var lastPivot = (PivotItem) e.RemovedItems[0];
// Getting indices using the ItemCollection of the pivot
int selectedIndex = pivot.Items.IndexOf(selectedPivot);
int previousIndex = pivot.Items.IndexOf(lastPivot);
if (selectedIndex < previousIndex)
{
// user swiped to the right
}
else
{
// user swiped to the left
}
}
private void pivot\u SelectionChanged(对象发送方,selectionchangedventargs e)
{
//将e.AddedItems和e.RemovedItems强制转换为PivotItem类型以获取
//选定的轴和上次选定的轴
var selectedPivot=(PivotItem)e.AddedItems[0];
var lastpoivot=(PivotItem)e.RemovedItems[0];
//使用pivot的ItemCollection获取索引
int selectedIndex=pivot.Items.IndexOf(selectedPivot);
int-previousIndex=pivot.Items.IndexOf(lastpoivot);
如果(已选择索引<以前的索引)
{
//用户向右滑动
}
其他的
{
//用户向左滑动
}
}
即使您只有两个数据透视,这也会对您有所帮助。大家都做得很好,但是当我们单击按钮向前和向后导航数据透视项时,我们会怎么做呢 因此,只需将以下代码放在两个按钮中,即向前和向后。 您可以通过设置Pivot.SelectedIndex轻松实现这一点 前进
if(pivotName.SelectedIndex < (pivotName.Items.Count - 1))
pivotName.SelectedIndex++;
else
pivotName.SelectedIndex = 0;
检查后,我从问题标题中删除了一个标记-请注意,在大多数情况下,此代码对我有效,我在解决方案中使用了,如果您发现任何问题,请告诉我谢谢您的精彩解释,我认为您的代码在我的示例中不起作用,因为我使用了两个透视项,当我向右或向左键入时,上次选中和选中的索引将具有相同的值,如果我向左移动,则选中的索引也为(1)(因为我只有2个项目)所以我不会辨别方向。但如果有三个或更多项目,这是一个很好的解决方案。我认为在这种情况下我必须使用工具包。谢谢你的支持help@user3471718我必须说你是对的——有两个项目,仅仅根据它们的数字是不可能判断移动是右还是左。你可以用这样的东西来确定:var Selection=e.AddedItems[0];//Selection var LastSelection=e.RemovedItems[0]//最后一次选择,并通过将上面的内容转换为(PivotItem)来计算差异。@NavikGoswami当您只有两个项目时,则无论在哪个方向上滑动,您都将拥有相同的RemovedItem-因此仍然无法确定方向。