向后按钮跳过收藏夹中的第一个项目
Backward Button Skip First Item from favorites
我有两个按钮,一个是后退的,另一个是前进的。
两个按钮都在文本块中一一显示收藏项。
前进按钮工作正常,但后退按钮总是跳过收藏夹列表中的第一项。
Example - I have 5 items in a favorite list. If i am in second item,
if i press backward button then it directly jump to last item and skip
first item from favorite list
XAML
<Button Name="BackwardButton" FontFamily="Segoe MDL2 Assets" Content="" />
<Button Name="ForwardButton" FontFamily="Segoe MDL2 Assets" Content="" />
<TextBlock Name="DisplayTextBlock" />
C#
private int _displayedFavoriteIndex = -1;
private void BackwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the previous item
_displayedFavoriteIndex--;
if (_displayedFavoriteIndex <= 0)
{
//we have reached the end of the list
_displayedFavoriteIndex = listobj.Count - 1;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}
private void ForwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the next item
_displayedFavoriteIndex++;
if (_displayedFavoriteIndex >= listobj.Count)
{
//we have reached the end of the list
_displayedFavoriteIndex = 0;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}
在检查是否到达列表开头时,如果 position <= 0
则换行。这包括第一项的情况,其中 position == 0
.
将条件更改为 position < 0
,它将按您预期的那样工作。
你需要更新你的 if 条件,它应该是 _displayedFavoriteIndex < 0 而不是 _displayedFavoriteIndex <= 0
private void BackwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the previous item
_displayedFavoriteIndex--;
if (_displayedFavoriteIndex < 0) // Change here
{
//we have reached the end of the list
_displayedFavoriteIndex = listobj.Count - 1;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}
我有两个按钮,一个是后退的,另一个是前进的。 两个按钮都在文本块中一一显示收藏项。 前进按钮工作正常,但后退按钮总是跳过收藏夹列表中的第一项。
Example - I have 5 items in a favorite list. If i am in second item, if i press backward button then it directly jump to last item and skip first item from favorite list
XAML
<Button Name="BackwardButton" FontFamily="Segoe MDL2 Assets" Content="" />
<Button Name="ForwardButton" FontFamily="Segoe MDL2 Assets" Content="" />
<TextBlock Name="DisplayTextBlock" />
C#
private int _displayedFavoriteIndex = -1;
private void BackwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the previous item
_displayedFavoriteIndex--;
if (_displayedFavoriteIndex <= 0)
{
//we have reached the end of the list
_displayedFavoriteIndex = listobj.Count - 1;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}
private void ForwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the next item
_displayedFavoriteIndex++;
if (_displayedFavoriteIndex >= listobj.Count)
{
//we have reached the end of the list
_displayedFavoriteIndex = 0;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}
在检查是否到达列表开头时,如果 position <= 0
则换行。这包括第一项的情况,其中 position == 0
.
将条件更改为 position < 0
,它将按您预期的那样工作。
你需要更新你的 if 条件,它应该是 _displayedFavoriteIndex < 0 而不是 _displayedFavoriteIndex <= 0
private void BackwardButton_Tapped(object sender, TappedRoutedEventArgs e)
{
if (FavoriteListBox.Items.Count > 1)
{
//move to the previous item
_displayedFavoriteIndex--;
if (_displayedFavoriteIndex < 0) // Change here
{
//we have reached the end of the list
_displayedFavoriteIndex = listobj.Count - 1;
}
//show the item
DisplayTextBlock.Text = listobj[ _displayedFavoriteIndex ].AnswerName;
}
}