点击时更改image.source

时间:2014-07-23 06:45:46

标签: c# image windows-store-apps

我是一名新手程序员,所以不要野蛮。 我正在为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工作正常。

我检查了这个问题: ImageTools on Windows Phone 8, changing ImageSource and DataContext

但是我遇到了很多错误。我似乎不明白该物业是如何改变的。

2 个答案:

答案 0 :(得分:1)

这似乎是一个小错误。你使用的是错误的方法。

 Image.Source.Equals()

是一种布尔方法,只是将当前源与"源"进行比较。你作为争论给予,并将根据比较返回真或假。 但你想要的是设置图像的来源。 所以你需要使用:

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

这会将图像的来源设置为您想要的新图像。

答案 1 :(得分:0)

假设“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));
相关问题