错误:'无法创建未知类型'{clr-namespace:NameSpace.Properties}Settings'。

34

我在一个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-namespace:Kavand.UI.Properties}Settings'。'第6行位置14。


这是编译错误还是运行时错误?如果删除前三个setter,应用程序是否可以运行? - Adrian
2
这是一个运行时错误,不,只有当我删除包含定义的部分时,应用程序才能编译。 - amiry jd
4个回答

54

我将文件的“Build Action”属性设置为“Resource”。当我将其改为“Page”时,问题得到了解决。


3
这也对我起作用了,但文档明确说明要确保您的资源字典具有“Resource”构建操作。 - Kyle Delaney

51

保持您的“生成操作”属性为“Resource”,只需更改此行:

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

有了这个:

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

1
我将构建操作设置回资源,即使没有更改命名空间,它也可以正常工作。这个问题真是棘手。Visual Studio 怎么了? - Kyle Delaney
有谁知道何时使用 Resource 和何时使用 Page? 在其他一些文章中,使用 Resource 被建议用于解决用户控件库的其他神秘行为。 - RudolfJan

4

我在这里提供另一种可能的解决方案,因为最近我也遇到了这个异常。

可能是因为你引用的类定义(在你的情况下是 Kavand.UI.Properties.Settings)没有使用 public 访问修饰符。

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


谢谢!这就是我遇到的问题,导致我提出了这个问题。我的类是“internal”。将其更改为“public”解决了我的问题。+1 - Nick

3
作为其他答案和评论的变化,这对我有效:
- 将文件的“Build Action”属性设置为“Page”。 - 构建(问题解决了,但是...) - 将文件的“Build Action”属性设置为“Resource”。 - 构建 - 问题仍然消失!
看起来像是一个VS的bug。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接