我有一个列表框,我从媒体库加载一些图像。但是第二次加载或点击后退按钮时出现异常。这是我的代码............
public MainPage()
{
InitializeComponent();
getImages();
}
public void getImages()
{
try
{
MediaLibrary mediaLibrary = new MediaLibrary();
var pictures = mediaLibrary.Pictures;
foreach (var picture in pictures)
{
BitmapImage image = new BitmapImage();
image.SetSource(picture.GetImage());//out of memory exception
img mediaImage = new img();
mediaImage.Imgs = image;
imageList.Items.Add(mediaImage);
}
}
catch (Exception ex)
{
}
}
public class img
{
public img()
{ }
public BitmapImage Imgs { get; set; }
}
在Xaml ......
<ScrollViewer Name="sc" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" Margin="0,422,0,43">
<ListBox Name="imageList" SelectionChanged="imageList_SelectionChanged_1" Height="126">
<ListBox.ItemsPanel>
<ItemsPanelTemplate >
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Margin="10" Name="image1" Source="{Binding Imgs}" Height="150" Width="150"></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
在picture.getimage()中的getimages()方法中,它显示内存异常(我已经用代码编写)。我尝试过imageList.items.Clear()。但是它没有用。
所以请帮忙......