Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Xaml设计视图可以';找不到背景笔刷_Wpf_Xaml_Resourcedictionary_Designview - Fatal编程技术网

Wpf Xaml设计视图可以';找不到背景笔刷

Wpf Xaml设计视图可以';找不到背景笔刷,wpf,xaml,resourcedictionary,designview,Wpf,Xaml,Resourcedictionary,Designview,为了清理我的代码,我尝试将app.xaml拆分为单独的资源字典。这在运行时有效,但在设计时无效: 在app.xaml中截取 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/;component/Theme/Colors

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

style.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?

这就是静态资源的问题所在,不是吗。它需要使用分层的共享\合并\直接资源字典显式解析资源密钥

有两种选择

Colors.xaml
字典合并到
style.xaml


Styles.xaml
中,如果资源位于不同于MainWindow所在的程序集中,并且一个字典引用另一个字典,则使用
DynamicSource
引用bursh。在这种情况下,引用未解析。如果您的目标框架是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>

试试看

{StaticResource ApplicationPageBackgroundThemeBrush}

对于状态栏背景值,您的两个ResourceDictionary文件是在同一个程序集中还是在不同的程序集中?因为如果资源字典在同一个程序集中,那么它在我这边可以正常工作。主程序集中的所有内容。-即使使用StaticResource,它在我这边也可以正常工作。所以,我想问题出在其他地方。好的,DynamicSource解决了这个问题。但是为了理解,请告诉我区别是什么,会有什么影响?dynmaic资源直到运行时才使用其“x:Key”解析<代码>动态资源
s如果使用过度,可能会使应用程序运行缓慢。但它们是gr8,用于动态更改应用程序外观(彩色主题)。好吧,我将选择您的第一个选项,因为我不希望因为DesignView无法解析静态资源而导致应用程序运行缓慢。我只是好奇地问一下,您有多少这样的资源。。。几百就行了。。。只要它们是画笔或颜色(轻量级),只要我们不经常更改合并\共享\引用的词典,它们就不会成为问题。没有帮助。我的资源字典都驻留在主组件中,我只在DesignView中遇到此错误。运行时一切正常。