简体   繁体   中英

Remove WPF element while program is running with C#

I was wanting to get some information from the user before they get to see or use the program. Currently I just have a black rectangle hiding my GUI with a text box in front of it. When the information is entered into the box I have it and the rectangle's visibility set to collapsed.

I only need them at the very start of the program and I am assuming they are still existing and taking memory while collapsed. Is there a way to remove the elements completely once I have no use for them?

Why not have a modal window that pops up, then when its closed it'll be removed form the visual/logical tree automatically

for example

Window win = new MyPopupWindow();
win.ShowDialog();

Using some sort of standard UI element like a modal windows suggested above or the ChildWindow in silverlight would be better.

However, collapsed elements shouldn't cause you any memory trouble, they are taken out of the rendering pipeline. Opacity = 0 will stick around though. If you really want to remove it, the parent Panel (eg Grid) has a Children collection with a Remove() method.

You can do this with a technique meant for splash screens, like in the following example.

Two differences:

  1. Use ShowDialog() instead of Show() to make window modular (always on top and freezes execution).

  2. Run on main.thread, not on separate thread.

Example:

Hide WPF Window Until Fully Loaded

However, you may want to consider rethinking your approach, since it does not seem user friendly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM