无废话WPF系列16:资源

简介:

在WPF中资源通常用作“样式”(Style)、样式模板、数据模板等。

一、资源的定义及使用

1. 应用程序级资源:

定义在App.xaml文件中,作为整个应用程序共享的资源

1
2
3
4
5
6
7
8
< Application  x:Class="DeepXAML.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="Red" x:Key="redBrush"></ SolidColorBrush >
     </ Application.Resources >
</ Application >

使用应用程序集资源

1
2
3
4
5
6
7
8
9
10
< Window  x:Class="DeepXAML.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DeepXAML"      
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         Title="MainWindow" Height="250" Width="450">  
     < StackPanel  x:Name="stackPanel">
         < Button  Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button >
     </ StackPanel >
</ Window >

2. 窗体级资源:定义在Window或Page中,作为一个窗体或页面共享的资源存在

1
2
3
4
5
6
7
8
9
10
11
12
13
< Window  x:Class="DeepXAML.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DeepXAML"      
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         Title="MainWindow" Height="250" Width="450"> 
     < Window.Resources >
         < SolidColorBrush  Color="Red" x:Key="redBrush"></ SolidColorBrush >
     </ Window.Resources >
     < StackPanel  x:Name="stackPanel">
         < Button  Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button >
     </ StackPanel >
</ Window >

 

3. 文件级资源:定义在资源字典的XAML文件中,再引用

在Visual Studio的WPF应用程序项目中,添加“资源字典(Resource Dictionary)”类型的项

1
2
3
4
< ResourceDictionary  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     < SolidColorBrush  Color="Red" x:Key="redBrush"></ SolidColorBrush >
</ ResourceDictionary >
1
2
3
4
5
6
7
8
9
10
11
12
13
< Window  x:Class="DeepXAML.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DeepXAML"      
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         Title="MainWindow" Height="250" Width="450"> 
     < Window.Resources >
         < ResourceDictionary  Source="Skin1.xaml"></ ResourceDictionary >
     </ Window.Resources >
     < StackPanel  x:Name="stackPanel">
         < Button  Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button >
     </ StackPanel >
</ Window >

4.对象(控件)级资源:定义在某个ContentControl中,作为其子容器、子控件共享的资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< Window  x:Class="DeepXAML.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DeepXAML"      
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         Title="MainWindow" Height="250" Width="450"> 
      < StackPanel  x:Name="stackPanel">
         < StackPanel.Resources >
             < ResourceDictionary  Source="Skin1.xaml"></ ResourceDictionary >
         </ StackPanel.Resources >
         < Button  Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button >
     </ StackPanel >
</ Window >
 
二、资源文件解析的顺序
1
这个顺序和层叠样式表类似,优先级从高到底为:对象级,窗体级,应用程序集。静态资源(StaticResource)和动态资源(DynamicResource)

资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。

通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。

 

三、静态资源(StaticResource)和动态资源(DynamicResource)

资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。

通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。

Background="{DynamicResource redBrush}"
 
private void Button_Click(object sender, RoutedEventArgs e)
{
   SolidColorBrush brush = new SolidColorBrush(Colors.Green);
   this.Resources["redBrush"] = brush;

}

本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2011/02/27/1966170.html如需转载请自行联系原作者


王德水

相关文章
|
C#
避免让WPF资源字典变得杂乱臃肿
原文:避免让WPF资源字典变得杂乱臃肿                   避免让WPF资源字典变得杂乱臃肿                           周银辉 今天看到项目种的一个XXXResource.xaml文件代码有二千多行,这引发了我一些思考:如何组织我们的WPF资源。
1060 0
|
C#
WPF 界面实现多语言支持 中英文切换 动态加载资源字典
原文:WPF 界面实现多语言支持 中英文切换 动态加载资源字典 1、使用资源字典,首先新建两个字典文件en-us.xaml、zh-cn.xaml。定义中英文的字符串在这里面【注意:添加xmlns:s="clr-namespace:System;assembly=mscorlib】 zh-cn.
2915 0
|
3月前
|
C#
浅谈WPF之样式与资源
WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。
44 0
|
C# 数据格式 XML
WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
8058 0
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
|
C#
WPF 添加 Resources Dictionary 资源 一般类库项目中无法添加资源文件(ResourceDictionary)
原文:WPF 添加 Resources Dictionary 资源 一般类库项目中无法添加资源文件(ResourceDictionary) 在文件夹或者项目右键-> Add(添加),会弹出可以快捷添加的资源,但是你会发现没有 ResourceDictionary资源可以选择。
3831 0
|
C#
WPF BitmapImage 占用资源无法释放、无法删除问题
原文:WPF BitmapImage 占用资源无法释放、无法删除问题 使用Image控件显示图片后,虽然自己释放了图片资源,Image.Source =null 了一下,但是图片实际没有释放。解决方案:修改加载方式~        public static BitmapImage GetImage...
2023 0
|
C#
WPF中的一个bug造成CPU资源拉满
WPF中的一个bug造成CPU资源拉满
170 0
|
C#
WPF资源字典怎么用
资源字典的使用方法
188 0
WPF资源字典怎么用
|
C#
WPF XAML 资源样式模板属性存放位置
原文:WPF XAML 资源样式模板属性存放位置 WPF的XAML 资源申明 类似HTML。 整体来说分3种1.行类资源样式属性 1.1 行内属性 1.2 行内样式 模板(没多大意义) ...
1109 0