WPF资源词典介绍及在VS中的使用
一、WPF资源词典介绍
传统意义上的资源(也称为二进制资源):编译器把外部文件编译进程序主体,成为程序主体不可分割的一部分。
WPF不但支持程序级的传统资源,还推出了独具特色的对象级资源,每个界面元素都可以携带自己的资源并可被自己的子级元素共享。比如各种模板(Template)、程序样式(Style)都放在对象级资源。
每个WPF界面元素都具有一个名为Resources的属性,继承自FrameworkElement类,其类型为ResourceDictionary。ResourceDictionary以“键(Key)-值(Value)”对的形式存储资源,使用某个资源时,使用键可以索引到资源对象中的值。
ex.在wpf应用程序中使用:
<Window x:Class="WpfApp1.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>
<StackPanel>
<TextBlock Text="{StaticResource str}" Margin="5"/>
</StackPanel>
</Window>
使用存在资源词典中的资源,可以使用静态方式(StaticResource)和动态方式(DynamicResource)
二、在VS中的使用资源词典
资源字典出现的初衷就在于可以实现多个项目之间的共享资源,VS中资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情。
1. 创建资源字典
在创建的wpf应用程序中添加资源字典,命名为Test_Dictionary,其是一个Xaml文件,将需要使用的资源可以全都包含进这个xaml文件之中。
示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Style_test">
<Style TargetType="TextBlock" x:Key="tt">
<Style.Setters>
<Setter Property="FontSize" Value="24"/>
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="FontStyle" Value="Italic"/>
</Style.Setters>
</Style>
</ResourceDictionary>
说明:在创建资源的时候要确保资源文件的编译选项为page,这样就能够保证XAML资源文件最终能够编译为baml文件。但是如果设置为Resource也是一个不错的选择,这样它能够嵌入到程序集中,但是不被编译,当然其解析的速度会稍微慢一点。
2. 使用资源字典
2.1 集成资源
使用资源字典,首先要将资源字典集成到应用程序的某些资源集合中。一般的做法是在app.xaml文件中进行集成。代码如下:
<Application x:Class="Style_test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Style_test"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Test_Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
说明:上面的标记通过明确地创建一个ResourceDictionary对象进行工作,资源集合总是ResourceDictionary对象。但是这只是需要明确指定细节从而可以设定ResourceDictionary.MergedDictionaries属性的一种情况。
如果没有这个步骤ResourceDictionary.MergedDictionaries属性将为空。ResourceDictionary.MergedDictionaries属性是一个ResourceDictionary对象的集合,可以使用这个集合提供自己需要使用的资源的集合。也就是说如果需要某个资源,只需要将与该资源相关的xaml文件。添加到这个属性中即可。如上面添加Test_Dictionary.xaml文件。
2.2 使用资源
集成之后就可以在当前的工程中使用这些资源了。使用方法如下:
<Window x:Class="Style_test.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:Style_test"
mc:Ignorable="d"
Title="MainWindow" Height="132" Width="300">
<StackPanel Margin="5">
<TextBlock Text="Hello WPF" Style="{StaticResource tt}"/>
<TextBlock Text="This is a sample for Style" Style="{StaticResource tt}"/>
<TextBlock Text="by Tim 2009.12.23"/>
</StackPanel>
</Window>
3. 总结:
使用资源字典的主要原因有两个:
a. 提供皮肤功能。
b. 存储需要被本地话的内容(错误消息字符串等,实现软编码)
使用过程也比较简单,归纳起来主要有下面几个步骤:
1. 创建资源字典文件
2. 资源字典集成
3. 使用字典中的资源
参考:1、《深入浅出WPF》-刘铁锰;
2、http://www.cnblogs.com/tianyou/archive/2012/12/07/2806835.html
一、WPF资源词典介绍
传统意义上的资源(也称为二进制资源):编译器把外部文件编译进程序主体,成为程序主体不可分割的一部分。
WPF不但支持程序级的传统资源,还推出了独具特色的对象级资源,每个界面元素都可以携带自己的资源并可被自己的子级元素共享。比如各种模板(Template)、程序样式(Style)都放在对象级资源。
每个WPF界面元素都具有一个名为Resources的属性,继承自FrameworkElement类,其类型为ResourceDictionary。ResourceDictionary以“键(Key)-值(Value)”对的形式存储资源,使用某个资源时,使用键可以索引到资源对象中的值。
ex.在wpf应用程序中使用:
<Window x:Class="WpfApp1.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>
<StackPanel>
<TextBlock Text="{StaticResource str}" Margin="5"/>
</StackPanel>
</Window>
使用存在资源词典中的资源,可以使用静态方式(StaticResource)和动态方式(DynamicResource)
二、在VS中的使用资源词典
资源字典出现的初衷就在于可以实现多个项目之间的共享资源,VS中资源字典只是一个简单的XAML文档,该文档除了存储希望使用的资源之外,不做任何其它的事情。
1. 创建资源字典
在创建的wpf应用程序中添加资源字典,命名为Test_Dictionary,其是一个Xaml文件,将需要使用的资源可以全都包含进这个xaml文件之中。
示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Style_test">
<Style TargetType="TextBlock" x:Key="tt">
<Style.Setters>
<Setter Property="FontSize" Value="24"/>
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="FontStyle" Value="Italic"/>
</Style.Setters>
</Style>
</ResourceDictionary>
说明:在创建资源的时候要确保资源文件的编译选项为page,这样就能够保证XAML资源文件最终能够编译为baml文件。但是如果设置为Resource也是一个不错的选择,这样它能够嵌入到程序集中,但是不被编译,当然其解析的速度会稍微慢一点。
2. 使用资源字典
2.1 集成资源
使用资源字典,首先要将资源字典集成到应用程序的某些资源集合中。一般的做法是在app.xaml文件中进行集成。代码如下:
<Application x:Class="Style_test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Style_test"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Test_Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
说明:上面的标记通过明确地创建一个ResourceDictionary对象进行工作,资源集合总是ResourceDictionary对象。但是这只是需要明确指定细节从而可以设定ResourceDictionary.MergedDictionaries属性的一种情况。
如果没有这个步骤ResourceDictionary.MergedDictionaries属性将为空。ResourceDictionary.MergedDictionaries属性是一个ResourceDictionary对象的集合,可以使用这个集合提供自己需要使用的资源的集合。也就是说如果需要某个资源,只需要将与该资源相关的xaml文件。添加到这个属性中即可。如上面添加Test_Dictionary.xaml文件。
2.2 使用资源
集成之后就可以在当前的工程中使用这些资源了。使用方法如下:
<Window x:Class="Style_test.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:Style_test"
mc:Ignorable="d"
Title="MainWindow" Height="132" Width="300">
<StackPanel Margin="5">
<TextBlock Text="Hello WPF" Style="{StaticResource tt}"/>
<TextBlock Text="This is a sample for Style" Style="{StaticResource tt}"/>
<TextBlock Text="by Tim 2009.12.23"/>
</StackPanel>
</Window>
3. 总结:
使用资源字典的主要原因有两个:
a. 提供皮肤功能。
b. 存储需要被本地话的内容(错误消息字符串等,实现软编码)
使用过程也比较简单,归纳起来主要有下面几个步骤:
1. 创建资源字典文件
2. 资源字典集成
3. 使用字典中的资源
参考:1、《深入浅出WPF》-刘铁锰;
2、http://www.cnblogs.com/tianyou/archive/2012/12/07/2806835.html