在Windows手机应用程序中按下按钮时C#更改图像

时间:2014-04-17 02:56:06

标签: c# windows

我知道如何将图像放入图片框并按下按钮显示,但是如何通过按下相同的按钮来更改图像?这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;

namespace HuntersApp
{

    public partial class TracksPage : PhoneApplicationPage
    {
        public TracksPage()
        {
            InitializeComponent();
        }
        string[] imageArray = new string[] { "/Images" };
        private void nextButton_Click(object sender, RoutedEventArgs e)
        {   

        }

        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            tracksImage.Source = new BitmapImage(new Uri("/Images/cottontail55.jpg",UriKind.Relative));
        }
    }
}

2 个答案:

答案 0 :(得分:0)

添加类中所有图像的数组以及一个如下所示的增量变量:

public partial class TracksPage : PhoneApplicationPage
{
    string[] imagePaths = new string[] { "/path/to/image1.jpg",
                                         "/path/to/image2.jpg",
                                         "/path/to/image3.jpg",
                                         "/path/to/image4.jpg",
                                         "/path/to/image5.jpg" };
    int i = 0;
    .
    .
    .

然后在previousButton_Click()函数中:

private void previousButton_Click(object sender, RoutedEventArgs e)
{
    tracksImage.Source = new BitmapImage(new Uri(imagePaths[i], UriKind.Relative));

    i = i == 4 ? 0 : i + 1; // Change 4 to the number of images you have + 1. In this example, I assume 5 images.
}

答案 1 :(得分:0)

假设您想继续nextButton_Click上的下一张图片,请按以下步骤操作。

string[] imageArray = new string[] { "url1", "url2" }; //This array contains URI strings
private counter = 0; //This denotes index of array

private void nextButton_Click(object sender, RoutedEventArgs e)
{   
    tracksImage.Source = new BitmapImage(new Uri(imageArray[counter], UriKind.Relative));
    counter++;
}

当然,你必须照顾不超过阵列长度的计数器。

另外,奖励:请坚持.Net命名约定。名称是用pascal case而不是camel case写的。那么,它将是TracksImageNextButton等等! Here是更多细节。