2013-02-23 54 views
1

我有大約30張圖像,我想保留爲透視控件中的項目。但是如果我所有這些都遇到了OutOfMemoryException。所以如果我超過一些限制,我想刪除數據透視表項,但如果我刪除透視選擇更改我得到InvalidException。在片段樞軸顯示中是樞軸控制。在windows phone中刪除數據透視表項

void PivotShow_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     AddItems(); 
    } 

    private void AddItems() 
    { 
     PivotItem toadd = PivotGen(images[i]); 
     i = (i + 1) % (images.Length); 
     PivotShow.Items.Add(toadd); 
     try 
     { 
      if (PivotShow.Items.Count > 3) 
       PivotShow.Items.RemoveAt(0); 
     } 
     catch (InvalidOperationException) 
     { 
      MessageBox.Show("Operation not allowed"); 
     } 
    } 

    private PivotItem PivotGen(string urlimage) 
    { 
     PivotItem p = new PivotItem(); 
     p.Margin = new Thickness(0, -90, 0, 0); 

     Image img = new Image(); 
     BitmapImage bmp = new BitmapImage(new Uri(urlimage, UriKind.Relative)); 
     img.Stretch = Stretch.Fill; 
     img.Source = bmp; 
     p.Content = img; 

     return p; 
     //PivotShow.Items.Add(p); 
    } 

在此先感謝

回答

1

這是因爲你試圖改變當前正在修改的集合最有可能發生的歷史。可以按如下步驟推遲代碼:

 EventHandler handler = null; 
     handler = (s, e) => 
     { 
      element.LayoutUpdated -= handler; 

      AddItems(); 
     }; 
     element.LayoutUpdated += handler; 

上面的代碼將在接下來的佈局傳遞調用AddItems。給這個去看看是否有幫助!

相關問題