ListBox 控件的错误行为和灾难性故障 - Windows Phone 8.1



我有一个基于DataTemplateListBoxItems填充ListBox。在页面的构造函数中,我将对象添加到列表中,并将其作为 ListBox 的源放置。

这是我的构造函数代码:

public ContactsPage()
{
    List<Profil> listContacts = new List<Profil>();
    this.InitializeComponent();
    listContacts.Add(new Profil("Hub'z", "Hubert Solecki"));
    listContacts.Add(new Profil("Rachton", "Rachid Berthal"));
    listContacts.Add(new Profil("Hub'z", "Hubert Solecki"));
    listContacts.Add(new Profil("Rachid", "Rachid Berthal"));
    listContacts.Add(new Profil("Hub'z", "Hubert Solecki"));
    listContacts.Add(new Profil("Rachid", "Rachid Berthal"));
    listContacts.Add(new Profil("Hub'z", "Hubert Solecki"));
    listContacts.Add(new Profil("Rachton", "Rachid Berthal"));
    listContacts.Add(new Profil("Hub'z", "Hubert Solecki"));
    listContacts.Add(new Profil("Rachton", "Rachid Berthal"));
    ContactsOuters.ItemsSource = listContacts;
}

ListBox在模拟器上看起来很好,但我面临两种错误的行为:

我正在触发ListBoxSelectionChanged事件,以获取所选项目并对其执行一些操作,如删除所选项目。

为此,我使用了以下代码:

private async void ContactsOuters_SelectionChanged(object sender, SelectionChangedEventArgs e)
{       
    try
    {
        if (e.AddedItems.Count() > 0)
        {
            ContactsOuters.Items.RemoveAt(ContactsOuters.SelectedIndex);
        }
    }
    catch (Exception ex)
    {
    }
}

第一期:第一个问题是此事件在页面加载后立即触发,我不知道它来自哪里,因为我没有在加载时调用该事件。

第二期:当我尝试使用上面的代码从列表中删除项目时,我总是收到灾难性错误:灾难性故障(HRESULT:0x8000FFFF (E_UNEXPECTED)的异常),我不知道是什么原因造成的。我已经使用调度程序尝试了同一主题的给定解决方案,但没有解决问题。

第二个问题的解决方案:正如您在上面看到的,我将自定义对象列表设置为ListBox的源,但是正确的方法是使用另一个方法,您将在构造函数中调用该方法,该方法将通过添加Items来填充ListBox,如下所示:

 public void getContacts()
    {
        listContacts.Add(new ImOutLibrary.Profil("Hub'z", "Hubert Solecki"));
        listContacts.Add(new ImOutLibrary.Profil("Rachton", "Rachid Berthal"));
        listContacts.Add(new ImOutLibrary.Profil("Hub'z", "Hubert Solecki"));
        listContacts.Add(new ImOutLibrary.Profil("Rachid", "Rachid Berthal"));
        listContacts.Add(new ImOutLibrary.Profil("Hub'z", "Hubert Solecki"));
        listContacts.Add(new ImOutLibrary.Profil("Rachid", "Rachid Berthal"));
        listContacts.Add(new ImOutLibrary.Profil("Hub'z", "Hubert Solecki"));
        listContacts.Add(new ImOutLibrary.Profil("Rachton", "Rachid Berthal"));
        listContacts.Add(new ImOutLibrary.Profil("Hub'z", "Hubert Solecki"));
        listContacts.Add(new ImOutLibrary.Profil("Rachton", "Rachid Berthal"));
        foreach (ImOutLibrary.Profil profil in listContacts)
        {
            ContactsOuters.Items.Add(profil);
        }
    }

然后,我能够使用附加到 ListBox 控件的方法管理、删除和添加项。

下面是用于设计列表框的 xaml:

 <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot"
                        Background="White"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot"
                                               Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPointerOverBackgroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPointerOverForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot"
                                               Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemDisabledForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="PressedBackground"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0" />
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPressedForegroundThemeBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected" />
                                <VisualState x:Name="SelectedUnfocused" />
                                <VisualState x:Name="SelectedDisabled" />
                                <VisualState x:Name="SelectedPointerOver" />
                                <VisualState x:Name="SelectedPressed" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="PointerFocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid x:Name="InnerGrid" Background="Transparent">
                            <Rectangle x:Name="PressedBackground" Fill="White" Opacity="0" />
                            <ContentPresenter x:Name="ContentPresenter"
                      Content="{TemplateBinding Content}"
                      ContentTransitions="{TemplateBinding ContentTransitions}"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      Margin="{TemplateBinding Padding}" />
                            <Rectangle x:Name="FocusVisualWhite"
               Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}"
               StrokeEndLineCap="Square"
               StrokeDashArray="1,1"
               Opacity="0"
               StrokeDashOffset=".5" />
                            <Rectangle x:Name="FocusVisualBlack"
               Stroke="White"
               StrokeEndLineCap="Square"
               StrokeDashArray="1,1"
               Opacity="0"
               StrokeDashOffset="1.5" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

有人知道这里发生了什么吗?

1) 从列表框的 XAML 中删除 SelectionChanged 事件处理程序赋值。 在将 ListBox 绑定到列表后添加它,然后以编程方式重新添加它。

2) 尝试从列表中删除该项,然后将列表重新绑定到 ListBox。

为此

我已经做了.清除列表框项时

列表框。项目源 = 空;

然后它对我来说工作正常

最新更新