System.Windows.Markup.XamlParseException:““在“System.Windows.StaticResourceExtension”上提供值时引发了异常

在写WPF程序时,出现了以下错误,看错误名,应该是xaml的解释器未找到StaticResourceExtension的资源,后来参考文章:https://muzizongheng.blog.csdn.net/article/details/85127317

 我发现StaticResource用于xaml加载时使用ResourceDictionary中x:Key定义的资源,必须先定义再使用,不然会抛出异常。原来的xaml代码如下:

<Window x:Class="WpfStringResources.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:WpfStringResources"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="357" Width="500">
   
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="name1" Text="{StaticResource ResourceKey=name1}" Margin="90,70"/>
        <TextBlock Text="{StaticResource ResourceKey=name2}" Margin="90,70" Grid.Column="1"/>
        <TextBlock Text="{DynamicResource ResourceKey=name3}" Margin="90,70" Grid.Row="1"/>
        <TextBlock Text="{DynamicResource ResourceKey=name4}" Margin="90,70" Grid.Row="1" Grid.Column="1"/>
    </Grid>

    <Window.Resources>
        <system:String x:Key="name1">床前明月光,</system:String>
        <system:String x:Key="name2">疑是地上霜。</system:String>
        <system:String x:Key="name3">举头望明月,</system:String>
        <system:String x:Key="name4">低头思故乡。</system:String>
    </Window.Resources>

</Window>

将定义的window.Resources放在<Grid>之前就能成功运行了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值