C#实现图片切换效果

<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="&#x25C0;" Grid.Row="1" Click="PrevButton_Click"/>
               
                <Button Content="&#x25B6;" 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();
        }
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值