一个WPF用户控件火灾Loaded事件两次 [英] Loaded event of a WPF user control fire two times

查看:1718
本文介绍了一个WPF用户控件火灾Loaded事件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要落实在WPF中,我们需要但是我们的WPF的形​​式转换为用户控件来实现基于标签的环境中,这样当标签基础的环境,加载事件用户控件的是名为两次

To implement tab based environment in WPF we need to convert our WPF form to user control to accomplish tab based environment, however when doing this, loaded event of user control is called two times.

尽管互联网其他人也指出这个问题上的搜索。我们如何才能确保加载事件被称为只有一次,因为控制,当它被多次调用初始化发生多次。

While searching on the internet other people also pointed this issue. How can we ensure that loaded event is called only one time, because when it is called multiple times initialization of controls happen multiple times.

感谢

推荐答案

在本解释的博客,Loaded事件被当过某个控件被渲染发射(即加入到视觉树)。

As explained in this blog, the Loaded event is fired when ever a control is about to be rendered (i.e. added to the visual tree).

有这将导致你的控制几个控件加载/卸载多次。例如,本机的WPF的TabControl只呈现所选选项卡的内容。所以,当你选择一个新的标签,以前选择的选项卡的内容被卸载。如果您单击后退到先前选择的选项卡,然后它的内容被重新装载。

There are several controls that would cause your control to be loaded/unloaded multiple times. For example, the native WPF TabControl only renders the content of the selected tab. So when you select a new tab, the content of the previously selected tab is unloaded. If you click back to the previously selected tab, then it's content is reloaded.

一个解决办法是使用一个布尔值,标志是否已经初始化你的控制,别人建议。或者,你可以使用初始化事件,而不是

One work around is to use a Boolean to flag whether you have already initialized your control, as suggested by others. Alternatively, you may be able to use the Initialized event instead.

这篇关于一个WPF用户控件火灾Loaded事件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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