好的,我不知道是什么导致我的应用程序崩溃,而且我根本不了解发生了什么。我将在短期内解释我的应用程序可以做什么以及我的问题是什么。
此外,我在这里几乎没有阅读有关该主题的任何主题,并且正在通过不同的Google网站点击我的方式。
我没有找到解决方案,所以我不得不问!
我将图像设置为背景->工作正常。
我有一个TextBlock,它每15秒显示一次不同的文本,由计时器控制,每个文本都保存在列表中! ->工作正常。
我对该文本有淡入淡出功能->效果很好。
我在底部有一个应用程序栏->运行正常。
以那种和平的代码来跟踪一幅明确的图片->效果很好。
private void Appearance_Click(object sender, EventArgs e)
{
Hintergrund.Source = new BitmapImage(new Uri("/Pictures/StarsNight19.jpg", UriKind.Relative));
}
好吧,我有大约20种不同的图片,它们的名称都差不多,都保存在我的项目的文件夹中。路径如代码片段所示:/Pictures/StarsNightXX.jpg
Build Action设置为:CONTENT(基本上已经尝试了所有方法。)
复制到输出目录设置为:始终复制。
现在这是我的问题。
我将图像的名称保存在列表中。
.....
pictures.Add("StarsNight4.jpg");
pictures.Add("StarsNight5.jpg");
pictures.Add("StarsNight6.jpg");
....
我使用与以前相同的操作,希望当我在应用程序栏中单击漂亮的小按钮时更改图像:
private void Appearance_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int next = rnd.Next(0, pictures.Count - 1);
background.Source = new BitmapImage(new Uri("/Pictures/"+pictures.ElementAt(next), UriKind.RelativeOrAbsolute));
}
BOOM应用程序崩溃
我只是不知道问题出在哪里。
如开头所示,通过写一个明确的名称来更改它,效果很好...
也许有人可以告诉我该列表是否引起了问题?
我只是想不通。
“循环”也不可行:
int i = 0;
private void Appearance_Click(object sender, EventArgs e)
{
if (i >= pictures.Count) i = 0;
background.Source = new BitmapImage(new Uri("/Pictures/" + pictures.ElementAt(i), UriKind.RelativeOrAbsolute));
i++;
}
因为我直接在WP上测试我的App,所以我不知道会遇到哪种异常。
只是为了让您知道,无法在我的计算机上对其进行编译和测试。
...
我在这里失去理智。
最佳答案
请尝试使用此源代码,我已尝试处置在下面的源代码中创建的对象,使用您的列表和其他代码,例如循环。
private static BitmapImage bi = null;//this line at the top, not in function
private static Image si = null;//this line at the top, not in function
if bi!=null)
{
bi.Dispose();
bi = null;
}
if si!=null)
{
si.Dispose();
si = null;
}
BitmapImage bi = new BitmapImage();
Image si = new Image();
bi.BeginInit();
bi.UriSource = new Uri(@"/img/img1.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
si.Source = bi;
关于c# - WP8:更改BitMapImage时C#应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860227/