【问题标题】:Error message design time only: "Cannot locate resource 'styles/buttonstyles.xaml'"仅错误消息设计时:“无法找到资源 'styles/buttonstyles.xaml'”
【发布时间】:2021-02-11 22:19:51
【问题描述】:

在以下场景中出现了一条神秘消息: 找不到资源'styles/buttonstyles.xaml'

此消息仅在设计模式下出现,不能被忽略。如果我重建解决方案,错误消息会消失,直到我打开 ApplicationWindow.xaml。

环境如下:

MainWindow.xaml:

...
<Grid Grid.Row="1">
    <controls:CustomToolbar Style="{StaticResource ToolBarStyle}"/>
</Grid>
...

问题指向controls:CustomToolbar部分。

CustomToolbar.xaml:

<UserControl x:Class="Frontend.Views.Controls.CustomToolbar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Frontend.Views"
             mc:Ignorable="d" 
             d:DesignHeight="30" d:DesignWidth="800">
    
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Styles/ButtonStyles.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/Views/GlobalStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Horizontal">
        <Button Style="{StaticResource ToolbarButton}" Command="{Binding NewProjectCommand}" ToolTip="New project">
            <Image Source="{StaticResource NewProject}" Stretch="None" />
        </Button>
        <StackPanel Orientation="Horizontal">
            <Separator Height="20" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
        </StackPanel>
    </StackPanel>
</UserControl>

如果我更改合并字典的顺序,错误消息会切换到第一个。

我尝试过的:

  • google 和 stackoverflow 的每个链接
  • 清理解决方案,包括删除 bin、obj、.vs 文件夹,然后重新构建。
  • 重启 Visual Studio,重启电脑
  • 使用完全限定的 URI -> 不起作用,这些字典在当前程序集中。
  • 每个 ResourceDictionary 的属性都设置为 BuildAction:Page 和 MSBuild:Compile

如果我不使用任何 ResourceDictionary,则不会出现错误消息。

EDIT1:

这是项目结构: HERE

ResourceDict 用法: HERE

谢谢, 诺伯特

【问题讨论】:

  • 项目结构是什么,东西放在哪里,用在哪里?

标签: c# wpf xaml resourcedictionary


【解决方案1】:

你还没有引入命名空间在哪里

&lt;controls:CustomToolbar Style="{StaticResource ToolBarStyle}"/&gt; 是程序。

将以下行添加到 Window 标签中

xmlns:controls="clr-namespace:Your name space.Views.Controls"

查看CustomToolbar所在的位置,根据上面的代码输入

【讨论】:

  • 问题仍然存在。我用其他信息扩展了这个问题。
  • @Behold 灯光系统 CustomToolbar 无法识别您。将其引入程序
  • 我不知道你到底是什么意思。
  • 你给系统一个CustomToolbar命名空间了吗?我在帖子里解释过了。
  • 是的,我做到了,我收到以下错误消息:“未定义的 CLR 命名空间。‘clr-namespace’ URI 引用了找不到的命名空间‘Frontend.Views.Controls’。”
【解决方案2】:

看来我通过重构mergedDictionary包含解决了这个问题,方法如下:

  • 从每个控件、样式等中删除了每个 MergedDictionary。
  • 将它们全部移至 App.xaml(移至根目录)

代码:

<Application x:Class="Frontend.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Styles/Images.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Styles/ButtonStyles.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Styles/MenuStyles.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Styles/ToolbarStyles.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Views/GlobalStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我非常有经验的朋友说: 如果您交换 xaml 文件中的包含顺序,可能会发生这种情况,例如:

Style1 在 Resdic1 中定义。 Style2 在 Resdic2 中定义。 Style2 使用 Style1。

但是合并字典中的顺序如下:

  • Resdic2
  • Resdic1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2017-11-29
    • 1970-01-01
    • 2011-02-22
    • 2020-01-17
    • 2017-04-20
    • 2013-07-20
    相关资源
    最近更新 更多