(UWP) 在访问 ComboBox 时索引列表会引发 System.ArgumentOutOfRangeException。

huangapple go评论60阅读模式
英文:

(UWP) Indexing List Causes System.ArgumentOutOfRangeException when Accessing ComboBox

问题

I have a problem where indexing a List<class> will break code that is seemingly unrelated to it. Doing anything with the List will cause the exception, but if I just remove the activePeople[] the ComboBox code works. In both cases I have looked through all the Lists and made sure that there are values in all of them, so I don't understand how there can be an OutOfRange exception.

以下是翻译好的代码部分:

class Person 
{
    public string ID;
    public string Name;
}

ObservableCollection&lt;string&gt; peopleBox = new ObservableCollection&lt;string&gt; { &quot;All&quot; }; // More items are added before any of this code runs
List&lt;Person&gt; activePeople = new List&lt;Person&gt;(); // this also get&#39;s populated before any of the other code runs

private void RemovePersonClick(object sender, RoutedEventArgs e) 
{
    int selectedPersonIndex = ComboBox.SelectedIndex;

    if(selectedPersonIndex &lt; 1) return; //Don&#39;t do anything if &quot;All&quot; or nothing is selected

    activePeople.RemoveAt(selectedPersonIndex - 1); // -1 because it doesn&#39;t contain &quot;All&quot; at the start

    //Both of these throw System.ArgumentOutOfRangeException
    //Even though I have verified that the indexes exist
    ComboBox.SelectedIndex = 0; // 0 index always exists as &quot;All&quot; is never removed, i&#39;ve even tried -1 which should be nothing, but it throws the error
    peopleBox.RemoveAt(selectedPersonIndex);
}

// This code is always run before the RemovePersonClick method
private void SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
    //This causes the issue. If i index activePeople in any way, an error will be thrown in the RemovePersonClick method.
    //Removing this line stops the error. I can&#39;t find any link between the two, and I have verified that nothing is out of range.
    string ID = activePeople[0].ID;
}

I have tried removing the activePeople[0] which did solve the issue, but I need to get the value from activePeople. I have checked that values do exist in both the ComboBox and peopleBox, but I still get the error.

英文:

I have a problem where indexing a List<class> will break code that is seemingly unrelated to it. Doing anything with the List will cause the exception, but if I just remove the activePeople[] the ComboBox code works. In both cases I have looked through all the Lists and made sure that there are values in all of them, so I don't understand how there can be an OutOfRange exception.

The following code only takes parts from the actual code and doesn't follow the same structure, but both peopleBox and 'activePeople' will be populated before any of the code runs, and SelectionChanged() always runs before RemovePersonClick(). Note peopleBox is binded to ComboBox

class Person 
{
    public string ID;
    public string Name;
}

ObservableCollection&lt;string&gt; peopleBox = new ObservableCollection&lt;string&gt; { &quot;All&quot; }; // More items are added before any of this code runs
List&lt;Person&gt; activePeople = new List&lt;Person&gt;(); // this also get&#39;s populated before any of the other code runs

private void RemovePersonClick(object sender, RoutedEventArgs e) 
{
    int selectedPersonIndex = ComboBox.SelectedIndex;

    if(selectedPersonIndex &lt; 1) return; //Don&#39;t do anything if &quot;All&quot; or nothing is selected

    activePeople.RemoveAt(selectedPersonIndex - 1); // -1 because it doesn&#39;t contain &quot;All&quot; at the start

    //Both of these throw System.ArgumentOutOfRangeException
    //Even though I have verified that the indexes exist
    ComboBox.SelectedIndex = 0; // 0 index always exists as &quot;All&quot; is never removed, i&#39;ve even tried -1 which should be nothing, but it throws the error
    peopleBox.RemoveAt(selectedPersonIndex);
}

// This code is always run before the RemovePersonClick method
private void SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
    //This causes the issue. If i index activePeople in any way, an error will be thrown in the RemovePersonClick method.
    //Removing this line stops the error. I can&#39;t find any link between the two, and I have verified that nothing is out of range.
    string ID = activePeople[0].ID;
}

I have tried removing the activePeople[0] which did solve the issue, but I need to get the value from activePeople. I have checked that values do exist in both the ComboBox and peopleBox, but I still get the error.

答案1

得分: 0

> // -1 是因为它不包含以 "All" 开头

如果删除所有项目,activePeople 将变为空,在这种情况下访问 activePeople[0] 是无效的,你应该像这样处理这个异常。

string ID = activePeople.Count &gt; 0 ? activePeople[0].ID : &quot;All&quot;;
英文:

> // -1 because it doesn't contain "All" at the start

So if you remove all items, activePeople will become empty, in this situation accessing activePeople[0] is invalid, you should handle this exception like this.

string ID = activePeople.Count &gt; 0 ? activePeople[0].ID : &quot;All&quot;;

huangapple
  • 本文由 发表于 2023年5月6日 17:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定