【问题标题】:Error: 'Cannot create unknown type '{clr-namespace:NameSpace.Properties}Settings'.'错误:“无法创建未知类型 '{clr-namespace:NameSpace.Properties}Settings'。”
【发布时间】:2011-09-03 15:28:15
【问题描述】:

我在 ResourceDictionary 中定义我的设置和样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:properties="clr-namespace:Kavand.UI.Properties">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
            <properties:Settings x:Key="settings" />
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="PopupMenu_StackPanel">
        <Setter Property="TextBlock.FontSize" Value="{Binding Source={StaticResource settings}, Path=Default.Font_Menu_Size}" />
        <Setter Property="TextBlock.FontFamily" Value="{Binding Source={StaticResource settings}, Path=Default.Font_Menu_Family}" />
        <Setter Property="TextBlock.FontWeight" Value="{Binding Source={StaticResource settings}, Path=Default.Font_Menu_Weight}" />
        <Style.Resources>
            <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource KavandMenuItem}">
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter Property="IsEnabled" Value="false" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsChecked" Value="True" />
                            <Condition Property="IsHighlighted" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Foreground" Value="{DynamicResource K_Brush_Gray}" />
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>
</ResourceDictionary>

当我运行我的应用程序时,我得到了错误:

'无法创建未知类型 '{clr-命名空间:Kavand.UI.Properties}设置'。' 行号“6”和行位置 '14'。

【问题讨论】:

  • 是编译错误还是运行时错误?删除前三个 setter 是否允许应用程序运行?
  • 这是一个运行时错误,不,当我删除包含定义的部分时,应用程序将被编译

标签: wpf xaml styles application-settings wpf-style


【解决方案1】:

作为其他答案和 cmets 的变体,这对我有用:

  • 将文件的“构建操作”属性设置为“页面”。

  • 构建(问题消失了 - 但是...)

  • 将文件的“构建操作”属性设置为“资源”。

  • 构建

  • 问题依旧!

对我来说似乎是一个 VS 错误。

【讨论】:

    【解决方案2】:

    刚刚发布了另一个潜在的解决方案,因为我最近偶然发现了这个异常。

    可能是您引用的 class-cefinition(在您的情况下为 Kavand.UI.Properties.Settings)不使用 public-access-modifier。

    所以在我的情况下,我可以通过在类定义之前写 public 来解决这个问题。

    【讨论】:

    • 谢谢!这是我遇到的问题,导致我提出这个问题。我的课是“内部的”。将其更改为“公共”解决了我的问题。 +1
    【解决方案3】:

    将“构建操作”属性保留为“资源”,然后更改此行:

    xmlns:properties="clr-namespace:Kavand.UI.Properties"
    

    用这个:

    xmlns:properties="clr-namespace:Kavand.UI.Properties;assembly=Kavand.UI"
    

    【讨论】:

    • 我将构建操作设置回 Resource,即使没有更改命名空间,它也能正常工作。啊啊!这个问题太变态了。 Visual Studio 怎么了?
    • 有谁知道什么时候使用Resource,什么时候使用Page?在其他一些文章中,建议使用 Resource 来解决用户控件库的其他神秘行为。
    【解决方案4】:

    我已将文件的“构建操作”属性设置为“资源”。当我将其更改为“Page”时,问题就解决了。

    【讨论】:

    • 哇?这对我也有用。但是文档明确指出要确保您的资源字典具有“资源”构建操作。
    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 2013-01-02
    • 2022-10-17
    • 2021-09-10
    • 2022-08-14
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多