在WPF中,DoubleAnimation后UIElement属性无法更改?

发布于 2024-07-23 02:49:44 字数 1000 浏览 6 评论 0原文

好的,我正在使用 WPF 为我的 MP3 播放器制作一个 GUI,并且我有一个边框,可以为播放曲目的每一秒放大其宽度属性,从而为当前播放的歌曲制作一个“进度条”。 我将边框命名为 ProgressBarBorder。 整个播放列表完成后,我想使用 DoubleAnimation 淡出边框。 现在,如果我再次启动播放器,边框会按预期进行反应(意味着宽度从 0 开始并进展到歌曲的结尾),但由于某种奇怪的原因,不透明度属性保持为 0.0(这是 DoubleAnimation 的值)套)。 明确编码

ProgressBarBorder.Opacity = 1.0;

我已在开始播放的方法中 。 尽管如此,它仍然是不可见的。 现在,如果我不使用 DoubleAnimation,而只是

ProgressBarBorder.Opacity = 0.0; 

在播放列表完成时写入,那么当我再次启动播放器时,它会返回到 1.0。 这就是为什么我确信动画是导致问题的原因。 另外,动画结束后属性不是应该恢复到原始状态吗? 如果是,我的边框应该在动画完成后自动变得可见。

这是我的部分伪代码:

if (TrackIsComplete)
{
    DoubleAnimation Fading = new DoubleAnimation();
    Fading.From = 1.0;
    Fading.To = 0.0;
    Fading.Duration = TimeSpan.FromSeconds(3);
    ProgressBarBorder.BeginAnimation(Border.OpacityProperty, Fading);
}

有人

private void PlayTrack()
{
    ProgressBarBorder.Opacity = 1.0;
    Play();
    ....
}

可以帮忙吗? 谢谢。

OK, I'm making a GUI for my MP3 player using WPF and I have a border that enlarges its width property for every second of the played track, thus making a "Progress Bar" for the currently played song. I named the border ProgressBarBorder. After the whole playlist is complete, I wanted to use a DoubleAnimation to fade out the border. Now, if I start the player again, the border reacts as it's supposed to (meaning the width starts from 0 and progresses to the end of the song), but the opacity property for some strange reason stays 0.0 (that is the value that DoubleAnimation sets). I have explicitly coded

ProgressBarBorder.Opacity = 1.0;

in the method that starts the playback. Nevertheless, it stays invisible. Now, if I don't use DoubleAnimation and just write

ProgressBarBorder.Opacity = 0.0; 

when the playlist is complete, it does go back to 1.0 when I start the player again. This is the reason why I am positive that the animation is the one causing the problem. Also, isn't the property supposed to go back to it's original state after the animation is finished? If yes, my border should become visible automatically after the animation is complete.

Here's my partially pseudo-code:

if (TrackIsComplete)
{
    DoubleAnimation Fading = new DoubleAnimation();
    Fading.From = 1.0;
    Fading.To = 0.0;
    Fading.Duration = TimeSpan.FromSeconds(3);
    ProgressBarBorder.BeginAnimation(Border.OpacityProperty, Fading);
}

and

private void PlayTrack()
{
    ProgressBarBorder.Opacity = 1.0;
    Play();
    ....
}

Could anyone help please? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫雨熙 2024-07-30 02:49:44

动画保持其目标值。 要释放任何动画的依赖属性,请使用 null 值执行 BeginAnimation

private void PlayTrack()
{
    ProgressBarBorder.BeginAnimation(Border.OpacityProperty, null);
    ProgressBarBorder.Opacity = 1.0;
    ....
}

The animation is holding on to its target value. To free the dependency property of any animations, do a BeginAnimation with a null value:

private void PlayTrack()
{
    ProgressBarBorder.BeginAnimation(Border.OpacityProperty, null);
    ProgressBarBorder.Opacity = 1.0;
    ....
}
寄居人 2024-07-30 02:49:44

动画结束时,它会继续保留该值。 这就是导致您注意到的行为的原因,其中设置属性似乎不会更新它。 这里有关如何在动画播放后设置属性的一些信息应用于它。

When an Animation ends, it continues holding the value. This is what is causing the behavior you noticed, where setting the property does not appear to update it. Here's some info on how to set a property after an animation has been applied to it.

往日情怀 2024-07-30 02:49:44

尝试一下动画时间轴的 FillBevior。 这可能有帮助:
http://msdn.microsoft.com/ zh-cn/library/system.windows.media.animation.fillbehavior.aspx

Play around with the FillBevior of your animation timeline. This might help:
http://msdn.microsoft.com/en-us/library/system.windows.media.animation.fillbehavior.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文