我有我的應用程序中的大部分Label
對象綁定,以便它們可以從web服務中替換。WPF詞典綁定失敗是非常緩慢的
我將我的替換存儲在字典中。這些替換取決於爲應用程序加載的客戶端。
我遇到的問題是,大多數時候我不會替換標籤,並且綁定失敗會拋出非常慢的異常。
下面是一個例子:(正確地注意該代碼的功能,它只是緩慢,如果它不能找到匹配)
<Label Content="_Gender"
Helpers:CaptionHelper.PropertyName="Content"
Helpers:CaptionHelper.PropertyValue="{Binding Labels[Gender],
FallbackValue=_Gender}"
Target="{Binding ElementName=cbGender}" />
當結合試圖查找標貼[性別]我得到這個在我的輸出窗口:
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)'
因爲我有很多這樣的,它走大約一整秒通過他們全部運行。
有沒有辦法讓字典查找失敗時綁定不會拋出異常?或者以某種方式失敗更快?
不知道這是否太老了澄清問題,但它在這裏:我似乎無法找到一種方法來返回DependecyProeprty.UnsetValue。假設我有一個'PassthruDictionary',我該如何返回UnsetValue? (它爲我編譯但引發了運行時異常。) –
Vaccano