zoukankan      html  css  js  c++  java
  • WPF的静态资源(StaticResource)和动态资源(DynamicResource)

    静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了。

    动态资源(DynamicResource)指的是在程序运行过程中然会去访问资源。

    WPF中,每个界面元素都含有一个名为Resources的属性,其存储的是以“键-值”对形式存在的资源,而其子级元素在使用这些资源时会从Resources中找到这些资源。在子级元素引用的资源分为StaticResource和DynamicResource,两者的不同在于,StaticResource在程序编译完成后就不能改变,而DynamicResource在编译完成后可以进行修改,

    一、定义并使用资源

    <Window x:Class="Demo010.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="149" Width="296">
        <Window.Resources>
            <TextBlock x:Key="Res1" Text="海上生明月" />
            <TextBlock x:Key="Res2" Text="海上生明月" />
        </Window.Resources>
        <StackPanel>
            <Button Margin="5,5,5,0" Content="{StaticResource Res1}" />
            <Button Margin="5,5,5,0" Content="{DynamicResource Res2}" />
            <Button Margin="5,5,5,0" Content="Update" Click="UpdateRes_Click" />
        </StackPanel>
    </Window>

    二、更新资源内容

    private void UpdateRes_Click(object sender, RoutedEventArgs e)
    {
        this.Resources["Res1"] = new TextBlock() { Text = "天涯共此时" };
        this.Resources["Res2"] = new TextBlock() { Text = "天涯共此时" };
    }

     

     this.Resources["资源键值"]和this.FindResource("资源键值");

  • 相关阅读:
    Pig Latin-freecodecamp算法题目
    Search and Replace -freecodecamp算法题目
    Where art thou-freecodecamp算法题目
    Roman Numeral Converter-freecodecamp算法题目
    Diff Two Arrays-freecodecamp算法题目
    Asp.Net前台调用后台变量
    ASP.NET获取前端页面的Html标签的值
    echart 设置图例图标形状
    解决tableexport导出到excel中有关中文乱码的问题
    C# Async与Await用法
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/13389458.html
Copyright © 2011-2022 走看看