WPF快速指导1:资源
本文摘要:
1:资源应用场景;
2:静态资源和动态资源;
3:Application.Current.Resources和Application.Current.Resources.MergedDictionaries
4:路径
一:资源的应用场景
场景1:格式化界面显示元素
所谓格式化界面显示元素,就是使用统一的风格来定义软件的每个界面。
要满足本需求,只需要在App.xaml中如下定义资源
<Application.Resources>
<Style TargetType="TextBlock" x:Key="TitleText">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="TextBlock" x:Key="Label">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Application.Resources>
同时,在每个页面如下引用资源即可:
<StackPanel>
<TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
<TextBlock Style="{StaticResource Label}">Label</TextBlock>
</StackPanel>
场景2:动态更新界面风格
要动态更新界面风格,首先需要定义多种界面风格。假设有Sytle1和Style2两种风格,其中Style1在Style1.xaml中定义:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock" x:Key="TitleText">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="TextBlock" x:Key="Label">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</ResourceDictionary>
Style2在Style2.xaml中定义(假设将Style1中的Blue改为Yellow,12改为24,不再列出)。那么,在页面中,我们如下引用:
<StackPanel>
<TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
<TextBlock Style="{DynamicResource Label}">Label</TextBlock>
<Button Click="Button1_Click">Style1</Button>
<Button Click="Button2_Click">Style2</Button>
</StackPanel>
同时,后台代码为:
ResourceDictionary style1;
ResourceDictionary style2;
private void Button1_Click(object sender, RoutedEventArgs e)
{
style1 = new ResourceDictionary();
style1.Source = new Uri(@"Resouce\Style1.xaml", UriKind.Relative);
Application.Current.Resources = style1;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
style2 = new ResourceDictionary();
style2.Source = new Uri(@"Resouce\Style2.xaml", UriKind.Relative);
Application.Current.Resources = style2;
}
如此一来,我们便完成动态的界面风格变化。
二:静态资源和动态资源
在上文的场景二示例中,如果将XAML中的
<TextBlock Style="{DynamicResource TitleText}">Title</TextBlock>
<TextBlock Style="{DynamicResource Label}">Label</TextBlock>
换成
<TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
<TextBlock Style="{StaticResource Label}">Label</TextBlock>
我们会发现界面的风格根本没有得到改变。这里我们引出静态资源和动态资源最重要的一个区别:静态资源不基于运行时行为进行重新求值,而动态资源在运行时加载。
关于静态资源和动态资源其它区别请查看MSDN。
三:Application.Current.Resources和Application.Current.Resources.MergedDictionaries
先来看这两个变量的原型:
Application.Current.Resources的原型是一个ResourceDictionary。
Application.Current.Resources.MergedDictionaries是一个Collection<ResourceDictionary> 。
从本质上来讲,这两个变量没有区别,MergedDictionaries是在表现形式上,在运行时扩展系统的资源。
我们再来看上文中运行时动态改变界面的示例,我们通过动态给Application.Current.Resources赋值,来改变界面风格。
在实际使用中,必不要这么做。因为不管你是否需要在运行时动态更新部分界面风格,有些资源是肯定不变的。也就是说,一个系统,必定已经存在一个资源文件,即,最好不要在运行时改变Application.Current.Resources。那么,实际要做的,就是动态的增加或者删除Application.Current.Resources.MergedDictionaries就可以了。
四:路径
第一种:
imgContent.Source = new BitmapImage(new Uri("Content.jpg", UriKind.Relative));
imgResource.Source = new BitmapImage(new Uri("Resource.jpg", UriKind.Relative));
第二种:
imgContent.Source = new BitmapImage(new Uri("pack://application:,,,/Content.jpg"));
imgResource.Source = new BitmapImage(new Uri("pack://application:,,,/Resource.jpg"));
第三种:
imgContent.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/Content.jpg"));
最后一点需要说说的是路径的问题,关于路径,在WPF中有几种表示方法:
第一种和第二种都可以访问相对WPF资源路径的Resource和Content资源。第三种方式可以访问运行目录下的Content资源文件以及完全松散的文件。完全松散的文件指那些没有添加到项目中,只是拷贝在程序目录中的文件。
应用程序根本不知道它的存在。pack://application:,,,/Content.jpg表示当前项目的资源。它是pack://application:,,,/DllName;Component/Content.jpg的简写。将DllName替换成其他程序集,就可以访问其他程序集的资源。
pack://SiteOfOrigin:,,,/Content.jpg表示从部署位置访问文件。
pack URI格式是XML文件规范的一部分,具体格式如下 pack://packageURI/partPath。PackageURI实际上是在URI中放一个URI,它是把反斜杠都变成了逗号。packageURI的WPF资源路径可以志向一个XPS文档,例如file : /// c: /Document . xps会被编码为file:...c:,Document.xps。在WPF程序中有两种URI系统是特别处理的:
siteOfOrigin:/// 编码后siteOfOrigin:,,,
application:/// 编码后application:,,,
3个逗号其实是反斜杠编码过来的。
该系列参考:MSDN、《WPF编程》
练习:
1:要统一整个应用程序的风格,应该如何来处理。
2:要对某个特定的页面(Window或Page)上所有的TextBlock统一风格,应该如何处理。
3:静态资源和动态资源的一个区别。
4:Application.Current.Resources和Application.Current.Resources.MergedDictionaries的用处。
本文基于
Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名
http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。