2015-11-02 99 views
1

我正在使用.net 4.5.2 我不知道为什么我的项目继续说资源丢失。我已经将我的mainWindow.xaml放在一个文件夹中,并重新编写应用程序以定位它的位置。真奇怪的是,它在Visual Studio中正确显示的事实,但是当我试图编译错误,说'主题'命名空间不存在于项目中。wpf资源字典在命名空间中不存在?

enter image description here

严重性代码说明项目文件行错误CS0234类型或 命名空间名称“主题”不会在命名空间 “WpfApplication1”存在(是否缺少程序集 参考?)WpfApplication1Ç :\用户\ jmartini \项目\ wpf_Styling_4.5.2 \ WpfApplication1 \ WpfApplication1 \ OBJ \调试\查看\ MainWindow.gics 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> 

回答

0

我删除了这行代码

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

虽然我正在创建自己的自定义主题。当我将自定义主题添加到app.xaml时出错。 – JokerMartini

+0

您需要将主题资源字典添加到MergedDictionaries集合。示例如下所示。 <资源字典> <! - 其他资源去这里 - > < vm:ViewModelLocator x:Key =“ViewModelLocatorKey”/> BSG

+0

我在上面的代码中做了这个。在app.xaml中。 – JokerMartini

相关问题