WPF 资源

        每个WPF的界面元素都具有一个名为Resources的属性,这个属性继承自FrameworkElement类,其类型为ResourceDictionary。ResourceDictionary能够以"键-值"对的形式存储资源,当许需要使用某个资源时,使用"键-值"对可以索引到资源对象。如图所示,按键的背景色设置成资源中的对应key的value值:

 

    <Window.Resources>
        <ResourceDictionary>
            <SolidColorBrush x:Key="backgroudColor" Color="LightBlue"/>
            <TextBlock x:Key="res1" Text="静态资源"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
       <StackPanel VerticalAlignment="Center">
            <Button Width="100" Height="30"  Content="动态资源" Background="{DynamicResource backgroudColor}"/>
            <Button Width="100" Height="30" Content="{StaticResource res1}" Margin="0,20"/>
        </StackPanel>
    </Grid>

引用资源:动态引用资源,静态引用资源
动态引用资源:当资源被改变的时候,引用该资源的对象会同步发生变化;
  通过xaml标记扩展语法 XX属性 = "{DynamicResource myBrush}"
静态引用资源:程序启动时候加载该资源后,无论资源发生变化,程序不会发生变化
  通过xaml标记扩展语法 XX属性 = "{StaticResource myBrush}"

向程序添加二进制资源

        资源文件在目标文件里以二进制数据的形式存在、形成目标文件的资源段(Resource Section),使用时数据会被提取出来。操作如下:

代码中使用:

<Window x:Class="WpfResourceDemo.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:WpfResourceDemo"
        xmlns:prop="clr-namespace:WpfResourceDemo.Properties"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Grid.Column="1" Text="{x:Static prop:Resources.UserName}" Width="100" Height="30"/>
    </Grid>
</Window>

使用Pack URI路径访问二进制资源

        <StackPanel Grid.Row="1" >
            <Image Source="Images/1.png" Width="100" Height="100"/>
            <Image Source="pack://application:,,,/WpfResourceDemo;component/Images/2.png" Width="100" Height="100"/>
        </StackPanel>

系统资源
        当某个元素查找资源时,应用程序资源仍然不是最后一站。如果没有在应用程序资源中找到所需的资源,元素还会继续查找系统资源。
系统资源的三个类

  • SystemColors   访问系统颜色设置。
  • SystemFonts    访问字体设置。
  • SystemParamerers  封装了大量的设置列表,这些设置描述了各种屏幕像素的标准尺寸、键盘和鼠标设置、屏幕尺寸以及各种图形效果(如热跟踪、阴影以及拖动窗口时显示窗口内容)是否已经打开。

后台查询资源

  public MainWindow()
 {
     InitializeComponent();
     //读取Resources资源
     txtProduct.Text = Properties.Resources.UserName;

     //读取Settings资源
     string title = Properties.Settings.Default.Title;
     txtTitle.Text = title;
     //设置和保存Settings资源
     Properties.Settings.Default.Title = "Hello World!";
     Properties.Settings.Default.Save();
 }

//在事件源中查询资源
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     Button cmd = (Button)sender;
     //FindResource查询资源
     SolidColorBrush brush = (SolidColorBrush)cmd.FindResource("backgroudColor");
 }

 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     //查询本窗体资源
     SolidColorBrush b =  (SolidColorBrush)this.Resources["backgroudColor"];
     //查询程序公共资源
    var p = Application.Current.Resources["product"];
 }

资源字典
         如果希望多个项目中共享资源,可创建资源字典。资源字典只是xaml文档,除了存储希望使用的资源外,不做其他任何事情。

<ResourceDictionary 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">
    <sys:String x:Key="product">
        WPF资源
    </sys:String>
    
</ResourceDictionary>

使用资源字典将其整合到应用程序资源中:

<Application x:Class="WpfResourceDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfResourceDemo"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

资源实例:https://download.csdn.net/download/lvxingzhe3/87642943

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无熵~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值