Background Color Changes

前端 未结 1 762
长情又很酷
长情又很酷 2021-01-28 18:17
SolidColorBrush bgColor;

public ModernBTN() {
  InitializeComponent();
  this.Loaded += delegate (object sender, RoutedEventArgs e) {
    bgColor = (SolidColorBrush)Bac         


        
1条回答
  •  有刺的猬
    2021-01-28 19:14

    bgColor = (SolidColorBrush)Background;

    Because SolidColorBrush is a reference type, bgColor and Background will reference the same object after the above line. So, when changes are made to Background (as you do with the animation), this changes will be reflected in bgColor.

    An easy way to solve this may be to changebgColor to type Color:

    Color bgColor;
    
    public MainWindow()
    {
       InitializeComponent();
       this.Loaded += delegate (object sender, RoutedEventArgs e) {
            bgColor = ((SolidColorBrush)Background).Color;
            MouseEnter += EnterAnim;
            MouseLeave += LeaveAnim;
       };
    }
    
    private void EnterAnim(object sender, MouseEventArgs e)
    {
        ColorAnimation animC = new ColorAnimation(BGHover, TimeSpan.FromMilliseconds(200));
        myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
    }
    
    private void LeaveAnim(object sender, MouseEventArgs e)
    {
        ColorAnimation animC = new ColorAnimation(bgColor, TimeSpan.FromMilliseconds(200));
        myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
    }
    

    0 讨论(0)
提交回复
热议问题