简体   繁体   中英

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. 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! -> 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. The path is as shown in the code fragment: /Pictures/StarsNightXX.jpg

Build Action is set to: CONTENT (well tried everything basically..)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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