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