WPF制作图片闪烁的自定义控件

1.定义自定义控件

BlinkingImage.cs:

复制代码
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace YourNamespace
{
    public class BlinkingImage : Control
    {
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(string), typeof(BlinkingImage), new PropertyMetadata(null, OnImageSourceChanged));

        public static readonly DependencyProperty IsBlinkingProperty =
            DependencyProperty.Register("IsBlinking", typeof(bool), typeof(BlinkingImage), new PropertyMetadata(false, OnIsBlinkingChanged));

        public string ImageSource
        {
            get { return (string)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        public bool IsBlinking
        {
            get { return (bool)GetValue(IsBlinkingProperty); }
            set { SetValue(IsBlinkingProperty, value); }
        }

        static BlinkingImage()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BlinkingImage), new FrameworkPropertyMetadata(typeof(BlinkingImage)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (IsBlinking)
            {
                StartBlinking();
            }
        }

        private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BlinkingImage;
            if (control != null)
            {
                control.UpdateImageSource();
            }
        }

        private static void OnIsBlinkingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BlinkingImage;
            if (control != null)
            {
                if ((bool)e.NewValue)
                {
                    control.StartBlinking();
                }
                else
                {
                    control.StopBlinking();
                }
            }
        }

        private void UpdateImageSource()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null && !string.IsNullOrEmpty(ImageSource))
            {
                image.Source = new BitmapImage(new Uri(ImageSource, UriKind.RelativeOrAbsolute));
            }
        }

        private void StartBlinking()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null)
            {
                DoubleAnimation animation = new DoubleAnimation
                {
                    From = 1.0,
                    To = 0.0,
                    Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                    AutoReverse = true,
                    RepeatBehavior = RepeatBehavior.Forever
                };
                image.BeginAnimation(OpacityProperty, animation);
            }
        }

        private void StopBlinking()
        {
            Image image = GetTemplateChild("PART_Image") as Image;
            if (image != null)
            {
                image.Opacity = 1.0;
                image.BeginAnimation(OpacityProperty, null);
            }
        }
    }
}

2. 定义控件样式

Generic.xaml:

复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:YourNamespace">
    <Style TargetType="{x:Type local:BlinkingImage}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BlinkingImage}">
                    <Image x:Name="PART_Image" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

3.将自定义的控件样式添加到App.xaml文件中

复制代码
<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

4. 使用自定义控件

MainWindow.xaml:

复制代码
<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
           <local:BlinkingImage Grid.Row="1" Grid.Column="0" ImageSource="./image.png"     Width="50" Height="70" IsBlinking="{Binding ElementName=CkOne,Path=IsChecked}"/>
        <CheckBox Grid.Row="1" Grid.Column="1" x:Name="CkOne" IsChecked="False"/>
    </Grid>
</Window>
相关推荐
绿龙术士1 天前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng3 天前
【WPF】Opacity 属性的使用
wpf
姬激薄3 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心3 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go5 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet5 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf