JokerMartini
JokerMartini

Reputation: 6147

wpf resource dictionary does not exist in namespace?

I'm using .net 4.5.2 I do not have a clue why my project continues to say the resource is missing. I've paced my mainWindow.xaml inside a folder and repathed the app to target its location. What is really odd is the fact that it displays correctly in visual studio but when i try to compile it errors out saying the 'themes' namespace does not exist in the project.

enter image description here

Severity Code Description Project File Line Error CS0234 The type or namespace name 'Themes' does not exist in the namespace 'WpfApplication1' (are you missing an assembly reference?) WpfApplication1 C:\Users\jmartini\Projects\wpf_Styling_4.5.2\WpfApplication1\WpfApplication1\obj\Debug\View\MainWindow.g.i.cs 33

Here is my code...

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="250"
        WindowStartupLocation="CenterScreen">
    <DockPanel>
        <Button Content="Push It!" Width="70" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </DockPanel>
</Window>

JMStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication1.Themes">

    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
    </Style>

</ResourceDictionary>

App.xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="View/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/JMStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Solution to get it working... I removed this line from the style page:

xmlns:local="clr-namespace:WpfApplication1.Themes"

original

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
    </Style>

</ResourceDictionary>

Upvotes: 3

Views: 5052

Answers (3)

Esteven
Esteven

Reputation: 1

For some reason Visual Studio doesn't recognize namespaces of folders with only Resource Dictionary files. If you add another file in that folder like a User Control you would notice that the namespace would be recognized. The solution is to remove the line of "local" namespace:

xmlns:local="clr-namespace:WpfApplication1.Themes

because we will mention the resourse with

<ResourceDictionary Source="Themes/...

Without needing the namespace, in difference when we create User Controls that we do need the namespace in the code of the file where they're used

Upvotes: 0

JokerMartini
JokerMartini

Reputation: 6147

I removed this line of code

xmlns:local="clr-namespace:WpfApplication1.Themes"

from the style page file contents below...

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication1.Themes">

    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
    </Style>

</ResourceDictionary>

Upvotes: 1

BSG
BSG

Reputation: 2104

Microsoft.Windows.Themes is found in the theme-specific PresentationFramework assemblies. You'll need to add a reference to one of the following depending on which one is referenced in your XAML:

PresentationFramework.Aero.dll
PresentationFramework.AeroLite.dll
PresentationFramework.Classic.dll
PresentationFramework.Luna.dll
PresentationFramework.Royale.dll

Upvotes: 0

Related Questions