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 u开发者_如何学Pythonse 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:
Use ShowDialog() instead of Show() to make window modular (always on top and freezes execution).
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.
精彩评论