gpt4 book ai didi

c# - 淡出带有颜色的动画以使用标签突出显示屏幕上的变化

转载 作者:行者123 更新时间:2023-12-04 04:53:03 25 4
gpt4 key购买 nike

我是这个领域的新手。我正在屏幕上动态创建控件。在按钮上单击我正在更改标签值。我想添加一些带有颜色的淡出动画,以便捕捉用户的 View ,从而表明屏幕上发生了某些事情。目前我正在使用它来隐藏带有淡入淡出动画的控件

myLabel.Visibility = System.Windows.Visibility.Visible;
var a = new DoubleAnimation
{
From = 1.0,
To = 0.0,
FillBehavior = FillBehavior.Stop,
BeginTime = TimeSpan.FromSeconds(2),
Duration = new Duration(TimeSpan.FromSeconds(0.5))
};
var storyboard = new Storyboard();

storyboard.Children.Add(a);
Storyboard.SetTarget(a, myLabel);
Storyboard.SetTargetProperty(a, new PropertyPath(OpacityProperty));
storyboard.Completed += delegate { myLabel.Visibility = System.Windows.Visibility.Hidden; };
storyboard.Begin();

相反,我必须使用一些黄色框作为背景,它会淡出,以便指示屏幕上发生的事情。任何帮助都会很感激

最佳答案

关于Storyboard的问题没有 xaml 方法看起来不太好。

因此,这里有一种替代方法,可以与您从 xaml 发布的答案相同:

首先创建Storyboard在 xaml 资源中并分配一个 Key

<Storyboard x:Key="MyStoryboard">
<ColorAnimation BeginTime="0:0:0"
Duration="0:0:2"
Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)"
To="Yellow" />
<ColorAnimation BeginTime="0:0:2"
Duration="0:0:3"
Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)"
To="Transparent" />
</Storyboard>

接下来,因为您想从动态 Label 上的代码后面调用它你可以在你的代码隐藏中做这样的事情来在这个动态添加的 Label 上调用它:
var myStoryboard = (Storyboard)TryFindResource("MyStoryboard"); 
foreach(var animation in myStoryboard.Children)
Storyboard.SetTarget(animation, /* WhateverDynamicLabelControl */);
myStoryboard.Begin();

您可以从 here 获得此工作演示.如果您可以避免代码隐藏来调用 Storyboard ,然后去做,但我不确定你的 View 是如何设置的

更新:

访问相同的 Storyboard从多个角度来看,要么
  • 移动 Storyboard到两个 View 都可以访问的更高资源范围(例如在 app.xaml 中)
  • StoryboardResourceDictionary并将此字典合并到您需要 Storyboard 的 View 中。

  • 对于这些方法中的任何一种,请确保指定 x:Shared="False"在您的 Storyboard防止只读修改错误。

    所以而不是
    <Storyboard x:Key="MyStoryboard">

    你会有
    <Storyboard x:Key="MyStoryboard" x:Shared="False">

    关于c# - 淡出带有颜色的动画以使用标签突出显示屏幕上的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142652/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com