x:Bind image with null string

[亡魂溺海] 提交于 2019-11-30 06:46:58

If you use x:Bind, the Source of the Image needs to bind to a property of the exact same type ImageSource (e.g. BitmapImage) instead of string, otherwise it will throw a compile-time error, which is exactly a compile-time binding is supposed to do. The old binding allows strings 'cause it uses Reflection to resolve the type for you during run-time.

Turns out my explicit type theory was wrong (thanks to @igrali for pointing it out). The Source does take a string as long it's is not null or ''. So it leaves us two options to fix this.

Option 1

Keep your uri as a string, but do a check in your vm, once it's null or '', return some dummy text (even returning an a letter x would work!).

Option 2

Change the uri from a string to a BitmapImage. Then you can use TargetNullValue and FallbackValue to handle nulls and invalid bindings.

... FallbackValue='http://Assets/SplashScreen.png' TargetNullValue='http://Assets/SplashScreen.png'}"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!