首页 > 解决方案 > 仅错误消息设计时:“无法找到资源 'styles/buttonstyles.xaml'”

问题描述

在以下场景中出现一条神秘消息:无法找到资源“样式/按钮样式.xaml”

此消息仅在设计模式下出现,无法摆脱。如果我重建解决方案,错误消息会消失,直到我打开 ApplicationWindow.xaml。

环境如下:

MainWindow.xaml:

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

问题指向控件: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>

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

我试过的:

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

编辑1:

这是项目结构: 这里

ResourceDict 用法: 这里

谢谢,诺伯特

标签: c#wpfxamlresourcedictionary

解决方案


你还没有引入命名空间

<controls:CustomToolbar Style="{StaticResource ToolBarStyle}"/>是程序。

将以下行添加到 Window 标记

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

看看在哪里CustomToolbar,根据上面的代码输入


推荐阅读