0

我的应用程序需要每次使用提供的类别从网络加载图像并且它正在工作,问题是内存,加载到内存中的图像在下一个类别图像加载时不会被删除,因此内存增加并且应用程序关闭并显示以下消息 -

程序“[4036] TaskHost.exe”已退出,代码为 -2005270523 (0x887a0005)。

代码是---

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { timer.Stop(); 返回; } 列表 rootobj = JsonConvert.DeserializeObject>(e.Result);

     int c = 0, x = 0;
        for (int i = 0; i < rootobj.Count; i++)
        {
            Image img = new Image();
            img.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            img.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            img.Height = 160;
            img.Width = 210;
            img.Stretch = System.Windows.Media.Stretch.Fill;


            BitmapImage bit = new BitmapImage();
            string path = rootobj.ElementAt(i).ThumbnailUrl;
            bit.UriSource = new Uri(path,UriKind.RelativeOrAbsolute);
            img.Source = bit;
   img.Margin = new Thickness(x, y, 0, 0);


            c++;
            if (c == 2)
            {
                x = 0;
                y = y + 160;
                c = 0;
            }
            else
            {
                x = x + 210;
            }
            mainGrid.Children.Add(img);  
        } mainGrid.Children.Add(grid);
       }

并删除我曾尝试过这些 -

        for (int i = 0; i < rootobj.Count; i++)
        {
            Image image = (Image)mainGrid.Children.ElementAt(i);
            BitmapImage bitmapImage = image.Source as BitmapImage;
            bitmapImage.UriSource = null;
            image.Source = null;
            bitmapImage = null;
            image = null;
        }

        grid.Children.Clear();

        mainGrid.Children.Remove(grid);

但是在选择了几种类型的图像后它仍然崩溃。

4

1 回答 1

0

您可以执行以下操作:

grid1.Children.Remove(image1);
image1 = null;
于 2013-03-11T07:14:55.300 回答