Xaml 设计视图找不到背景画笔

发布于 2024-12-09 13:53:35 字数 2508 浏览 0 评论 0原文

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

Snipped of 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呢?

in order to clean up my code, I'm trying to split my app.xaml into seperate resource dictionaries. This works at runtime, but not at design time:

snipped in 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>

Snipped of 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 gives the error:
Error 8 '{DependencyProperty.UnsetValue}' is not a valid value for property 'Background'. C:\Daten\DotNet\test\test\MainWindow.xaml 123

If I put backgroundBrush directly into app.xaml like so:

    <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 has no problems.

So is there a way to tell DesignView where to find backgroundBrush, if this brush is placed into a seperate resource dictionary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫林﹌晚霞¤ 2024-12-16 13:53:35

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

有两个选项...

Styles.xaml 中合并 Colors.xaml 字典

,或者

Styles.xaml 中使用 DynamicResource 引用 bursh

Thats the problem with StaticResource isnt it. It needs the resource key resolved explicitly using shared \ merged \ direct resource dictionaries hierarchically up.

There are two options...

merge Colors.xaml dictionary in Styles.xaml

OR

in Styles.xaml refer the bursh using DynamicResource.

心头的小情儿 2024-12-16 13:53:35

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

In case Resources are in different assemblies than that where MainWindow resides and the one dictionary is refering to the other dictionary. In that case reference is not resolved. This bug is already reported at Microsoft site in case your target framework is 4.0. However they have provided a workaround for it. Simply add the empty style in your Resource dictionaries and it will work fine like this -

<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>

For further refernce please look at this link - https://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references#details

ゃ懵逼小萝莉 2024-12-16 13:53:35

尝试

{StaticResource ApplicationPageBackgroundThemeBrush}

设置您的状态栏背景值

Try

{StaticResource ApplicationPageBackgroundThemeBrush}

for your status bar background value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文