开发者

Silverlight define custom Dictionary in XAML

You can define custom Dictionaries in XAML in the following way.

// *.cs
public class MyDictionary : Dictionary<string, int> { }

// *.xaml
<sys:Int32 x:Key="Zero">0</sys:Int32>

But what about other Key-Types? You can use the extended element usage in XAML 2009:

<object>
    <x:Key>keyObject</x:Key>
</object>

However, this isn't supported in Silverlight yet.

Is there anything I can do? I want to use a custom Dictionary with S开发者_运维百科ystem.Type as Key-Type.


Silverlight does not support generics in XAML. You also can't create an arbitrary typed object directly because SL would not know how to instantiate it.

You should consider Chris Haines's suggestion of having your dictionary as a view model because it's better practice to have such things be defined outside of xaml.

You can't really do this in Silverlight (see Is there any way to instantiate a 'Type' in Silverlight XAML?)

You could wrap your dictionary and input strings and let it convert them to types internally, but REALLY, why can't you just use the string representation of Types? Do Type.FullName

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜