【发布时间】:2012-11-12 03:38:35
【问题描述】:
为了清理我的代码,我试图将我的 app.xaml 拆分为单独的资源字典。这在运行时有效,但在设计时无效:
在 app.xaml 中截取
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/;component/Theme/Colors.xaml" />
<ResourceDictionary Source="/;component/Theme/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Colors.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
</ResourceDictionary>
样式.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="StatusBar">
<Setter Property="Background" Value="{StaticResource backgroundBrush}" />
</Style>
</ResourceDictionary>
MainWindow.xaml 的截图
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="test" Width="800" Height="600" >
<StatusBar Name="statusBar" DockPanel.Dock="Bottom">
<StatusBarItem Content="{Binding statusMessage}" />
</StatusBar>
DesignView 给出错误: 错误 8“{DependencyProperty.UnsetValue}”不是属性“背景”的有效值。 C:\Datan\DotNet\test\test\MainWindow.xaml 123
如果我像这样将 backgroundBrush 直接放入 app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/;component/Theme/Colors.xaml" />
<ResourceDictionary Source="/;component/Theme/Styles.xaml" />
<ResourceDictionary>
<SolidColorBrush x:Key="backgroundBrush" Color="Gold"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
DesignView 没有问题。
那么有没有办法告诉 DesignView 在哪里可以找到 backgroundBrush,如果这个画笔被放置在一个单独的资源字典中?
【问题讨论】:
-
您的两个 ResourceDictionary 文件是在同一个程序集中还是在不同的程序集中?如果资源字典在同一个程序集中,因为它在我的最后工作正常。
-
主程序集中的所有内容。
标签: wpf xaml resourcedictionary designview