显示 UserControl 时会触发什么事件? [英] What event is fired when a UserControl is displayed?

查看:42
本文介绍了显示 UserControl 时会触发什么事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 WPF UserControls 添加淡入淡出效果(动画)(尽管是为 FrameworkElement 添加,以使其更通用).

I'm trying to add a fade effect (animation) for WPF UserControls (although rather for FrameworkElement, to make it more general).

如果我让 Loaded 事件触发初始淡入,淡入淡出动画有时会在 UserControl 显示之前开始.结果一团糟.例如,如果 UserControl 在 Loaded 事件处理程序中执行一些冗长的操作(十分之几秒),例如执行查询,就会发生这种情况.

If I let the Loaded event trigger the initial fade-in, the fade animation will sometimes have started before the UserControl has been displayed. The result is a mess. That happens for example if the UserControl does something lengthy (a few tenths of a second), like execute a query, in the Loaded event handler.

因此,我需要处理在呈现内容时 FrameworkElement/UserControl 获取的一些事件,然后开始淡入.System.Windows.Window 有一个 ContentRendered 事件,但 UserControl 没有.那么,在呈现 FrameworkElement(或 UserControl)时会触发什么事件?

So, I would need to handle some event that FrameworkElement/UserControl gets when the content has been rendered, and start the fade-in then. The System.Windows.Window has a ContentRendered event, but UserControl has not. So, what event is fired when a FrameworkElement (or UserControl) has been rendered?

推荐答案

尝试检查 SizeChanged 或 LayoutUpdated 上的大小.实际宽度或高度不等于0时执行作业.

Try to check size on SizeChanged or LayoutUpdated. Do job when actual width or height not equals to 0.

view.LayoutUpdated+=(o,e)=>
{
  if (!loaded && (view.ActualHeight > 0 || view.ActualWidth > 0))
  {
     // You can also unsubscribe event here.
     loaded =true;
  }
}

这篇关于显示 UserControl 时会触发什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆