背景颜色变化

2024-05-11

SolidColorBrush bgColor;

public ModernBTN() {
  InitializeComponent();
  this.Loaded += delegate (object sender, RoutedEventArgs e) {
    bgColor = (SolidColorBrush)Background;
    MouseEnter += EnterAnim;
    MouseLeave += LeaveAnim;
  };
}

private void EnterAnim(object sender, MouseEventArgs e) {
  DoubleAnimation anim = new DoubleAnimation(0, myBtn.Width, TimeSpan.FromMilliseconds(200));
  indicatorBtn.BeginAnimation(WidthProperty, anim);
  ColorAnimation animC = new ColorAnimation(BGHover, TimeSpan.FromMilliseconds(200));
  myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
}

private void LeaveAnim(object sender, MouseEventArgs e) {          
  DoubleAnimation anim = new DoubleAnimation(myBtn.Width, 0, TimeSpan.FromMilliseconds(200));
  indicatorBtn.BeginAnimation(WidthProperty, anim);
  ColorAnimation animC = new ColorAnimation(Color.FromArgb(bgColor.Color.A, bgColor.Color.R, bgColor.Color.G, bgColor.Color.B), TimeSpan.FromMilliseconds(200));
  myBtn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animC);
}

请告诉我,如果我仅将 bgColor 值放在 this.Loaded 和 = (SolidColorBrush) 背景中,为什么 bgColor 会更改为 BGHover 颜色。

我的按钮的 ModernBtn.xaml xaml 代码

<UserControl x:Class="ModernButton.ModernBTN"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ModernButton"
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="200" Name="myBtn" Background = '#FF282829'>
<Grid Width="Auto" Height="Auto" Name="mainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding ElementName=myBtn, Path=BtnText}" FontSize="24" Foreground="#FFC7BBBB" TextAlignment="Center" Grid.Row="0" Name="txtBtn" Padding="{Binding ElementName=myBtn, Path=TextPadding}"></TextBlock>
    <Rectangle Grid.Row="1" Fill="Lime" Height="5" Name="indicatorBtn" Width="0"></Rectangle>
</Grid>

bgColor = (SolidColorBrush)背景;

Because SolidColorBrush是一个引用类型,bgColor and Background将在上面的行之后引用同一个对象。因此,当进行更改时Background(正如您对动画所做的那样),此更改将反映在bgColor.

解决这个问题的一个简单方法可能是改变bgColor输入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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

背景颜色变化 的相关文章

随机推荐

  • Android 中的 SSL 会话重用问题(J2SE 工作正常)

    我一直在 iOS Java 桌面和 Android 上尝试 SSL 会话重用 iOS 似乎尝试重用 SSL 会话 但并非总是如此 只要您在创建 SSLEngine 时传递主机名 端口 Java 似乎总是会重用会话 当我使用 Android
  • 找不到 NGINX brew 安装命令

    I do brew install nginx and get gt Downloading http nginx org download nginx 1 2 2 tar gz Already downloaded Library Cac
  • Pandas 将列添加到非引用数据框中

    这件事让我心潮澎湃好几个小时了 也许我遗漏了一些神秘的 陷阱 但它一定是非常违反直觉的 Trial unq 是一个两列数据帧 Trial unq2 是一个相同的副本 for 循环遍历 unique in 中的所有字符串 如果 unique
  • CollectionBase 与泛型

    我正在将应用程序从 NET 1 1 迁移到 NET 2 0 我应该删除 CollectionBase 的所有使用吗 如果是这样 最好的迁移策略是什么 是的 最好看的类是 System Collections Generic 我通常使用列表
  • 如何记录 Doxygen 中不存在的变量?

    例如 我在配置文件中定义了 theme 全局变量 Doxygen 不处理该变量 但我想记录下来 我尝试这样做 var theme brief Active theme 但没有成功 您可以创建一个 doxygen 特定文件来记录变量 例如 配
  • 锁定时 AVAudioPlayer 在 iPhone 5 中不播放音频

    使用 AVAudioPlayer 我尝试在 iphone 播放器播放时播放声音 当设备锁定时也是如此 问题是 在 iPhone 4s ios 7 中 效果很好 但在带有 6 和 7 ios 的 iPhone 5 上没有任何反应 In the
  • 哪种 SQL 模式能够更快地避免插入重复行?

    我知道有两种不重复插入的方法 第一个是使用WHERE NOT EXISTS clause INSERT INTO table name col1 col2 col3 SELECT s s s WHERE NOT EXISTS SELECT
  • 由于超时,应用程序无法在模拟器上启动

    从过去 1 小时开始 我无法启动我的应用程序Eclipse Helios和我的控制台Eclipse给出以下错误 2011 11 21 10 37 00 PagingScrollerExample Failed to install Pagi
  • 以编程方式创建表

    我正在开发 devexreport 我想以编程方式创建一个表 我使用这些代码 但有一个小问题 DevExpress XtraReports UI XRTable tbl new XRTable DevExpress XtraReports
  • Windows 8 Metro 应用程序的依赖注入框架

    我似乎找不到 Windows 8 Metro 应用程序的依赖注入框架 win8 Metro应用程序有框架吗 Ninject 尚不支持 win8 Metro 那么有人有建议吗 城堡 春天 你可以尝试Unity 3 0 for NET4 5 预
  • 升级到 Visual Studio 16.3.0 后,dotnet ef 命令不再起作用

    这种情况首先发生在家里 所以我想这可能是我家里的台式电脑的问题 但现在我回到工作岗位 我尝试升级并得到了同样的结果 升级前截图 升级 Visual Studio 后的屏幕截图 我得到的错误是 无法执行 因为找不到指定的命令或文件 造成这种情
  • c#.net 中委托事件的超级简单示例? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我需要一个非常简单的例子来了解自己对这些事件的了解 我觉得很难理解互联网或书籍上提供的示例 这是公开事
  • python 中的 tail -f 没有 time.sleep

    我需要在 python 中模拟 tail f 但我不想在读取循环中使用 time sleep 我想要一些更优雅的东西 比如某种阻塞读取 或者带有超时的 select select 但是 python 2 6 select 文档特别指出 它不
  • MPMoviePlayerViewController,删除 Quicktime 符号/添加背景图像?

    我有一个播放音频的 MPMoviePlayerViewController 我想删除 Quicktime 徽标和 或向播放器添加自定义背景图像 但保留播放控件 我发誓我在 iOS 5 之前就已经这样做过 但我无法重新弄清楚 我尝试过的事情
  • 如何在Python中测试变量是否为空[重复]

    这个问题在这里已经有答案了 val del val if val is None print null 我运行了上面的代码 但是得到了NameError name val is not defined 如何判断变量是否为null 并避免Na
  • GridLayout 之上的 FlowLayout 不起作用

    我正在尝试创建一款刽子手游戏 到目前为止 它进展顺利 但布局设计似乎不太到位 字母表应该以FlowLayout命令位于刽子手图片顶部 底部有 重新启动 帮助 添加新单词 和 退出 按钮 我究竟做错了什么 import java awt im
  • Rails 日志太详细

    如何防止 Rails 记录过多日志 这是我的 production log 文件中的典型跟踪 许多部分 缓存命中 它在开发中很有用 但我不希望在我的生产环境中使用它 Started GET redirected true for 46 19
  • SOAPUI:如何包含外部文件中的 Groovy 脚本

    How can I include groovy script from an external file 我试图使用 def script new GroovyScriptEngine d soapui payment v2 with l
  • CSS -webkit-外观:无;导致复选框未被选中

    All 我有一个复选框 我应用了以下 CSS 样式 webkit appearance none 我拥有的一些文本字段上有相同的代码 并且它们仍然可以正常工作 为什么此功能会导致不允许选中复选框 我喜欢这种复选框的样式 但仍然需要该功能才能
  • 背景颜色变化

    SolidColorBrush bgColor public ModernBTN InitializeComponent this Loaded delegate object sender RoutedEventArgs e bgColo