【问题标题】:Xaml Design View can't find Background BrushXaml 设计视图找不到背景画笔
【发布时间】: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


【解决方案1】:

这不是StaticResource 的问题。它需要使用分层的共享\合并\直接资源字典显式解析资源键。

有两种选择...

Styles.xaml中合并Colors.xaml字典

Styles.xaml 中使用DynamicResource 引用bursh。

【讨论】:

  • -即使使用 StaticResource 也能正常工作。所以,我想问题出在其他地方。
  • 好的,DynamicResource 解决了这个问题。但是要理解,请你告诉我有什么区别,会有什么影响吗?
  • 动态资源直到运行时才使用其“x:Key”解析。 DynamicResources 如果过度使用可能会使您的应用程序变慢。但它们是 gr8 动态更改应用程序皮肤(彩色主题)。
  • 好的,所以我会选择你的第一个选项,因为我不想要一个缓慢的应用程序,因为 DesignView 无法解析静态资源。
  • 只是好奇地问一下,你有多少这样的资源......几百个就可以了......只要它们是画笔或颜色(轻量级)和只要我们不改变合并\共享\引用的字典。
【解决方案2】:

如果资源与 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

【讨论】:

  • 没有帮助。我的资源字典都驻留在主组件中,我只在 DesignView 中收到此错误。在运行时一切正常。
【解决方案3】:

试试

{StaticResource ApplicationPageBackgroundThemeBrush}

为你的状态栏背景值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多