Wpf 用xaml代码创建字典

Wpf 用xaml代码创建字典,wpf,xaml,dictionary,Wpf,Xaml,Dictionary,此代码给出错误的原因: 命名空间xmlns中不存在名称词典:scg=clr命名空间:System.Collections.Generic;assembly=mscorlib <Window x:Class="Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/

此代码给出错误的原因: 命名空间xmlns中不存在名称词典:scg=clr命名空间:System.Collections.Generic;assembly=mscorlib

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
        xmlns:test="clr-namespace:Test"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <scg:Dictionary x:Key="CountryPopulRecords" TypeArguments="sys:String,sys:String"></scg:Dictionary>
    </Window.Resources>

    <Grid>

    </Grid>
</Window>    
参考资料:

也许这有助于:

您不能在XAML中直接使用Dictionary类,因为无法指定泛型类型参数,这在下一版本的XAML中是可能的,但在VS2010 WPF designer中不受支持。。。至少在最初的版本中没有


但是,您可以声明一个从Dictionary继承的非泛型类,并在XAML中使用它。

我使用了基于此MSDN链接的代码:用两个字符串类型参数实例化一个新字典。