1. <Window x:Class="RescourceDemo1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525" Icon="/RescourceDemo1;component/image/test.png">
  5. <Window.Resources>
  6. <TextBlock x:Key="res1" Text="海上生明月"/>
  7. <TextBlock x:Key="res2" Text="海上生明月"/>
  8. </Window.Resources>
  9. <Grid>
  10. <Button x:Name="btnStatic" Content="{StaticResource res1}" Height="23" HorizontalAlignment="Left" Margin="211,70,0,0" VerticalAlignment="Top" Width="75" />
  11. <Button x:Name="btnDynamic" Content="{DynamicResource res2}" Height="23" HorizontalAlignment="Left" Margin="211,113,0,0" VerticalAlignment="Top" Width="75" />
  12. <Button Content="Update" Height="23" HorizontalAlignment="Left" Margin="212,162,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
  13. <Label Content="静态资源:" Height="28" HorizontalAlignment="Left" Margin="144,69,0,0" Name="label1" VerticalAlignment="Top" />
  14. <Label Content="动态资源:" Height="28" HorizontalAlignment="Left" Margin="144,108,0,0" Name="label2" VerticalAlignment="Top" />
  15.  
  16. <Image Height="135" Source="image/test.png" HorizontalAlignment="Left" Margin="328,50,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="150" />
  17. </Grid>
  18. </Window>
  1. using System.Windows;
  2. using System.Windows.Controls;
  3.  
  4. namespace RescourceDemo1
  5. {
  6. /// <summary>
  7. /// MainWindow.xaml 的交互逻辑
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16. private void button3_Click(object sender, RoutedEventArgs e)
  17. {
  18. //静态资源一旦使用,则不变
  19. var res1 = this.btnStatic.Content as TextBlock;
  20. var res2 = this.btnDynamic.Content as TextBlock;
  21.  
  22. if (res1.Text.Equals("海上生明月")) this.Resources["res1"] = new TextBlock() { Text = "天涯共此时" };
  23. else this.Resources["res1"] = new TextBlock() { Text = "海上生明月" };
  24.  
  25. //动态资源设置后,扔可能改变
  26. if (res2.Text.Equals("海上生明月")) this.Resources["res2"] = new TextBlock() { Text = "天涯共此时" };
  27. else this.Resources["res2"] = new TextBlock() { Text = "海上生明月" };
  28. }
  29. }
  30. }

注意:静态资源不可改变,动态资源可动态更改! <Window.Resources>,为窗体级资源。

属性资源==》

  1. <Window x:Class="资源1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Window.Resources>
  7. <ResourceDictionary>
  8. <sys:String x:Key="str">
  9. 我是资源——资源为属性元素
  10. </sys:String>
  11. </ResourceDictionary>
  12. </Window.Resources>
  13. <Grid x:Name="grid">
  14. <TextBlock x:Name="textblock" Text="{StaticResource ResourceKey=str}"/>
  15. </Grid>
  16.  
  17. </Window>
  18. <!--
  19. 在上面代码中资源为属性元素,所以<ResourceDictionary>是可以省略掉的,下面是在后台的等效代码:
  20. this.Resources["str1"] = "我是资源";
  21. this.textblock.Text = this.FindResource("str1") as string;
  22. -->

静态、动态资源(资源为属性元素)==》

  1. <Window x:Class="资源2.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Window.Resources>
  7. <sys:String x:Key="DynamicRes">动态资源</sys:String>
  8. <sys:String x:Key="StaticRes">静态资源</sys:String>
  9. </Window.Resources>
  10. <StackPanel>
  11. <TextBox x:Name="txt1" Text="{DynamicResource ResourceKey=DynamicRes}" Margin="10"/>
  12. <TextBox x:Name="txt2" Text="{StaticResource ResourceKey=StaticRes}" Margin="10"/>
  13. <Button x:Name="btn" Content="资源类型区分" Click="btn_Click_1" Height="25" Margin="5"/>
  14. </StackPanel>
  15. </Window>
  1. using System.Windows;
  2.  
  3. namespace 资源2
  4. {
  5. /// <summary>
  6. /// MainWindow.xaml 的交互逻辑
  7. /// </summary>
  8. public partial class MainWindow : Window
  9. {
  10. public MainWindow()
  11. {
  12. InitializeComponent();
  13. }
  14.  
  15. private void btn_Click_1(object sender, RoutedEventArgs e)
  16. {
  17. this.Resources["StaticRes"] += "静态资源发生";
  18. this.Resources["DynamicRes"] += "改变";
  19. }
  20. }
  21. }

窗体级、文件级、应用程序级、对象级资源==》

项目结构:

  1. App.xml==》
  2.  
  3. <Application x:Class="资源3.App"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. StartupUri="MainWindow.xaml">
  7. <Application.Resources>
  8. <SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
  9. </Application.Resources>
  10. </Application>
  11.  
  12. 文件资源==》
  13. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  14. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  15. <SolidColorBrush x:Key="myWhiteBrush" Color="White" />
  16. </ResourceDictionary>

用法如下:

  1. <Window x:Class="资源3.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Window.Resources>
  6. <!--窗体级资源-->
  7. <!--<SolidColorBrush x:Key="myRedBrush" Color="Red" />-->
  8.  
  9. <ResourceDictionary>
  10. <SolidColorBrush x:Key="myRedBrush" Color="Red" />
  11. <ResourceDictionary.MergedDictionaries>
  12. <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
  13. </ResourceDictionary.MergedDictionaries>
  14. </ResourceDictionary>
  15. <!--文件级资源-->
  16. <!--<ResourceDictionary x:Key="aa" Source="Dictionary1.xaml"/>-->
  17. </Window.Resources>
  18.  
  19. <StackPanel>
  20. <!--窗体级资源-->
  21. <Button Margin="5" Background="{StaticResource myRedBrush}">窗体级资源Button</Button>
  22. <!--应用程序级资源-->
  23. <Button Margin="5" Background="{StaticResource myGoldBrush}">应用程序级资源Button</Button>
  24. <!--文件级资源-->
  25. <Button Margin="5" Background="{StaticResource myWhiteBrush}">文件级资源Button</Button>
  26. <!--对象级资源-->
  27. <Button Margin="5">
  28. <Button.Resources>
  29. <SolidColorBrush x:Key="myGreenBrush" Color="Green" />
  30. </Button.Resources>
  31. <Button.Content>
  32. <TextBlock Text="Sample Text" Background="{StaticResource myGreenBrush}" />
  33. </Button.Content>
  34. </Button>
  35. </StackPanel>
  36. </Window>

实例二:

  1. App.xaml
  2.  
  3. <Application x:Class="资源4.App"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. StartupUri="MainWindow.xaml">
  7. <Application.Resources>
  8. <!-- 应用程序级资源 -->
  9. <SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
  10. <SolidColorBrush Color="Blue" x:Key="myBrush" />
  11. </Application.Resources>
  12. </Application>
  13.  
  14. MainWindow.xaml
  15.  
  16. <Window x:Class="资源4.MainWindow"
  17. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  18. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  19. Title="MainWindow" Height="350" Width="525">
  20. <Window.Resources>
  21. <!-- 窗体级资源 -->
  22. <SolidColorBrush Color="White" x:Key="myWhiteBrush" />
  23. <SolidColorBrush Color="Green" x:Key="myBrush" />
  24. </Window.Resources>
  25. <StackPanel>
  26. <!-- 使用应用程序级定义的资源 -->
  27. <Button Margin="5" Content="使用应用程序级定义的资源" Background="{StaticResource myGoldBrush}" />
  28. <!-- 使用窗体级定义的资源 -->
  29. <Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myWhiteBrush}" />
  30. <!-- 窗体级资源的值覆盖应用程序级资源的值 -->
  31. <Button Margin="5" Content="窗体级资源的值覆盖应用程序级资源的值" Background="{StaticResource myBrush}" />
  32. <StackPanel Background="#FF999999">
  33. <StackPanel.Resources>
  34. <!-- 对象级资源 -->
  35. <SolidColorBrush Color="Yellow" x:Key="myYellowBrush" />
  36. <SolidColorBrush Color="Red" x:Key="myBrush" />
  37. </StackPanel.Resources>
  38. <!-- 使用应用程序级定义的资源 -->
  39. <Button Margin="5" Content="使用应用程序级定义的资源" Background="{StaticResource myGoldBrush}" />
  40. <!-- 使用窗体级定义的资源 -->
  41. <Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myWhiteBrush}" />
  42. <!-- 使用对象级定义的资源 -->
  43. <Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myYellowBrush}" />
  44. <!-- 使用对象级定义的资源覆盖窗体级、应用程序级定义的资源 -->
  45. <Button Margin="5" Content="使用对象级定义的资源覆盖窗体级、应用程序级定义的资源" Background="{StaticResource myBrush}" />
  46. </StackPanel>
  47. </StackPanel>
  48. </Window>

WPF Demo16 资源的更多相关文章

  1. WPF之资源字典zz

    最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...

  2. WPF 之 资源(Resource)

    1.什么叫WPF的资源(Resource)? 资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. ...

  3. WPF 中资源路径的问题

    WPF 中资源路径的问题 1. 引用当前工程的资源(注意xxxx.png的build action 应设置为Resource 或Embedded Resource) <ImageBrush Im ...

  4. WPF样式资源文件简单运用

    WPF通过资源来保存一些可以被重复利用的样式,下面的示例展示了简单的资源样式文件的使用: 一.xaml中定义资源及简单的引用 <Window.Resources > <!--wpf窗 ...

  5. WPF 访问资源中的Storyboard

    原文:WPF 访问资源中的Storyboard <UserControl.Resources> <Storyboard x:Key="testStoryboard" ...

  6. WPF学习资源整理

    WPF(WindowsPresentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的编程模型 ...

  7. WPF 调用资源图片

    原文:WPF 调用资源图片 最近做的wpf项目中,在开发的时候,把图片放到了bin下面,采用了imagePath =System.IO.Directory.GetCurrentDirectory()+ ...

  8. WPF - 资源收集

    原文:WPF - 资源收集 OpenExpressApp的UI现在是使用WPF,所以熟悉WPF是必须的,以下我将可能用到的一些相关内容随时记录下来,以备查阅.此篇文章将不断更新,感兴趣的可以看看,也欢 ...

  9. WPF 为资源字典 添加事件响应的后台类

    原文:WPF 为资源字典 添加事件响应的后台类 前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件 ...

随机推荐

  1. NEO VM原理及其实现(转载)

    NEO Vm原理及其实现 简介及与evm主要区别 neo vm和evm类似.底层都实现了一套opcode以及对应的执行器,opcode设计差距蛮大的,总体上来说evm的更加简洁,neo vm的功能更加 ...

  2. [转]Poisson Distribution

    Poisson Distribution Given a Poisson process, the probability of obtaining exactly successes in tria ...

  3. Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

    A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...

  4. 人工智能之KNN算法

    转载自:https://www.cnblogs.com/magic-girl/p/python-kNN.html 基于python实现的KNN算法 邻近算法(k-NearestNeighbor) 是机 ...

  5. Go Example--通道方向

    package main import "fmt" func main() { pings := make(chan string, 1) pongs := make(chan s ...

  6. Go Example--协程

    package main import "fmt" func main() { //main gorouting中调用f函数 f("direct") //重新建 ...

  7. 紧接着上篇文章,实现类一个是标准的FIFO,一个是出队在头部入队不一定追加到末尾

    注意描述:一个是插入队列(1是可以插入尾部eg一遍的序列尾部追加,2是可以插入中间eg优先队列) 而移除的描素都是删除头部 import java.util.Comparator; import ja ...

  8. List接口的使用方法

    ★List接口特点: 1.元素存储有序的集合 2.带索引的集合 3.集合中可以有重复的元素 4.常用的子类有ArrayList ,  LinkedList ★List接口的方法:add   , rem ...

  9. MySQL--时间戳属性2

    在MySQL 5.6版本中引入参数explicit_defaults_for_timestamp设置,该参数会影响Timestamp的默认属性. 同时在MySQL 5.6版本中中,去除一张表只能有一个 ...

  10. day 47 html 学习 css 学习

    前端基础之CSS CSS实例 每个CSS样式由两个组成部分:选择器和声明.声明又包括属性和属性值.每个声明之后用分号结束. CSS(Cascading Style Sheet,层叠样式表)定义如何显示 ...