XAML开发报错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
相关产品推荐
相关产品推荐

