我正在使用.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>
应用程式
<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>
最佳答案
我删除了这行代码
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>
关于c# - WPF资源字典在 namespace 中不存在?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33477705/