简体   繁体   English

点击时更改image.source

[英]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. 我正在为Windows Store开发游戏,并且想给运行周期设置动画。 I made many GIF animations but all have BLACK background, and I need it transparent. 我制作了许多GIF动画,但所有动画都具有黑色背景,因此我需要透明。 So I've decided to make a run cycle using DispatcherTimer. 因此,我决定使用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. 如图所示,xaml可以正常工作。

I have checked this question: ImageTools on Windows Phone 8, changing ImageSource and DataContext 我已经检查了以下问题: Windows Phone 8上的ImageTools,更改了ImageSource和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. 是一个布尔方法,仅将当前来源与您提供的“来源”进行比较,并根据比较结果返回true或false。 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: 假设“ moon.png”在您的解决方案的主文件夹中,则两个解决方案都可以工作:

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

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

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