0

我在我的项目中使用了两次列表框样式,所以我想在资源字典中使用它。这个列表框样式有两个值转换器,所以我在同一个资源文件中实例化了转换器。在运行时虽然它说“无法声明未知类型”,尽管在 mainwindow.xaml 文件中使用它们时相同的转换器声明有效。

有人有想法吗?

4

1 回答 1

4

在将转换器移动到资源部分下的 App.xaml 文件之前,我遇到了同样的问题。这是我的示例 App.xaml 文件,我刚刚为示例创建了一个名为 TextConverter 的转换器。

如果您使用其他 ResourceDictionaries,则有两种方法可以在所有情况下都使用 ResourceDictionary.MergedDictionaries,如下所示:

<Application x:Class="WPFFeatureSample_Application.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter>
            </ResourceDictionary>
            <ResourceDictionary Source="Resources/ControlDictionary.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

如果您没有其他资源文件,它将如下所示:

<Application x:Class="WPFFeatureSample_Application.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter>
    </ResourceDictionary>
</Application.Resources>

一般使用转换器和资源字典时,一个有趣的参考是:http: //www.dotnetdude.com/2011/01/23/ResourceDictionariesAreMakingMyHairHurt.aspx

于 2012-01-06T21:09:38.263 回答