WPF+AduSkin实现图片轮播

新建WPF项目

项目结构如下:

在这里插入图片描述

安装AduSkin

选中项目右键,在管理NuGet程序包中安装AduSkin v1.1.1.9

在这里插入图片描述

模型

Carousel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp_Carousel.Model
{
    public class Carousel
    {
        public string name { get; set; }
        public string imgpath { get; set; }
        public string info { get; set; }
    }
}

视图

定义用户控件:Carousel.xaml,引入AduSkin命名空间:

xmlns:Metro="clr-namespace:AduSkin.Controls.Metro;assembly=AduSkin"

<UserControl x:Class="WpfApp_Carousel.View.Carousel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp_Carousel.View"
             xmlns:Metro="clr-namespace:AduSkin.Controls.Metro;assembly=AduSkin"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Metro:Carousel x:Name="Carousels" AutoPlay="True" AutoPlaySpeed="1000" Height="350" Width="700" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Metro:Carousel.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image x:Name="cover" RenderTransformOrigin="0.5,0.5" Source="{Binding imgpath}" Stretch="UniformToFill" >
                            <Image.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="1" ScaleY="1" />
                                </TransformGroup>
                            </Image.RenderTransform>
                        </Image>
                        <Metro:AduButtonIcon
                                        x:Name="PlayBtn"
                                        Background="Transparent"
                                        Margin="10,50" Width="60"
                                        HorizontalAlignment="Right" VerticalAlignment="Bottom"
                                        Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}, Path=DataContext.PlayRecommendClick}"
                                        CommandParameter="{Binding}"
                                        Foreground="#fff"
                                        Icon="{StaticResource Icon_Desgin}"
                                        IconHeight="16"
                                        IconWidth="25"
                                        Visibility="Hidden" />
                        <StackPanel  HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,50">
                            <TextBlock Text="{Binding name}" Padding="6,0" Foreground="#FFF" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center"  MaxWidth="300" TextTrimming="CharacterEllipsis"/>
                            <TextBlock Text="{Binding info}" Padding="6,2" Foreground="#FFF" FontSize="14" HorizontalAlignment="Center"  MaxWidth="300" TextTrimming="CharacterEllipsis"/>
                        </StackPanel>
                    </Grid>
                    <DataTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="PlayBtn" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <!-- 鼠标进入事件 -->
                        <EventTrigger RoutedEvent="UIElement.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="cover" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" From="1" To="1.1" Duration="0:0:0.3" AutoReverse="False" />
                                    <DoubleAnimation Storyboard.TargetName="cover" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" From="1" To="1.1" Duration="0:0:0.3" AutoReverse="False" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <!-- 鼠标离开事件 -->
                        <EventTrigger RoutedEvent="UIElement.MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="cover" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" To="1" Duration="0:0:0.3" AutoReverse="False" />
                                    <DoubleAnimation Storyboard.TargetName="cover" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" To="1" Duration="0:0:0.3" AutoReverse="False" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Metro:Carousel.ItemTemplate>
        </Metro:Carousel>
    </Grid>
</UserControl>

后台代码:Carousel.xaml.cs

using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace WpfApp_Carousel.View
{
    /// <summary>
    /// Carousel.xaml 的交互逻辑
    /// </summary>
    public partial class Carousel : UserControl
    {
        public Carousel()
        {
            InitializeComponent();

            // 轮播图
            ObservableCollection<Model.Carousel> list = new ObservableCollection<Model.Carousel>();
            list.Add(new Model.Carousel() { imgpath = "../img/c1.jpg", name = "大自然", info = "测试" });
            list.Add(new Model.Carousel() { imgpath = "../img/c2.jpg", name = "大自然", info = "测试" });
            list.Add(new Model.Carousel() { imgpath = "../img/c3.jpg", name = "大自然", info = "测试" });
            list.Add(new Model.Carousel() { imgpath = "../img/c4.jpg", name = "大自然", info = "测试" });
            list.Add(new Model.Carousel() { imgpath = "../img/c5.jpg", name = "大自然", info = "测试" });
            this.Carousels.ItemsSource = list;
        }
    }
}

App.xaml

<Application x:Class="WpfApp_Carousel.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp_Carousel"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Geometry x:Key="Icon_Desgin">M61.141068 400.532698L3.815672 725.688936l61.197513 74.337875 83.560965-473.80941H-0.067734zM79.169555 298.333642h74.315297l-61.208801-74.315297zM359.011862 660.653173l-13.095206 74.337875 53.566168 65.024474 13.095207-74.326586zM455.171319 484.138819v0.011289H287.959077l59.3687 72.068792L107.64034 716.398113 92.908233 800.015522l347.508401-232.259293 1.648189-9.302112h0.011289l13.106496-74.304009h-0.011289zM494.490806 261.181638H187.904926l-13.095207 74.315297 61.208802 74.315298 19.676677-111.478591h157.921418l-26.201702 148.641884h74.315297l32.771884-185.793888h-0.011289z M617.224501 400.543987l-57.359263 325.156238 61.23138 74.326586 83.527098-473.80941H556.00441zM635.23041 298.344931h74.315297L648.325616 224.018345zM982.783966 224.018345h-74.326586l-32.749306 185.805177H717.7415l30.626979 37.163293H869.137893l-62.224809 353.039996 87.410504-74.326586 49.140891-278.71341h74.315298l6.558892-37.163293h-74.326586z</Geometry>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

引入用户控件命名空间:xmlns:c="clr-namespace:WpfApp_Carousel.View"

<Window x:Class="WpfApp_Carousel.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:WpfApp_Carousel"
        xmlns:Metro="clr-namespace:AduSkin.Controls.Metro;assembly=AduSkin"
        xmlns:c="clr-namespace:WpfApp_Carousel.View"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <c:Carousel/>
    </Grid>
</Window>

运行效果

在这里插入图片描述

其实能实现这个功能,很大程度上归功于AduSkin控件库的作者,里面还有其它封装好的控件,希望大家在Github搜索AduSkin并多多支持。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杼蛘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值