当我选择项目时,网格工作正常但是......当我按下左键选择其他练习时,应用程序崩溃。
这是代码:
int specialbtnId = 0;
private void gvSpecialPagesViewExercise_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
switch (specialbtnId)
{
case 0:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton1Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 1:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton2Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 2:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton3Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 3:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton4Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
}
}
这是另一个发泄按钮,是我现在用来改变练习和部分练习的唯一代码。
private void UpdateLeftButtonContent()
{
ResetSpecialPagesButtonColors();
if (currentPageSettings.PageStyle.Contains("Especiales"))
{
if (lstCurrentPageLeftButton1Excersices != null)
lstCurrentPageLeftButton1Excersices.Clear();
if (lstCurrentPageLeftButton2Excersices != null)
lstCurrentPageLeftButton2Excersices.Clear();
if (lstCurrentPageLeftButton3Excersices != null)
lstCurrentPageLeftButton3Excersices.Clear();
if (lstCurrentPageLeftButton4Excersices != null)
lstCurrentPageLeftButton4Excersices.Clear();
foreach (BookExercises x in lstCurrentPageBookExercises)
{
if (x.AssignmentCode == 0 && x.Id == 0)
{
lstCurrentPageLeftButton1Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 1)
{
lstCurrentPageLeftButton2Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 2)
{
lstCurrentPageLeftButton3Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 3)
{
lstCurrentPageLeftButton4Excersices.Add(x);
}
}
if (lstCurrentPageLeftButton1Excersices.Count > 0)
{
btn1SpecialPages.DataContext = lstCurrentPageLeftButton1Excersices.ElementAt(0);
}
if (lstCurrentPageLeftButton2Excersices.Count > 0)
{
btn2SpecialPages.DataContext = lstCurrentPageLeftButton2Excersices.ElementAt(0);
btn2SpecialPages.Visibility = Visibility.Visible;
btn2Line.Visibility = Visibility.Visible;
}
else
{
btn2SpecialPages.Visibility = Visibility.Collapsed;
btn3SpecialPages.Visibility = Visibility.Collapsed;
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn2Line.Visibility = Visibility.Collapsed;
btn3line.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
if (lstCurrentPageLeftButton3Excersices.Count > 0)
{
btn3SpecialPages.DataContext = lstCurrentPageLeftButton3Excersices.ElementAt(0);
btn3SpecialPages.Visibility = Visibility.Visible;
btn3line.Visibility = Visibility.Visible;
}
else
{
btn3SpecialPages.Visibility = Visibility.Collapsed;
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn3line.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
if (lstCurrentPageLeftButton4Excersices.Count > 0)
{
btn4SpecialPages.DataContext = lstCurrentPageLeftButton4Excersices.ElementAt(0);
btn4SpecialPages.Visibility = Visibility.Visible;
btn4line.Visibility = Visibility.Visible;
}
else
{
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
GridSectionTitle.DataContext = null;
GridSectionTitle.DataContext = currentPageSettings;
}
}
答案 0 :(得分:2)
如果未选择任何项目,则gvSpecialPagesViewExercise.SelectedIndex
将为-1,这是ElementAt
的无效值。
在切换之前添加处理程序以防止该异常:
if(gvSpecialPagesViewExercise.SelectedIndex <= 0)
return;
答案 1 :(得分:0)
虽然没有回答你的问题(显然你同时发现了这个问题),但这里有一些提示:
您的代码看起来非常复杂,因为您正在单独处理这四个列表。如果将它们放在数组中,则可以全局处理它们,从而简化代码。
List<BookExercises>[] exercises = new List<BookExercises>[4];
private void UpdateLeftButtonContent()
{
ResetSpecialPagesButtonColors();
if (currentPageSettings.PageStyle.Contains("Especiales")) {
for (int i = 0; i < exercises.Length; i++) {
exercises[i] = new List<BookExercises>();
}
foreach (BookExercises x in lstCurrentPageBookExercises) {
if (x.AssignmentCode == 0 && x.Id >= 0 && x.Id < exercises.Length) {
exercises[x.Id].Add(x);
}
}
...
}
}
int specialbtnId = 0;
private void gvSpecialPagesViewExercise_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
// If you are sure that this condition is always true, you can drop it.
if (specialbtnId >= 0 && specialbtnId < exercises.Length) {
BookExercises lst = exercises[specialbtnId];
int index = gvSpecialPagesViewExercise.SelectedIndex;
if (index >= 0 && index < lst.Count) {
LoadSpecialPagesBookExcersices(lst[index]);
}
}
}