34

I define my settings and styles in a 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>

When I run my application, I get the error:

'Cannot create unknown type '{clr-namespace:Kavand.UI.Properties}Settings'.' Line number '6' and line position '14'.

amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • Is it a compile error or a runtime error? Does removing the first three setters allow the application to run? – Adrian May 27 '11 at 19:36
  • 2
    It is a runtime error, no, just when I remove section that contains definition, application will be compiled – amiry jd May 27 '11 at 19:37

4 Answers4

54

I had set the file's "Build Action" property to "Resource". When I changed it to "Page" the problem was resolved.

default
  • 11,485
  • 9
  • 66
  • 102
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 3
    Waaat? This worked for me too. But the documentation explicitly says to make sure your resource dictionaries have "Resource" build actions. – Kyle Delaney Jun 01 '18 at 17:43
51

Keep your "Build Action" property to "Resource" and just change this row:

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

with this:

xmlns:properties="clr-namespace:Kavand.UI.Properties;assembly=Kavand.UI"
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Vladimir Trifonov
  • 1,395
  • 11
  • 9
  • 1
    I set the build action back to Resource and it worked even without the namespace change. Gaaah! This problem is so fickle. What is up with Visual Studio? – Kyle Delaney Jun 01 '18 at 17:45
  • Anyone knows when to use Resource and when to use Page? In some other articles using Resource was recommended to solve other mysterious behaviour of user control libraries. – RudolfJan Feb 11 '19 at 20:01
4

Just posting another potential solution, because i just recently stumbled over this exception.

It could be that your referenced class-cefinition (in your case Kavand.UI.Properties.Settings) does not use public-access-modifier.

So in my case i could solve this problem by writing public before the class definition.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • Thank you! This was the problem I had that lead me to this question. My class was "internal". Changing it to "public" solved my issue. +1 – Nick Oct 17 '20 at 18:24
3

As a variation on other answers & comments, this worked for me:

  • Set the file's "Build Action" property to "Page".

  • Build (problem gone - but...)

  • Set the file's "Build Action" property to "Resource".

  • Build

  • Problem still gone!

Seems like a VS bug to me.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81