ListPicker IndexOutOfRangeException

时间:2012-03-12 21:58:58

标签: c# windows-phone-7

我的ListPicker控件存在问题。我在我的页面中实现了listpicker,如下所示,但是我在运行时收到了IndexOutOfRangeException,我不太清楚如何解决问题:

SettingsPage.xaml

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="SearchProviderItemTemplate">
        <TextBlock Text="{Binding SearchProvider}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<ScrollViewer x:Name="ContentPanel_Browser" Margin="12,0,12,0">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="70"/>
                        <RowDefinition Height="70"/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    ...

                    <!-- Search Provider -->
                    <TextBlock Text="Search provider" Margin="12,7,12,8"
                       Grid.Row="3" VerticalAlignment="Bottom"
                       Foreground="{StaticResource PhoneSubtleBrush}"/>
                    <toolkit:ListPicker x:Name="SearchProviderListPicker" Grid.Row="4" Grid.ColumnSpan="2" Margin="12,0,12,0"  
                                        ItemTemplate="{Binding SearchProviderItemTemplate}" 
                                        SelectionChanged="SearchProviderListPicker_SelectionChanged" />
                </Grid>
            </ScrollViewer>                

SettingsPage.xaml.cs

    string searchProvider;

    String[] SearchProvider = 
    {
        "Google", 
        "Bing",
        "Yahoo",
        "Ask",
        "AOL"
    };

    private void SearchProviderListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedItem = e.AddedItems[0] as string;  //IndexOutOfRangeException was unhandled

        switch (selectedItem)
        {
            case "Google":
                searchProvider = "http://www.google.com/search?q=";
                break;
            case "Bing":
                searchProvider = "http://www.bing.com/search?q=";
                break;
            case "Yahoo":
                searchProvider = "http://search.yahoo.com/search?p=";
                break;
            case "Ask":
                searchProvider = "http://www.ask.com/web?q=";
                break;
            case "AOL":
                searchProvider = "http://search.aol.com/search?q=";
                break;
            //default:
            //    SearchProvider = "http://search.aol.com/search?q=";
            //    break;
        }

IndexOutOfRangeException出现行'string selectedItem = e.AddedItems [0] as string;'在SettingsPage.xaml.cs中。我记得在StackOverflow上看到如何解决这个问题,但我现在找不到这个来源。有关如何设置边界或检查以确保此异常的任何想法都不会发生?提前致谢(任何代码帮助都会对我有所帮助!)。

1 个答案:

答案 0 :(得分:2)

由于AddedItems是IList,您是否可以在尝试访问它之前检查其中是否包含某些项目?

方法顶部的以下内容应该可以解决问题:

if (e.AddedItems.Count <= 0)
{
    return;
}