[英]change image.source when tapped
我是一个新手程序员,所以不要残酷。 我正在为Windows Store开发游戏,并且想给运行周期设置动画。 我制作了许多GIF动画,但所有动画都具有黑色背景,因此我需要透明。 因此,我决定使用DispatcherTimer进行运行。 一切正常,但是图像没有改变:/
void timer_Tick(object sender, object e)
{
numer++;
if (numer > 8) numer = 1;
hero.Source.Equals("Assets/Anim/" + nazwa + numer + ".png");
}
另外,当我拍拍另一张图像时,它应该更改该图像和其他图像,但事实并非如此……这是怎么回事?
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");
}
}
如图所示,xaml可以正常工作。
我已经检查了以下问题: Windows Phone 8上的ImageTools,更改了ImageSource和DataContext
但是我遇到了很多错误。 我似乎不了解该属性如何更改。
这似乎是一个小错误。 您使用了错误的方法。
Image.Source.Equals()
是一个布尔方法,仅将当前来源与您提供的“来源”进行比较,并根据比较结果返回true或false。 但是,您要设置图像的来源。 因此,您需要使用:
Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.RelativeOrAbsolute));
这会将“图像”的源设置为所需的新图像。
假设“ moon.png”在您的解决方案的主文件夹中,则两个解决方案都可以工作:
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(@"moon.png", UriKind.Relative)).Stream);
Image1.Source = tn;
要么
Image1.Source = new BitmapImage(new Uri("moon.png", UriKind.Relative));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.