如何确定视图的可见性. [英] How to determin view visibility.

查看:106
本文介绍了如何确定视图的可见性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一种情况,我需要每5秒刷新一次视图.我正在构建wpf应用程序并实现MVVM模式,因此视图模型将获取新数据并更新绑定在视图上的集合.这样做没有问题,但是现在的挑战是用户可以打开该视图的多个实例,而我只需要为该用户当前可见的视图进行更新.因此,如何确定用户控件对用户可见.

谢谢.

Ali

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

推荐答案

我不认为您是说您在意UIElementIsVisible == false含义上的不可见状态(您可以随时检查该状态) ).您还需要处理事件UIElement.IsVisibleChanged,以将其更改为true后立即进行更新.这是微不足道的.而且我认为您宁愿避免隐藏未关闭的视图.

当一个窗口被其他窗口覆盖时,您似乎想禁用更新.

即使您也可以做到.我建议您避免处理它,并为所有此类视图执行更新.正确实现起来并不容易,并且实际上并不需要实际实现.在现实生活中,如果用户创建多个视图,则他/他想一直从一个视图切换到另一个视图的过程中一直看到所有视图.每次打开视图时都需要更新视图,或者只需拖动一个阻塞视图的窗口即可将其移动到可见性中,实际上,它与一直更新视图几乎相同.即使用户打开了太多视图,也需要按照工作方式设计系统.在这种情况下,系统不应中断;但是性能差的用户的惩罚"是完全可以接受的.

如果您尝试检测所有被其他窗口部分或完全覆盖的视图暴露给用户的所有情况,则您将需要处理所有窗口的运动,而不仅仅是处理由应用程序创建的那些窗口.但是您的应用程序与其他Windows无关!因此,您将需要使用Windows挂钩和P/Invoke( http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms632589%28v=vs.85%29.aspx [http://en.wikipedia.org/wiki/PInvoke [ ^ ], http://msdn.microsoft .com/en-us/library/Aa712982 [ ^ ]).

由于该钩子应该是全局钩子,因此您不能将其安装在.NET应用程序中;请参见参考资料.如果必须安装在本机DLL中.该挂钩应使用一些IPC与您的应用程序进行通信.仅触发事件来告诉您的应用程序就足够了:发生了某些事情,使您的视图无效".在这种情况下,您应该使用原始Windows API的P/Invoked Windows函数来扫描所有可能向用户公开视图的窗口,进行计算并在需要时更新视图,并允许您进行轮询更新.在相反的情况下限制您轮询更新.所有应用程序可能需要花费更多的开发时间.

我建议你不要去那里.

—SA
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


这篇关于如何确定视图的可见性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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