[英]WPF C# 'System.OutOfMemoryException' After Changing ImageSource Several Times
我有一个列表视图菜单,它将更改网格的背景图像,但是在更改图像约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;
}
}
尝试这个:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.