9.WPF资源

9.WPF资源

每个WPF界面元素都有一个Resources属性,类型为ResourceDictionary,在保存资源时,ResourceDictionary将所有的资源视为Object类型,所以需要进行类型转化。

资源的定义与查找

  • 资源放在XAML中
<Window x:Class="WpfApp1.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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="50" Width="200">
    <Window.Resources>
        <ResourceDictionary>
            <sys:String x:Key="str">
                字符串资源
            </sys:String>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="{StaticResource ResourceKey=str}" Margin="5"/>
    </StackPanel>
</Window>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0aLWVw8Y-1668141306928)(9.WPF资源.assets/image-20221111114151992.png)]

在检索资源时,会先查找控件自己的Resources属性,如果没有,则沿着逻辑树向上查找,如果最顶层容器也没有该资源,程序则会去查找Application.Resources,程序集资源。如果还没有则抛出异常。

C#使用定义在XAML中的资源this.FindResource("str");如果明确资源放在那个资源字典中,string txt = (string)this.Resources["str"];

  • 资源放在外部文件中
<Window.Resources>
    <ResourceDictionary Source="./hello.xaml"/>
</Window.Resources>

动态资源和静态资源

  • 静态资源:在程序载入内存时对资源的一次性加载和使用,之后不再访问
  • 动态资源:程序在运行过程中仍然会去访问该资源
<Window x:Class="WpfApp1.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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="200">
    <Window.Resources>
        <TextBlock x:Key="res1" Text="海上生明月"/>
        <TextBlock x:Key="res2" Text="海上生明月"/>
    </Window.Resources>
    <StackPanel>
        <Button Content="{StaticResource res1}"/>
        <Button Content="{DynamicResource res2}"/>
        <Button Content="更新" Click="Button_Click"/>
    </StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Resources["res1"] = new TextBlock() { Text = "天涯共此时" };
    this.Resources["res2"] = new TextBlock() { Text = "天涯共此时" };
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bKXCGrpw-1668141306929)(9.WPF资源.assets/image-20221111115526773.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D28Qp9qO-1668141306929)(9.WPF资源.assets/image-20221111115546881.png)]

二进制资源

在Properties的Resources.resx中添加string类型

如果要在xaml中使用资源

xmlns:prop="clr-namespace:WpfApp1.Properties"

<TextBlock Text="{x:Static prop:Resources.helllo2}"/>

其他资源直接在解决方案中新建文件夹增加外部资源,如果想让外部资源编程成二进制资源,需要设置文件的生成操作为Resource(不是嵌入的资源)。

使用二进制资源

WPF使用Pack URL来访问二进制资源,Pack URL格式

pack://application,,,[/程序集名称;][可选版本号;][文件夹名称/]文件名称

实际使用时pack://application,可以省略,[/程序集名称;][可选版本号;]采用默认值

例如:

<Image x:Name="img" Source="dd/R-C.PNG"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

步、步、为营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值