2014-11-21 103 views
1

我有一個ListBoxListBoxItems填充,基於DataTemplate。在頁面的構造函數中,我將對象添加到列表中,並將它作爲ListBox的源代碼。ListBox控件的錯誤行爲和災難性故障 - Windows Phone 8.1

這裏是我的構造函數代碼:

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出現在模擬器罰款,但我面臨兩個錯誤行爲:

我燒成SelectionChanged事件ListBox,以獲取selectedItem並對其執行一些操作以刪除所選項目。

對於我用這個代碼:

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))和我不知道是什麼原因造成的。我嘗試了使用調度程序的同一topic上的給定解決方案,但沒有解決問題。

第二個問題解決辦法: 正如你可以在上面看到,我設置了我的自定義對象列表,列表框的來源,但以正確的方式做,我有一個另一種方法,你會調用構造函數和,這將通過增加項目填充列表框,如下圖所示:

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控件的方法添加項目。

這裏用來設計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> 

是否有人知道這裏發生了什麼?

+0

你的ListBox在加載時是否有焦點?這將導致選擇更改事件觸發。 – techvice 2014-11-21 16:46:21

+0

我們也可以得到關於您收到的錯誤的信息嗎?什麼是異常消息? – techvice 2014-11-21 16:52:16

+0

Thansk的答案,我不這麼認爲...我已經更新了添加xaml的地方,我用VisualStates設計了列表框 – 2014-11-21 16:52:20

回答

0

1)從列表框的XAML中刪除SelectionChanged事件處理程序分配。在將ListBox綁定到List之後添加它,然後以編程方式將其添加回來。

2)嘗試從列表中刪除的項目,然後重新綁定列表列表框。

0

爲此,我做了列表框中的項目。對清算

listbox.Itemssource = NULL;

那麼它對我的工作很好