アプリ内のほとんどのLabel
オブジェクトは、Web サービスから置き換えることができるようにバインドされています。
置換を Dictionary に保存します。置換は、アプリ用にロードされたクライアントによって異なります。
私が抱えている問題は、ほとんどの場合、ラベルを置き換えないことと、バインディングの失敗によって非常に遅い例外がスローされることです。
次に例を示します: (このコードは正しく機能することに注意してください。一致するものが見つからない場合は処理が遅くなります。)
<Label Content="_Gender"
Helpers:CaptionHelper.PropertyName="Content"
Helpers:CaptionHelper.PropertyValue="{Binding Labels[Gender],
FallbackValue=_Gender}"
Target="{Binding ElementName=cbGender}" />
バインディングが Labels[Gender] を検索しようとすると、出力ウィンドウに次のように表示されます。
System.Windows.Data Warning: 17 : Cannot get 'Item[]' value (type 'String') from 'Labels' (type 'Dictionary`2'). BindingExpression:Path=Labels[Gender]; DataItem='MyViewMV'
(HashCode=63776763); target element is 'Label' (Name=''); target property is 'PropertyValue'(type 'String') TargetInvocationException:'System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException:
The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Params Truncated)
at System.Reflection.RuntimeMethodInfo.Invoke(Params Truncated)
at System.Reflection.RuntimeMethodInfo.Invoke(Params Truncated)
at MS.Internal.Data.PropertyPathWorker.GetValue(Params Truncated)
at MS.Internal.Data.PropertyPathWorker.RawValue(Params Truncated)'
私はこれらをたくさん持っているので、それらすべてを実行するのに約 1 秒かかります。
辞書検索が失敗したときにバインディングが例外をスローしないようにする方法はありますか? それともどういうわけか早く失敗しますか?