はじめに
WPFを使っていて、xamlで以下のようなコーディングをした際のことです
<Image Source="{Binding ImageFilePath}"
Width="100"
Height="100"/>
.NetFramework4.8のときは、このImageFilePathがnullや空文字でもすぐに表示されたのですが、.NET5に変更した際に、エラーが起きて、表示がとても遅くなってしまいました。
エラーの内はこんな感じですね
例外がスローされました: 'System.NotSupportedException' (System.ComponentModel.TypeConverter.dll の中)
例外がスローされました: 'System.NotSupportedException' (PresentationCore.dll の中)
System.Windows.Data Error: 23 : Cannot convert '' from type '' to type 'System.Windows.Media.ImageSource'
対策
対策といえばググりましょうということで、いろいろと調べましたが、UWPのImageのSourceにnullや空文字をx:Bindしたときの対処方法(対処できない)という記事で、TargetNullValueを設定すればよさそうということがわかりました。
さらに調べて、TargetNullValue={x:Null} とすることで対策できました。
<Image Source="{Binding ImageFilePath, TargetNullValue={x:Null}}"
Width="100"
Height="100"/>
以上、できてしまえば簡単ですが、忘れてしまいそうだったので、備忘録もかねて記事とさせていただきました。