WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)

本文详细介绍了WPF中的资源管理,包括StaticResource静态资源的使用,如何将资源放入独立文件,DynamicResource动态资源的特性,以及如何通过Resources.resx添加字符串和图片二进制资源。此外,还探讨了利用XmlDataProvider进行XML数据绑定的方法。
摘要由CSDN通过智能技术生成

一、WPF对象级(Window对象)资源的定义与查找

实例一: StaticResource 静态资源(如:皮肤配置方案,运行后不改变)

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window12" Height="300" Width="300">
    <!--将System命名空间引入到xaml代码并映射为sys命名空间-->
    <!--在Window.Resources属性添加两个资源条目-->
    <Window.Resources>
       <!--键值对资源-->
        <ResourceDictionary>
            <sys:String x:Key="str">
                字符
            </sys:String>
            <sys:Double x:Key="dbl">
                3.1415926
            </sys:Double>
        </ResourceDictionary>       
    </Window.Resources>
    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource ResourceKey=str}" Margin="5"/>
    </StackPanel>
</Window>

 

简化:

 

<Window x:Class="WpfApplication.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window12" Height="300" Width="300">
    <!--将System命名空间引入到xaml代码并映射为sys命名空间-->
    <!--在Window.Resources属性添加两个资源条目-->
    <Window.Resources>

    </Window.Resources>

    <StackPanel>
        <TextBlock name="textblock1" Text="{StaticResource str}" Margin="5"/>
    </StackPanel>
</Window>

 

在C#代码中,使用定义的XAML代码中定义的资源。

 

            //string text = (string)FindResource("str");
            string text = (string)this.Resources["str"];//键值获取
            this.textblock1.Text = text;

 

 

 

实例二:把资源像CSS或JavaScript放到独立文件中。

 

新建 “资源字典”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tiz198183

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

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

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

打赏作者

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

抵扣说明:

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

余额充值