<Window x:Class="WpfPhotos.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfPhotos"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Carousel"
mc:Ignorable="d"
Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image x:Name="TipImage" Grid.Row="0" />
<Grid Grid.Row="1" HorizontalAlignment="Center" Margin="0,0,0,-5">
<WrapPanel>
<Button Content="◀" Grid.Row="1" Click="PrevButton_Click"/>
<Button Content="▶" Grid.Row="2" Click="NextButton_Click"/>
</WrapPanel>
</Grid>
<TextBlock x:Name="TipText" Grid.Row="2" HorizontalAlignment="Center"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
namespace WpfPhotos
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadImage();
}
private List<string> imageUrls = new List<string>() {
"C:/Users/0051/source/repos/WpfPhotos/WpfPhotos/image/1.png",
"C:/Users/0051/source/repos/WpfPhotos/WpfPhotos/image/2.png",
"C:/Users/0051/source/repos/WpfPhotos/WpfPhotos/image/3.png" };
private int currentImageIndex = 0;
private void LoadImage()
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(imageUrls[currentImageIndex]);
bi.EndInit();
TipImage.Source = bi;
}
private void NextButton_Click(object sender, RoutedEventArgs e)
{
currentImageIndex++;
if (currentImageIndex == imageUrls.Count)
currentImageIndex = 0;
LoadImage();
}
private void PrevButton_Click(object sender, RoutedEventArgs e)
{
currentImageIndex--;
if (currentImageIndex == -1)
currentImageIndex = imageUrls.Count - 1;
LoadImage();
}
}
}