WPF设计标准学习记录29

发布于:2025-04-17 ⋅ 阅读:(23) ⋅ 点赞:(0)
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行为之阴影

网站公告

今日签到

点亮在社区的每一天
去签到