简体   繁体   English

C#在Windows Phone应用程序中按按钮更改图像

[英]C# change image on button press in a windows phone app

I know how to put an image into a picture box and have it show up with the press of a button, but how would I change the image with the press of the same button? 我知道如何将图像放入图片框并通过按一个按钮将其显示出来,但是如何按同一按钮更改图像呢? Here's the code: 这是代码:

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

Add an array of all the images in your class and a variable that increments like this: 添加类中所有图像的数组和一个递增的变量,如下所示:

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

Then in your previousButton_Click() function: 然后在您的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.
}

Assuming that you want to keep going to next image on nextButton_Click , here's how you can do it. 假设您要继续转到nextButton_Click上的下一张图像,请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++;
}

Of course, you will have to take care of counter not exceeding the length of array. 当然,您将必须注意计数器的长度不能超过数组的长度。

Also, bonus: please stick to .Net naming conventions. 另外,要注意的是:请遵守.Net命名约定。 Names are written in pascal case instead of camel case here. 名称在这里用帕斯卡大小写而不是驼峰式写。 So, it would be TracksImage and NextButton and so on! 因此,它将是TracksImageNextButton等等! Here are more details. 这里有更多细节。

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

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