C#在windows手机应用程序中按下按钮更改图像
本文关键字:按钮 图像 windows 手机 应用程序 | 更新日期: 2023-09-27 17:58:46
我知道如何将图像放入图片框中,并通过按下按钮将其显示出来,但我如何通过按下同一按钮来更改图像?这是代码:
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));
}
}
}
添加类中所有图像的数组和一个增量如下的变量:
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.
}
假设您想继续转到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大小写的,而不是用camel大小写的。所以,它将是TracksImage
和NextButton
等等!以下是更多详细信息。