我有一个列表视图菜单,它将更改网格的背景图像,但在更改图像约5-6次后,程序冻结,当我关闭程序时,错误是System.OutOfMemoryException。这是我正在使用的代码。
private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (((ListViewItem)((ListView)sender).SelectedItem).Name)
{
case "Ground":
imageMap.Source = new BitmapImage(new Uri(@"\main\1.png", UriKind.Relative));
break;
case "Second":
imageMap.Source = new BitmapImage(new Uri(@"\main\2.png", UriKind.Relative));
break;
case "Third":
imageMap.Source = new BitmapImage(new Uri(@"\main\3.png", UriKind.Relative));
break;
case "Fourth":
imageMap.Source = new BitmapImage(new Uri(@"\main\4.png", UriKind.Relative));
break;
case "Fifth":
imageMap.Source = new BitmapImage(new Uri(@"\main\5.png", UriKind.Relative));
break;
case "Sixth":
imageMap.Source = new BitmapImage(new Uri(@"\main\6.png", UriKind.Relative));
break;
case "Seventh":
imageMap.Source = new BitmapImage(new Uri(@"\main\7.png", UriKind.Relative));
break;
default:
break;
}
}
发布于 2019-03-12 04:11:04
试试这个:
using System.GC;
...
private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
switch (((ListViewItem)((ListView)sender).SelectedItem).Name)
{
case "Ground":
imageMap.Source = new BitmapImage(new Uri(@"\main\1.png", UriKind.Relative));
break;
case "Second":
imageMap.Source = new BitmapImage(new Uri(@"\main\2.png", UriKind.Relative));
break;
case "Third":
imageMap.Source = new BitmapImage(new Uri(@"\main\3.png", UriKind.Relative));
break;
case "Fourth":
imageMap.Source = new BitmapImage(new Uri(@"\main\4.png", UriKind.Relative));
break;
case "Fifth":
imageMap.Source = new BitmapImage(new Uri(@"\main\5.png", UriKind.Relative));
break;
case "Sixth":
imageMap.Source = new BitmapImage(new Uri(@"\main\6.png", UriKind.Relative));
break;
case "Seventh":
imageMap.Source = new BitmapImage(new Uri(@"\main\7.png", UriKind.Relative));
break;
default:
break;
}
}
https://stackoverflow.com/questions/55109072
复制相似问题