C# ListPicker索引自动失效异常

C# ListPicker索引自动失效异常,c#,windows-phone-7,C#,Windows Phone 7,我的ListPicker控件有问题。我已经在我的页面中实现了我的listpicker,如下所示,但是我在运行时收到一个IndexOutOfRangeException,我不太确定如何解决这个问题: if (e.AddedItems.Count <= 0) { return; } setingspage.xaml <phone:PhoneApplicationPage.Resources> <DataTemplate x:Name="SearchProvi

我的ListPicker控件有问题。我已经在我的页面中实现了我的listpicker,如下所示,但是我在运行时收到一个IndexOutOfRangeException,我不太确定如何解决这个问题:

if (e.AddedItems.Count <= 0)
{
    return;
}
setingspage.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>                

发生IndexOutOfRangeException时,行“string selectedItem=e.AddedItems[0]作为字符串;”在setingspage.xaml.cs中。我记得在StackOverflow的某个地方看到了如何解决这个问题,但我现在找不到这个来源。您知道如何设置边界或检查以确保不会发生此异常吗?提前感谢(任何代码帮助都会对我有所帮助!)。

因为
AddedItems
是一个IList,在您尝试访问它们之前,您能不能先检查一下其中是否有一些项目

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

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