WPF资源字典在命名空间中不存在?

3
我正在使用.NET 4.5.2,但是我不知道为什么我的项目一直显示资源丢失。我已经将mainWindow.xaml放在一个文件夹中,并重新定位应用程序以针对其位置。真正奇怪的是,在Visual Studio中它显示正确,但是当我尝试编译它时,它会出现错误,说项目中不存在“themes”命名空间。

enter image description here

错误 代码 描述 项目 文件 行号 错误 CS0234 类型或命名空间名称 'Themes' 在命名空间 'WpfApplication1' 中不存在(您是否缺少程序集引用?)WpfApplication1 C:\Users\jmartini\Projects\wpf_Styling_4.5.2\WpfApplication1\WpfApplication1\obj\Debug\View\MainWindow.g.i.cs 33

这是我的代码...

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>

让它工作的解决方案... 我从样式页面中删除了这一行:

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

原始的

<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>
3个回答

1
我删除了这行代码。
xmlns:local="clr-namespace:WpfApplication1.Themes"

从下面的样式页面文件内容...

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

0

Microsoft.Windows.Themes可以在特定主题的PresentationFramework程序集中找到。根据你在XAML中引用的程序集,你需要添加以下其中一个的引用:

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

我正在创建自己的自定义主题。但是,当我将我的自定义主题添加到app.xaml时,它会出现错误。 - JokerMartini
1
您需要将主题资源字典添加到MergedDictionaries集合中。以下是示例。<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="BureauBlack.xaml" /> </ResourceDictionary.MergedDictionaries> </Application.Resources> - BSG
你有检查程序集和项目之间的.NET版本吗?(.NET 4.5.2客户端框架和非客户端.NET) - BSG
我在哪里可以检查它们? - JokerMartini
如果您认为您正在运行最新版本的VS2012,但仍然遇到此问题,请确保已应用更新,如果您使用Windows Update进行了更新,则需要告诉VS2012应用更新。 - BSG
我正在运行2015版本。但是我该在哪里检查程序集和项目中使用的.NET版本? - JokerMartini

0
由于某些原因,Visual Studio无法识别仅包含资源字典文件的文件夹的命名空间。如果您在该文件夹中添加另一个文件,例如用户控件,您会注意到命名空间将被识别。解决方案是删除“本地”命名空间行:
xmlns:local="clr-namespace:WpfApplication1.Themes

因为我们将会提及资源与

<ResourceDictionary Source="Themes/...

不需要命名空间,与我们创建用户控件时需要在使用它们的文件代码中使用命名空间不同


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