简体   繁体   English

我应该始终使用BindingBase.FallbackValue吗?

[英]Should I always use BindingBase.FallbackValue?

Greetings, 问候,

As I create WPF application I obviously use lots of bindings. 在创建WPF应用程序时,我显然使用了大量绑定。 However I figured out that generally I don't use FallbackValue property. 但是我发现通常我不使用FallbackValue属性。

My question is: should I always set Fallback value event if it should be: FallbackValue="" ? 我的问题是:如果应该设置Fallback value事件,是否应该始终设置:FallbackValue =“”?

What is the impact in memory/performance/others when using/not using FallbackValue? 使用/不使用FallbackValue会对内存/性能/其他产生什么影响?

You should hardly ever use FallbackValue. 您几乎不应该使用FallbackValue。

If you've set everything up correctly, then the databinding should never fail, so the fallbackvalue should never be seen. 如果正确设置了所有内容,则数据绑定永远不会失败,因此fallbackvalue永远不会被看到。

The biggest performance problem that will hit you here is the cost of a failed databinding (these usually involve exceptions and are much costlier than sucessful bindings). 此处会遇到的最大性能问题是数据绑定失败的代价(这些失败通常涉及异常,并且比成功的绑定要昂贵得多)。 Making use of FallbackValue could hide such performance problems. 利用FallbackValue可能会隐藏此类性能问题。 Therefore I'd argue that using FallbackValue is generally going to make performance worse (but as always, it depends) 因此,我认为使用FallbackValue通常会使性能变差(但始终取决于情况)

I personally recommend providing FallbackValue where you don't depend on the load to provide the functionality unless you are certain that the load will never fail. 我个人建议提供FallbackValue ,在这种情况下, 除非确定加载永远不会失败, 否则您不依赖于加载来提供功能。 There are multiple ways for a load to fail, and if your image etc. don't really affect functionality, it is harsh to your user to fail the program just because your company's logo happens not to load due to a corrupted file, or something... 加载失败的方式有多种,并且如果您的图片等并没有真正影响功能,那么用户就很难使程序失败,因为您的公司徽标恰巧由于文件损坏或其他原因而无法加载...

However, if you need the resource for your functionality, you should not provide a fallback value but should trap the exception and handle it. 但是,如果您需要功能所需的资源,则不应提供后备值,而应捕获并处理该异常。

FallbackValue can also be used in priority image sources. FallbackValue也可以在优先级图像源中使用。 Here, you fire off an async load of a resource somewhere else (possible over the Internet), and before the load is complete, your fallback value will be available. 在这里,您可以在其他地方(可能通过Internet)触发资源的异步负载,并且在负载完成之前,您的后备值将可用。 For example, you can use the fallback value to provide a place-holder image before your images come back. 例如,您可以使用后备值在图像返回之前提供占位符图像。 And if the image load (off the Internet) fails, you have a place-holder in place instead of a blank space -- sometimes having a blank space is not acceptable because of screen element sizing issues (things will disappear when having no size). 并且,如果图像加载失败(Internet上的失败),则说明您有一个占位符,而不是空白-有时由于屏幕元素尺寸问题,空白是不可接受的(没有大小时,这些东西会消失) 。

As for performance considerations, I have not sensed much degrading of performance due to fallback resources. 至于性能方面的考虑,由于后备资源的原因,我并没有感觉到太多的性能下降。 However, please understand that if the resource is loaded async, then you're essentially loading two resources instead of one -- but the fallback is usually a local resource so impact to performance should be minimal. 但是,请理解,如果资源是异步加载的,那么您实际上是在加载两个资源,而不是一个。但是回退通常是本地资源,因此对性能的影响应该最小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM