从动画完成事件获取 UIElement

发布于 2024-11-03 13:53:18 字数 870 浏览 3 评论 0原文

从我的代码隐藏中,我想在特定的 UIElement 上启动动画,当该动画结束时,我想对该 UIElement 进行一些其他处理。我无法弄清楚如何将作为动画完成事件的发送者收到的 AnimationClock 对象转换为执行动画的 UIElement 对象。

这是我用来构建和启动动画的代码:

DoubleAnimation FadeOutAnim = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(.5));
FadeOutAnim.Completed += new EventHandler(FadeOutAnim_Completed);

UIElement element = lstMessages.ItemContainerGenerator.ContainerFromItem(sender) as UIElement;
if(element != null)
   element.BeginAnimation(UIElement.OpacityProperty, FadeOutAnim);

这是我的 Completed 事件,我想在其中再次访问 UIElement

void FadeOutAnim_Completed(object sender, EventArgs e)
    {
        UIElement animation = sender; //This is an AnimationClock and I can't seem to figure out how to get my UIElement back.

    }

任何帮助将不胜感激。

From my codebehind I want to start an animation on a specific UIElement, when that animation ends I would like to do some other processing on that UIElement. I am having trouble figuring out how to convert the AnimationClock object that I receive as the sender of an Animation Completed event into the UIElement object that the animation was performed on.

Here is the code I use to build and start the animation:

DoubleAnimation FadeOutAnim = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(.5));
FadeOutAnim.Completed += new EventHandler(FadeOutAnim_Completed);

UIElement element = lstMessages.ItemContainerGenerator.ContainerFromItem(sender) as UIElement;
if(element != null)
   element.BeginAnimation(UIElement.OpacityProperty, FadeOutAnim);

And here is my Completed event where I want access to the UIElement again.

void FadeOutAnim_Completed(object sender, EventArgs e)
    {
        UIElement animation = sender; //This is an AnimationClock and I can't seem to figure out how to get my UIElement back.

    }

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

落在眉间の轻吻 2024-11-10 13:53:18

如果处理程序无用(我无法找到一种方法来恢复动画元素),您可以引发另一个包含该信息的事件:

private event EventHandler FadeAnimationCompleted;
private void OnFadeAnimationCompleted(object sender)
{
    var handler = FadeAnimationCompleted;
    if (handler != null)
        handler(sender, null);
}
FadeAnimationCompleted += new EventHandler(This_FadeAnimationCompleted);
FadeOutAnim.Completed += (s, _) => OnAnimationCompleted(element);
void This_FadeAnimationCompleted(object sender, EventArgs e)
{
    //Sender is the UIElement
}

甚至更简单的是在委托中进行直接方法调用:

FadeOutAnim.Completed += (s, _) => FadeAnimationCompleted(element);
void FadeAnimationCompleted(UIElement element)
{
    //Meaningful code goes here.
}

If the handler is useless (i for one cannot find a way to get the animated element back), you could just raise another event which does contain that information:

private event EventHandler FadeAnimationCompleted;
private void OnFadeAnimationCompleted(object sender)
{
    var handler = FadeAnimationCompleted;
    if (handler != null)
        handler(sender, null);
}
FadeAnimationCompleted += new EventHandler(This_FadeAnimationCompleted);
FadeOutAnim.Completed += (s, _) => OnAnimationCompleted(element);
void This_FadeAnimationCompleted(object sender, EventArgs e)
{
    //Sender is the UIElement
}

Even easier would be to make a direct method-call in the delegate:

FadeOutAnim.Completed += (s, _) => FadeAnimationCompleted(element);
void FadeAnimationCompleted(UIElement element)
{
    //Meaningful code goes here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文