x:Bind image with null string

后端 未结 1 1546
天命终不由人
天命终不由人 2020-12-30 04:16

In XAML I have the following line:



        
相关标签:
1条回答
  • 2020-12-30 04: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'}"
    
    0 讨论(0)
提交回复
热议问题