简体   繁体   English

WP8:更改BitMapImage时C#应用程序崩溃

[英]WP8: C# app crashes while changing BitMapImage

Okay, I do not know what is causing the crash of my app and I simply do not understand what is happening. 好的,我不知道是什么导致我的应用程序崩溃,而且我根本不了解发生了什么。 I will explain shortly what my app CAN do and what is my problem. 我将在短期内解释我的应用程序可以做什么以及我的问题是什么。 Furthermore I read barely any topic about that on here, and was clicking my way through different google sites. 此外,我在这里几乎没有阅读有关该主题的任何主题,并且正在通过不同的Google网站点击我的方式。 I am not finding a solution, so I have to ask! 我没有找到解决方案,所以我不得不问!

I have an image set as Background -> Works fine. 我将图像设置为背景->工作正常。

I have a TextBlock which displays different text every 15 seconds, controlled by a timer, each text is saved in a list! 我有一个TextBlock,它每15秒显示一次不同的文本,由计时器控制,每个文本都保存在列表中! -> Works fine. ->工作正常。

I have a fade in/fade out of that text -> Works fine. 我对该文本有淡入淡出功能->效果很好。

I have an application bar at the bottom -> Works fine. 我在底部有一个应用程序栏->运行正常。

Chaing one explicit picture with that peace of code -> Works fine. 以那种和平的代码来跟踪一幅明确的图片->效果很好。

private void Appearance_Click(object sender, EventArgs e)
    {
         Hintergrund.Source = new BitmapImage(new Uri("/Pictures/StarsNight19.jpg",    UriKind.Relative));
    }

Well I have around 20 different Images, all have pretty the same names, saved in a folder in my project. 好吧,我有大约20种不同的图片,它们的名称都差不多,都保存在我的项目的文件夹中。 The path is as shown in the code fragment: /Pictures/StarsNightXX.jpg 路径如代码片段所示:/Pictures/StarsNightXX.jpg

Build Action is set to: CONTENT (well tried everything basically..) 构建动作设置为:CONTENT (基本上已经尝试了所有方法。)

Copy To Output Directory is set to: Copy Always. 复制到输出目录设置为:始终复制。

Now here is my problem. 现在这是我的问题。

I saved the names of my images in a List. 我将图像的名称保存在列表中。

        .....
        pictures.Add("StarsNight4.jpg");
        pictures.Add("StarsNight5.jpg");
        pictures.Add("StarsNight6.jpg");
        ....

I use the same operation as before, wanting it to change the image when clicked on the nice little button in my application bar: 我使用与以前相同的操作,希望当我在应用程序栏中单击漂亮的小按钮时更改图像:

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 APP CRASHES BOOM应用崩溃

I just can not figure out where the problem is. 我只是不知道问题出在哪里。

Changing it by writing an explicit name as shown at the beginning is working out fine... 如开头所示,通过写一个明确的名称来更改它可以正常工作...

Maybe someone can tell me if the list is causing a problem? 也许有人可以告诉我该列表是否引起问题? I just can not figure it out. 我只是想不通。

"looping it" does not work out either: “循环”也不可行:

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++;

    }

Because I am testing my App directly on my WP I do not know what kind of exception I get. 因为我是直接在WP上测试我的App,所以我不知道会遇到哪种异常。 No way compiling and testing it on my computer just to let you know. 只是为了让您知道,无法在我的计算机上进行编译和测试。

... Losing my mind right here. ...在这里失去我的头脑。

Kindly try this source code, I've tried to dispose of the object which are created in the source below, use your list and other code such as loop as it is. 请尝试使用此源代码,我已尝试处置在下面的源代码中创建的对象,使用您的列表和其他代码,例如循环。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM