Wrong behavior and Catastrophic Failure with ListBox control - Windows Phone 8.1

1

I have a ListBox populated by ListBoxItems based on a DataTemplate. In the constructor of the page, I'm adding objects to a list and I'm placing it as a source of the ListBox.

Here is my constructor code:

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;
}

The ListBox appears fine on the emulator but I'm facing two wrong behaviors:

I'm firing the SelectionChanged event of the ListBox in order to get the selectedItem and make some actions on it as deleting the selected item.

For that I've used this code:

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

    }
}

First Issue: The first problem is that this event is fired as soon as the page loaded and I don't know where it comes from because I'm not calling that event on loading.

Second Issue: When I try to remove an item from the list using the code above, I'm always getting a CATASTROPHIC ERROR:Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) and I don't know what could cause that. I've tried the given solution on that same topic using the dispatcher but didn't solve the issue.

The solution for the second issue: As you can see above, I was setting the my custom object list as source of the ListBox, but the right way to do that i to have an another method that you would call in the constructor and which will populate the ListBox by adding Items, as below:

 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);
        }
    }

Then I'm able to manage, delete and add item by using the method attached to the ListBox control.

Here the xaml used to design the ListBox:

 <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>

Does somebody know what's happening here ?

c#
listbox
windows-phone-8.1
asked on Stack Overflow Nov 21, 2014 by Hubert Solecki • edited May 23, 2017 by Community

2 Answers

0

1) Remove the SelectionChanged event handler assignment from the Listbox's XAML. Add it after you've bound the ListBox to the List, then add it back programmatically.

2) Try removing the item from the list, then rebinding the list to the ListBox.

answered on Stack Overflow Nov 21, 2014 by Matt Small
0

For this I have done .On Clearing of list box items

listbox.Itemssource=null;

Then its Working fine for me

answered on Stack Overflow Nov 3, 2015 by venkatasuresh

User contributions licensed under CC BY-SA 3.0