简体   繁体   中英

change image.source when tapped

I'm a novice programmer so don't be brutal. I'm making a game for Windows Store, and I want to animate a run cycle. I made many GIF animations but all have BLACK background, and I need it transparent. So I've decided to make a run cycle using DispatcherTimer. Everything works fine, but the images don't change :/

void timer_Tick(object sender, object e)
    {
        numer++;
        if (numer > 8) numer = 1;
        hero.Source.Equals("Assets/Anim/" + nazwa + numer + ".png");

    } 

Also, When I TAP a different image, it should change the image and other images, but it doesn't... what is wrong?

bool sun = true;

    private void Image_Tapped(object sender, TappedRoutedEventArgs e)
    {
        sun = !sun;
        if (sun == false)
        {
            Image1.Source.Equals("moon.png");
            Image2.Source.Equals("ON.png");
        }
        else
        {
            Image1.Source.Equals("sun.png");
            Image2.Source.Equals("OFF.png");
        }
    }

The xaml works fine, as the images are shown.

I have checked this question: ImageTools on Windows Phone 8, changing ImageSource and DataContext

but I get loads of errors. I don't seem to understand how the property changed works.

it seems to be a small mistake. You are using the wrong method.

 Image.Source.Equals()

is a boolean method that simply compares the current source with the "source" you give as arguement and will return true or false based on the comparison. But what you want is to set the source of the image. So you need to use:

 Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.RelativeOrAbsolute));

This will set the source of the Image to the new image you want.

Assuming that "moon.png" is in the main folder in your solution, the two solutions both work:

BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(@"moon.png", UriKind.Relative)).Stream);
Image1.Source = tn;

Or

Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.Relative));

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