在WPF中,有没有控制卸载之前执行代码的方式...?如可能的卸载事件? [英] In wpf, is there a way to execute code before the control is unloaded...? like maybe an unloading event?

查看:410
本文介绍了在WPF中,有没有控制卸载之前执行代码的方式...?如可能的卸载事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个WPF用户控件卸载之前执行代码并取消卸载,如果某些条件得到满足,并保持控制在用户界面的当前状态开...

I need to execute code before a wpf user control is unloaded and cancel the unloading if certain conditions are met and keep the control open in its current state in the ui...

有什么办法,我可以做到这一点?我不可能看到像卸载事件东西吗?
谢谢,

Is there any way I can accomplish this? I couldnt see anything like unloading event? Thanks,

推荐答案

卸载被激发时控从WPF可视化树移除。至于我已经能够阅读没有退出事件,因为,我认为,在Windows窗体。但是,卸载并不意味着该控件被破坏,只是它是从视觉树中移除。

Unloaded is fired when the control is removed from the WPF visual tree. As far as I've been able to read there is no "Unloading" event as there is, I think, in Windows Forms. But, "Unloaded" doesn't mean that the control is destroyed, just that it's removed from the visual tree.

请给控件的引用在一个单独的地方在你的代码,用元数据的约其父控件一点点一起。您可以通过存储到属性在初始化事件处理程序的引用可能收集的元数据。

Keep a reference to the control in a separate place in your code, along with a little bit of metadata about its parent control. You can probably collect that metadata by storing a reference to the Parent property in your Initialized event handler.

然后,当卸载被调用,使你的测试在卸载事件处理程序,如果你的条件得到满足,重新插入控制到逻辑树。在 ContentControl中类有一个明确的的AddChild 受保护的方法,你可以调用。

Then, when Unloaded is called, make your tests in the Unloaded event handler, and if your conditions are met, re-insert the control into the logical tree. The ContentControl class has an explicit AddChild protected method you could call.

有可能是一些副作用需要提防;根据该文件,卸载主题时,在操作系统级改变,当WPF可视化树本身进行重构叫。

There are probably some side effects to watch out for; According to the documentation, Unloaded is called when themes are changed at the OS level, when the WPF visual tree reconstitutes itself.

这篇关于在WPF中,有没有控制卸载之前执行代码的方式...?如可能的卸载事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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