【问题标题】:wpf resource dictionary does not exist in namespace?命名空间中不存在 wpf 资源字典?
【发布时间】:2015-11-02 12:34:22
【问题描述】:

我正在使用 .net 4.5.2 我不知道为什么我的项目继续说资源丢失。我在一个文件夹中调整了我的 mainWindow.xaml 并重新定位了应用程序以定位其位置。真正奇怪的是它在 Visual Studio 中正确显示,但是当我尝试编译它时出错,说项目中不存在“主题”命名空间。

严重性代码描述项目文件行错误CS0234类型或 命名空间名称“主题”在命名空间中不存在 '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>

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      Microsoft.Windows.Themes 可在特定于主题的 PresentationFramework 程序集中找到。您需要添加对以下内容之一的引用,具体取决于 XAML 中引用的内容:

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

      【讨论】:

      • 我正在创建自己的自定义主题。当我将自定义主题添加到 app.xaml 时出现错误。
      • 您需要将主题资源字典添加到 MergedDictionaries 集合中。示例如下所示。
      • 您是否检查了程序集和项目之间的 .NET 版本(.NET 4.5.2 客户端配置文件和 .NET 非客户端)
      • 我在哪里检查这些?
      • 如果您认为您正在运行最新版本的 VS2012 并且仍然遇到此问题,如果您使用 Windows Update 进行了更新,请确保已应用更新,您需要告诉 VS2012 应用更新。
      【解决方案3】:

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

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

      因为我们会用

      提到资源
      <ResourceDictionary Source="Themes/...
      

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

      【讨论】:

        猜你喜欢
        • 2014-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多