Xaml Design View找不到Background Brush

时间:2011-10-12 13:01:24

标签: wpf xaml resourcedictionary designview

为了清理我的代码,我正在尝试将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>

Styles.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}'不是属性'Background'的有效值。 C:\ Daten \ 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,如果这个画笔放在一个单独的资源字典中?

3 个答案:

答案 0 :(得分:5)

StaticResource不是问题。它需要使用shared \ merged \ direct resource dictionaries显式解析资源键。

有两种选择......

Colors.xaml

中合并Styles.xaml字典

OR

Styles.xaml中的

使用DynamicResource引用bursh。

答案 1 :(得分:1)

如果资源与MainWindow所在的程序集不同,并且一个字典引用另一个字典。在那种情况下,参考未得到解决。如果您的目标框架是4.0,则已在Microsoft站点报告此错误。但是他们为它提供了一种解决方法。只需在资源字典中添加空样式,它就可以正常工作 -

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/;component/Theme/Colors.xaml" />
        <ResourceDictionary Source="/;component/Theme/Styles.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="{x:Type Window}"/>
  </ResourceDictionary>
</Application.Resources>

如需进一步参考,请查看此链接 - https://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references#details

答案 2 :(得分:1)

尝试

{StaticResource ApplicationPageBackgroundThemeBrush}

表示状态栏背景值