You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XAML开发报错XDG0062:资源“BlueButton”无法解析求助

解决XDG0062:资源“BlueButton”无法解析的问题

错误原因很直接:你在按钮里引用的BlueButton样式根本没有定义,所以XAML解析器找不到这个资源。

下面是几种可行的解决方法:

1. 在当前窗口内定义样式

直接在Window标签内添加Window.Resources节点,把BlueButton样式定义在这里,当前窗口的控件就能直接引用:

<Window x:Class="grafic.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:grafic"
    mc:Ignorable="d"
    Title="welcome" Height="450" Width="800">

<!-- 定义窗口级别的资源 -->
<Window.Resources>
    <Style x:Key="BlueButton" TargetType="Button">
        <!-- 自定义样式内容,可根据需求修改 -->
        <Setter Property="Background" Value="#2196F3"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <!-- 可添加状态触发器,比如鼠标悬停效果 -->
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#1976D2"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="thil.png" AlignmentY="Top" AlignmentX="Center"/>
    </Grid.Background>

    <Button 
        Style="{StaticResource BlueButton}"
        x:Name="signIn"
        Content="Sign in"
        Margin="297,40,347,333" FontWeight="Bold" FontSize="36" Click="signIn_Click"
        />
</Grid>
</Window>

2. 定义全局样式(全应用可用)

如果想让整个应用的控件都能使用BlueButton样式,把样式定义在App.xaml的Application.Resources节点里即可:

<Application x:Class="grafic.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:grafic"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="BlueButton" TargetType="Button">
            <Setter Property="Background" Value="#2196F3"/>
            <Setter Property="Foreground" Value="White"/>
            <!-- 其他样式设置 -->
        </Style>
    </Application.Resources>
</Application>

3. 引用外部资源字典

如果样式是放在单独的ResourceDictionary.xaml文件里,需要先在窗口或App里合并资源字典:

<!-- 在Window.Resources中添加 -->
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="YourResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

注意:确保资源字典的路径正确,否则仍会出现找不到资源的错误。


内容的提问来源于stack exchange,提问作者oralkobi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:05:18