Click here to Skip to main content
16,004,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a scenario in which I need to refresh the view after every 5 seconds. I am building wpf application and implementing MVVM pattern, so view model will get the new data and update the collection which is bind on the view. This is done with not problem but now the challenge is user can open multiple instances of this view and I only need to do the update for the view which is currently visible to the user. So how I can determine that usercontrol is visible to the user.

Thanks.

Ali
Posted

1 solution

I don't think you mean you care of the invisible state in the sense of IsVisible == false of UIElement (which you always can check up). You would also need to handle the event UIElement.IsVisibleChanged to update immediately after it is changed to true. This is trivial enough; and I think you would rather avoid hiding the views if they are not closed.

It looks like you want to disable updates when a window is covered by other windows.

Even though you can do it, too. I would advise you to avoid handling it and perform the update for all such views. It is not easy to implement correctly and is not really needed to practical purpose. In real life, if a user creates more than one view, she/he wants to actually see all the views all the time of switch from one view to another one. As you need to update the view each time it is switched on or simply moved into visibility by dragging a window which was blocking your view, in practice it's nearly the same as updating it all the time. You need to design the system the way it works even if the user opens too many views; in this case the system should not break; but the "punishment" of the user with bad performance is perfectly acceptable.

If you try to detect all situation where your view partially or fully covered by some other window gets exposed to the user, you will need to handle motion of all windows, not just those created by your application. But your application is agnostic to other windows! So, you will need use Windows Hooks and P/Invoke (http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^], http://en.wikipedia.org/wiki/PInvoke[^], http://msdn.microsoft.com/en-us/library/Aa712982[^]).

As the hook should be global, you cannot install it in your .NET application; if has to be installed in a native DLL. This hook should use some IPC to communicate with your application. It would be enough just to trigger the event to tell you application: "something was moved, invalidate your views". On this event, yous should use P/Invoked windows functions of raw Windows API to scan all windows which can potentially expose your views to the user, calculate it and update the views if needed and allow your polling updates; in the opposite situation; throttle you polling updates. It might take more of your development time that all your application.

I would advise you not to go there.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900