x:Bind image with null string x:Bind image with null string windows windows

x:Bind image with null string


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'}"