Xaml Design View找不到Background Brush

MTR*_*MTR 0 wpf xaml designview resourcedictionary

为了清理我的代码,我正在尝试将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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

剪切了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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

DesignView没有问题.

那么有没有办法告诉DesignView在哪里找到backgroundBrush,如果这个画笔放在一个单独的资源字典中?

WPF*_*-it 5

这不是问题StaticResource.它需要使用shared\merged\direct资源字典显式地解析资源密钥.

有两种选择......

合并Colors.xaml词典Styles.xaml

要么

Styles.xaml 参考bursh使用DynamicResource.