如何防止 Silverlight 视图中的闪烁? [英] How can I prevent flicker in Silverlight views?

查看:33
本文介绍了如何防止 Silverlight 视图中的闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silverlight UI 中遇到了一个相当普遍的情况,其中各种元素的可见性绑定到 ViewModel 上的某些属性.例如,我可能有这样的控件:

I've got a fairly common situation in my Silverlight UI where the Visibility of various elements is bound to some property on the ViewModel. For instance, I may have a control like this:

<myapp:WarningView Visibility="{Binding IsInWarningState}" />

如果警告状态为真,我们想显示控件,如果不是,我们不显示.问题是我有很多情况,页面渲染并显示所有内容,包括WarningView控件,然后一秒钟后隐藏视图,这看起来很笨拙.

If the warning state is true, we want to show the control, if not, we don't. The problem is that I've got many situations where the page renders and displays all the content, including the WarningView control, and then a second later hides the view, which looks really clumsy.

我认为正在发生的事情是 View 加载并向服务器进行异步调用以获取数据,但在此之前,没有 DataContext,因此 WarningView 没有绑定到的任何内容,并使用默认值和默认值是显示控件.

I think what's happening is that the View loads and makes an async call to the server to get the data, but until that happens, there's no DataContext and so nothing for the WarningView to bind to, and uses defaults, and the default is to show the control.

让视图实例化自己的视图模型似乎是最干净的,或者至少我现在正在这样做.而且由于调用是异步的,因此当您想要显示某些内容但还没有要显示的真实数据时,总会有一些间隔.我可以想象制作一个具有更好默认值的模拟 ViewModel,但这似乎需要在整个项目范围内完成很多工作.

It seems cleanest for the view to instantiate its own view model, or at least that's what I'm doing now. And since the call is async, there could always be some interval when you want to be showing something but you don't have the real data to show yet. I could imagine making a mock ViewModel with better defaults, but that seems like a lot of work to do project wide.

那么解决这个问题的最佳方法是什么?

So what's the best way around this problem?

推荐答案

好吧,在我放弃这个很久之后,我自己偶然发现了答案:在绑定中你可以指定一个 FallbackValue 和/或 TargetNullValue,它们指定一个当您绑定的事物无法获得或为空时使用的值.我的问题是 View 加载并创建了 ViewModel,并且在 View 可见和绑定完成之间的瞬间,控件可见,然后绑定将它们折叠起来,这会导致闪烁.通过设置回退值,我可以确保控件处于折叠状态,然后仅在绑定时打开,然后它们保持打开状态.无闪烁.

Well, long after I gave up on this, I stumbled across the answer myself: in the binding you can specify a FallbackValue and/or TargetNullValue, which specify a value to use when the thing you're binding to can't be obtained or is null. My problem was that the View loads and creates the ViewModel, and for the instant between when the View is visible and when the binding is complete, the controls are visible, and then the binding collapses them, and this causes the flicker. By setting Fallback values, I can make sure that the controls are Collapsed, and then turn on only when bound, and then they stay on. No flicker.

这篇关于如何防止 Silverlight 视图中的闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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