当前位置: 首页> 健康> 养生 > 网页设计教程与实训_国家税务网_郑州网络推广公司_搜图片找原图

网页设计教程与实训_国家税务网_郑州网络推广公司_搜图片找原图

时间:2025/7/26 16:19:48来源:https://blog.csdn.net/weixin_53545579/article/details/147161891 浏览次数:2次
网页设计教程与实训_国家税务网_郑州网络推广公司_搜图片找原图
public class TextBehavior : Behavior<TextBlock>
{protected override void OnAttached(){base.OnAttached();AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}protected override void OnDetaching(){base.OnDetaching();AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}private void AssociatedObject_MouseEnter(object sender,System.Windows.Input.MouseEventArgs e){var element = sender as TextBlock;DropShadowEffect effect = new DropShadowEffect();effect.Color = Colors.Gold;effect.ShadowDepth = 0;effect.BlurRadius = 15;element.Effect = effect;            }private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as TextBlock;DropShadowEffect effect = new DropShadowEffect();effect.Color = Colors.Gold;effect.ShadowDepth = 0;effect.BlurRadius = 15;element.Effect = null;}
}
public class TextBehavior : Behavior<TextBlock>
{// 当行为附加到TextBlock时的初始化操作 protected override void OnAttached(){base.OnAttached();// 绑定鼠标进入/离开事件(实现悬停交互)AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;// [!] 未校验AssociatedObject空引用风险 }// 当行为从TextBlock分离时的清理操作 protected override void OnDetaching(){base.OnDetaching();// 解除事件绑定(避免内存泄漏)AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;// [!] 未重置Effect可能导致残留状态 }// 鼠标进入事件:添加金色辉光效果 private void AssociatedObject_MouseEnter(object sender, MouseEventArgs e){var element = sender as TextBlock;// [!] 未处理element空值可能导致崩溃 DropShadowEffect effect = new DropShadowEffect();effect.Color = Colors.Gold;    // 固定金色调 effect.ShadowDepth = 0;        // 无偏移的全向辉光 effect.BlurRadius = 15;        // 模糊强度固定值 element.Effect = effect;       // [!] 高频创建对象可能引发GC压力 }// 鼠标离开事件:移除视觉效果 private void AssociatedObject_MouseLeave(object sender, MouseEventArgs e){var element = sender as TextBlock;// [!] 冗余创建未使用的Effect对象(建议直接置空)element.Effect = null;         // 清除效果 // [!] 未考虑平滑过渡动画 }
}
graph TD A[行为附加到TextBlock] --> B[注册MouseEnter/MouseLeave事件]B --> C{用户悬停交互}C -->|MouseEnter| D[创建金色辉光效果]C -->|MouseLeave| E[移除效果]F[行为分离] --> G[解除事件绑定]style D stroke:#FFD700,stroke-width:2px classDef warning fill:#FFF3E0,stroke:#FFA726;class B,F warning 
2. 优化实现建议
// 增强版代码示例(含动画与资源优化)
public class EnhancedTextBehavior : Behavior<TextBlock>
{private static readonly DropShadowEffect _sharedEffect = new DropShadowEffect {Color = Colors.Gold,ShadowDepth = 0,BlurRadius = 0  // 初始值 };protected override void OnAttached(){AssociatedObject.Effect = _sharedEffect;  // 共享静态资源 AssociatedObject.MouseEnter += StartGlowAnimation;AssociatedObject.MouseLeave += StartFadeAnimation;}private void StartGlowAnimation(object sender, MouseEventArgs e){_sharedEffect.BeginAnimation(DropShadowEffect.BlurRadiusProperty, new DoubleAnimation(15, TimeSpan.FromSeconds(0.3)) { EasingFunction = new CubicEase() });}private void StartFadeAnimation(object sender, MouseEventArgs e){_sharedEffect.BeginAnimation(DropShadowEffect.BlurRadiusProperty, new DoubleAnimation(0, TimeSpan.FromSeconds(0.5)) { EasingFunction = new BackEase() });}
}
<Window x:Class="HelloWorld.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:HelloWorld" xmlns:controls="clr-namespace:HelloWorld.Controls"xmlns:helper="clr-namespace:HelloWorld.MVVM" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:behavior="clr-namespace:HelloWorld.Behaviors"mc:Ignorable="d" Background="DarkCyan"Title="WPF中文网 - 行为 - www.wpfsoft.com" Height="350" Width="500"><Window.DataContext><local:MainViewModel/></Window.DataContext><Canvas><TextBlock Text="WPF中文网" Foreground="White" FontSize="60" Canvas.Left="92" Canvas.Top="75"><i:Interaction.Behaviors><behavior:TextBehavior/></i:Interaction.Behaviors></TextBlock><TextBlock Text="Behavior行为之阴影
关键字:网页设计教程与实训_国家税务网_郑州网络推广公司_搜图片找原图

版权声明:

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

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

责任编辑: