I've found more than one reference to a VisibleChanged event on MSDN pages that state it is for the Windows Phone 8 platform. However, when I try to access it via Intellisense for either the top level user control I'm building (using the "this" keyword), or for the LayoutRoot grid, I don't see it. I did a full search via the Object Browser and I don't see anything there either. Where is it? I need to perform certain task only when the user control is visible, and I need them to stop when it is not.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visiblechanged(v=vs.110).aspx
Your reference refers to Windows Form apps for Windows, not Windows Phone. The property you're asking about on Windows Phone is
Visibility
(notVisible
) so you should be looking forVisibilityChanged
--but that does not exist.You could, however, create your own by subclassing the control for which you want the event then use your new control. For example:
Or, of course, if you have complete control of the control's source code, don't bother with the inheritance.
If you want to have
VisibilityChanged
event for arbitrary controls, there is a slightly complicated workaround. First, create a wrapper class around that control that will have its own Visibility property and that is bound to the target's Visibility property. When you have that, you can listen for notifications.First, the extension method:
Helper event args class:
And now for the actual wrapper:
As you can see, we listen for the changes in the target's dependency property and when we detect a change, we fire our own event. Using it is very simple: