资源字典中 valueconverter 未知

发布于 2024-12-24 16:13:09 字数 151 浏览 2 评论 0原文

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

有人有主意吗?

i'm using a listbox style twice in my project so i want to use it in a resource dictionary. This listbox style has two value converter's in it, so i instantiated the converters in the same resource file. at runtime though it says that the 'unknown type cannot be declared' although the same converter declarations work when using them in the mainwindow.xaml file.

Anyone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-12-31 16:13:09

我遇到了同样的问题,直到我将转换器移动到资源部分下的 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

I had the same issue until I moved the converter into the App.xaml file under the resources section. This is my sample App.xaml file, I just created a converter named TextConverter for the sample.

There are two ways to do this if you are using other ResourceDictionaries you have to use the ResourceDictionary.MergedDictionaries in all cases as seen below:

<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>

If you do not have other resource files it would look like this:

<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>

One interesting reference when using converters and Resource Dictionaries in general is this:
http://www.dotnetdude.com/2011/01/23/ResourceDictionariesAreMakingMyHairHurt.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文