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

注意:静态资源不可改变,动态资源可动态更改! <Window.Resources>,为窗体级资源。
属性资源==》
<Window x:Class="资源1.MainWindow"
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="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<sys:String x:Key="str">
我是资源——资源为属性元素
</sys:String>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="grid">
<TextBlock x:Name="textblock" Text="{StaticResource ResourceKey=str}"/>
</Grid> </Window>
<!--
在上面代码中资源为属性元素,所以<ResourceDictionary>是可以省略掉的,下面是在后台的等效代码:
this.Resources["str1"] = "我是资源";
this.textblock.Text = this.FindResource("str1") as string;
-->

静态、动态资源(资源为属性元素)==》
<Window x:Class="资源2.MainWindow"
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="MainWindow" Height="350" Width="525">
<Window.Resources>
<sys:String x:Key="DynamicRes">动态资源</sys:String>
<sys:String x:Key="StaticRes">静态资源</sys:String>
</Window.Resources>
<StackPanel>
<TextBox x:Name="txt1" Text="{DynamicResource ResourceKey=DynamicRes}" Margin="10"/>
<TextBox x:Name="txt2" Text="{StaticResource ResourceKey=StaticRes}" Margin="10"/>
<Button x:Name="btn" Content="资源类型区分" Click="btn_Click_1" Height="25" Margin="5"/>
</StackPanel>
</Window>
using System.Windows; namespace 资源2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void btn_Click_1(object sender, RoutedEventArgs e)
{
this.Resources["StaticRes"] += "静态资源发生";
this.Resources["DynamicRes"] += "改变";
}
}
}

窗体级、文件级、应用程序级、对象级资源==》
项目结构:
App.xml==》 <Application x:Class="资源3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
</Application.Resources>
</Application> 文件资源==》
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myWhiteBrush" Color="White" />
</ResourceDictionary>
用法如下:
<Window x:Class="资源3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--窗体级资源-->
<!--<SolidColorBrush x:Key="myRedBrush" Color="Red" />--> <ResourceDictionary>
<SolidColorBrush x:Key="myRedBrush" Color="Red" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!--文件级资源-->
<!--<ResourceDictionary x:Key="aa" Source="Dictionary1.xaml"/>-->
</Window.Resources> <StackPanel>
<!--窗体级资源-->
<Button Margin="5" Background="{StaticResource myRedBrush}">窗体级资源Button</Button>
<!--应用程序级资源-->
<Button Margin="5" Background="{StaticResource myGoldBrush}">应用程序级资源Button</Button>
<!--文件级资源-->
<Button Margin="5" Background="{StaticResource myWhiteBrush}">文件级资源Button</Button>
<!--对象级资源-->
<Button Margin="5">
<Button.Resources>
<SolidColorBrush x:Key="myGreenBrush" Color="Green" />
</Button.Resources>
<Button.Content>
<TextBlock Text="Sample Text" Background="{StaticResource myGreenBrush}" />
</Button.Content>
</Button>
</StackPanel>
</Window>

实例二:
App.xaml <Application x:Class="资源4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!-- 应用程序级资源 -->
<SolidColorBrush Color="Gold" x:Key="myGoldBrush" />
<SolidColorBrush Color="Blue" x:Key="myBrush" />
</Application.Resources>
</Application> MainWindow.xaml <Window x:Class="资源4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- 窗体级资源 -->
<SolidColorBrush Color="White" x:Key="myWhiteBrush" />
<SolidColorBrush Color="Green" x:Key="myBrush" />
</Window.Resources>
<StackPanel>
<!-- 使用应用程序级定义的资源 -->
<Button Margin="5" Content="使用应用程序级定义的资源" Background="{StaticResource myGoldBrush}" />
<!-- 使用窗体级定义的资源 -->
<Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myWhiteBrush}" />
<!-- 窗体级资源的值覆盖应用程序级资源的值 -->
<Button Margin="5" Content="窗体级资源的值覆盖应用程序级资源的值" Background="{StaticResource myBrush}" />
<StackPanel Background="#FF999999">
<StackPanel.Resources>
<!-- 对象级资源 -->
<SolidColorBrush Color="Yellow" x:Key="myYellowBrush" />
<SolidColorBrush Color="Red" x:Key="myBrush" />
</StackPanel.Resources>
<!-- 使用应用程序级定义的资源 -->
<Button Margin="5" Content="使用应用程序级定义的资源" Background="{StaticResource myGoldBrush}" />
<!-- 使用窗体级定义的资源 -->
<Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myWhiteBrush}" />
<!-- 使用对象级定义的资源 -->
<Button Margin="5" Content="使用窗体级定义的资源" Background="{StaticResource myYellowBrush}" />
<!-- 使用对象级定义的资源覆盖窗体级、应用程序级定义的资源 -->
<Button Margin="5" Content="使用对象级定义的资源覆盖窗体级、应用程序级定义的资源" Background="{StaticResource myBrush}" />
</StackPanel>
</StackPanel>
</Window>

WPF Demo16 资源的更多相关文章
- WPF之资源字典zz
最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...
- WPF 之 资源(Resource)
1.什么叫WPF的资源(Resource)? 资源是保存在可执行文件中的一种不可执行数据.在WPF的资源中,几乎可以包含图像.字符串等所有的任意CLR对象,只要对象有一个默认的构造函数和独立的属性. ...
- WPF 中资源路径的问题
WPF 中资源路径的问题 1. 引用当前工程的资源(注意xxxx.png的build action 应设置为Resource 或Embedded Resource) <ImageBrush Im ...
- WPF样式资源文件简单运用
WPF通过资源来保存一些可以被重复利用的样式,下面的示例展示了简单的资源样式文件的使用: 一.xaml中定义资源及简单的引用 <Window.Resources > <!--wpf窗 ...
- WPF 访问资源中的Storyboard
原文:WPF 访问资源中的Storyboard <UserControl.Resources> <Storyboard x:Key="testStoryboard" ...
- WPF学习资源整理
WPF(WindowsPresentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的编程模型 ...
- WPF 调用资源图片
原文:WPF 调用资源图片 最近做的wpf项目中,在开发的时候,把图片放到了bin下面,采用了imagePath =System.IO.Directory.GetCurrentDirectory()+ ...
- WPF - 资源收集
原文:WPF - 资源收集 OpenExpressApp的UI现在是使用WPF,所以熟悉WPF是必须的,以下我将可能用到的一些相关内容随时记录下来,以备查阅.此篇文章将不断更新,感兴趣的可以看看,也欢 ...
- WPF 为资源字典 添加事件响应的后台类
原文:WPF 为资源字典 添加事件响应的后台类 前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件 ...
随机推荐
- lava.lang.String数据转换为java.sql.Date
在JavaWeb编程中,往往涉及数据库,javaBean,前端数据数据类型不一致的问题 数据库和javaBean之间可以直接选择相对应的数据类型,而serverlet从前端获取的数据往往是String ...
- 测试之法 —— mock object
mock object 与真实对象相比,用来构造测试场景. 1. 一个实例 一个闹钟根据时间来进行提醒服务,如果过了下午5点钟就播放音频文件提醒大家下班了,如果我们要利用真实的对象来测试的话就只能苦苦 ...
- github上DQN代码的环境搭建,及运行(Human-Level Control through Deep Reinforcement Learning)conda配置
最近师弟在做DQN的实验,由于是强化学习方面的东西,正好和我现在的研究方向一样于是我便帮忙跑了跑实验,于是就有了今天的这个内容. 首先在github上进行搜寻,如下图: 发现第一个星数最多,而且远高于 ...
- 【翻译】Context should go away for Go 2
2017/08/06 每次blog.golang.org更新博客,我都迫不及待去读一下:最新的一篇, Contributors Summit,记录了Go贡献者们的一些讨论.我读到一句话,让我感觉得 ...
- Groovy实现代码热载的机制和原理
前言: 真的很久没在博客园上更新博客了, 现在趁这段空闲的时间, 对之前接触的一些工程知识做下总结. 先来讲下借用Groovy如何来实现代码的热载, 以及其中涉及到的原理和需要注意的点. 总的来说, ...
- python之路,正则表达式
python3 正则表达式 前言: (1). 处理文本称为计算机主要工作之一(2)根据文本内容进行固定搜索是文本处理的常见工作(3)为了快速方便的处理上述问题,正则表达式技术诞生,逐渐发展为一个单独技 ...
- OpenCV2类批量处理文件夹及文件图像 及批量处理后保存到txt文件
//采用windows控制台实现计算文件夹中对象总数以及批量读取对象 //#include <afx.h> //和windows.h是一样的作用 #include <opencv2/ ...
- SQL-表-003
注:红色代表关键字,绿色代表解释说明,蓝色代表重点: 什么是数据表? 数据表是数据库中最重要的组成部分,可以将数据表分解成字段(列)和记录(行): 数据表的增加:约束同时创建 create table ...
- C++学习(十二)(C语言部分)之 循环
上期回顾:分支if else switch 三目运算符 格式:if(条件){语句1:}else{语句2:} switch (变量){default:break;case 常量1:语句1 ;break; ...
- PHP黑魔法(该篇文章转自:http://www.91ri.org/12634.html 目的是作为自己的笔记方便查找)
那些年我们学过的PHP黑魔法 作者:Matrix_ling 序 这里必须得说一下==和===这俩货的重要性.==是比较运算,它不会去检查条件式的表达式的类型===是恒等,它会检查查表达式的值与类型是否 ...