我是C#和WP8编程的新手,我设计了一个带有两个按钮(下一个,上一个)和一个Image(Main_Frame)的简单应用程序。我创建了一个包含24个图像的文件夹,从“im2”到“im25”。我希望在点击下一个和上一个按钮时更改图像。
这就是我尝试过的 -
(XAML) -
< Button Content="<---" HorizontalAlignment="Left" Margin="0,601,0,0"
VerticalAlignment="Top" Grid.RowSpan="2" Click="Button_Click_1"/>
< Button Content="--->" HorizontalAlignment="Left" Margin="379,601,0,0"
VerticalAlignment="Top" Width="91" Height="72" Grid.RowSpan="2" Click="Button_Click"/>
< Image x:Name="Main_Frame" HorizontalAlignment="Left" Height="298" Margin="33,164,0,0" VerticalAlignment="Top" Width="393" Source="/Assets/BG.jpg" />
(C#) -
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 2; i < 26; i++)
{
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + i + ".jpg", UriKind.Relative));
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
for (int i=25; i > 2; i--)
{
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + i + ".jpg", UriKind.Relative));
}
循环不起作用,我只是分别点击下一个和上一个按钮转发到im2.jpg和im25.jpg。有帮助吗?
答案 0 :(得分:1)
请记住,每次单击该按钮,都会执行整个功能。你希望代码在循环中执行一步,但它会立即运行整个循环,这就是你跳到2或25的原因。
需要两个步骤来解决这个问题。第一个 - 保持当前图像保存在任何功能之外。那是
private int currentImage = 2;
private void Button_Click(object sender, RoutedEventArgs e)
...
这意味着currentImage
的值在函数的执行之间保存。
接下来,每次单击按钮时都需要正确更改currentImage的值。这需要两个部分 - 递增或递减currentImage,并验证它是否在2 <= currentImage <= 25
范围内。
例如:
private int currentImage = 2; //Initialize the value to the minimum image value
private void Button_Click(object sender, RoutedEventArgs e)
{
currentImage++; //Change currentImage (which is saved outside of the function)
if(currentImage > 25) //Did increasing the value go over my limit?
{
currentImage = 2; //Reset to minimum so it cycles through
}
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + currentImage + ".jpg", UriKind.Relative));
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
currentImage--; //Change currentImage (which is saved outside of the function)
if(currentImage < 2) //Did decreasing the value go over my limit?
{
currentImage = 25; //Reset to maximum so it cycles through
}
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + currentImage + ".jpg", UriKind.Relative));
}
答案 1 :(得分:0)
实际上我不明白你为什么要使用循环。
你可以像索引一样声明一个fiedl变量。
喜欢:int i = 2;
和bitmaImage对象。
然后每次单击一个按钮,你将i递增1,然后检查i的值,并像在代码中一样分配图像对象。
与私人按钮相同。
你添加了一些if语句。检查i >=25
或i <=2
答案 2 :(得分:0)
这是因为当您单击按钮时,将执行完整循环。您需要保存图像的当前索引。这是一个例子:
private int currentImageIndex = 2; // Initialize this to whichever image number you want
private void Button_Click(object sender, RoutedEventArgs e)
{
if(currentImageIndex < 25) // if not at the last image, increment and set new image
{
currentImageIndex++;
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + currentImageIndex + ".jpg", UriKind.Relative));
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if(currentImageIndex > 2) // if not at the first image, decrement and set new image
{
currentImageIndex--;
Main_Frame.Source = new BitmapImage(new Uri("/Assets/Eye_thing/im" + currentImageIndex + ".jpg", UriKind.Relative));
}
}