您的位置:首页 > 健康 > 美食 > 在手机上可以做网页吗_国内网站建设代理_重庆seo排名扣费_深圳市文化广电旅游体育局

在手机上可以做网页吗_国内网站建设代理_重庆seo排名扣费_深圳市文化广电旅游体育局

2025/1/1 11:05:12 来源:https://blog.csdn.net/Hat_man_/article/details/144632910  浏览:    关键词:在手机上可以做网页吗_国内网站建设代理_重庆seo排名扣费_深圳市文化广电旅游体育局
在手机上可以做网页吗_国内网站建设代理_重庆seo排名扣费_深圳市文化广电旅游体育局

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>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com