MPF学习之二——Resource

1.   Resource

1.1.  什么是Resource

Resources are files or objects an application uses but are not creates in the actual executable code. Windows Forms 使用二进制资源来使程序获得大文件,例如图片和大文本文件。虽然WPF技术也使用二进制资源,但是也提出了逻辑资源(Logical resources,逻辑资源定义了对象 ,这些对象可在应用中使用,并且让你能在元素中共享对象.

1.2.  Using Binary Resources 使用二进制的资源

二进制资源每次使用时需要编译;属于Embeded Resources

1.2.1.  Embedding Resources嵌入式资源

l  使用Project菜单,选择“Add Existing Item”;

l  添加需要的资源文件;

l  随后”build”(F5)即可;

1.2.2.  Loading Resources

<Image Source="myPic.bmp" Margin="17,90,61,22" Name="Image1" Stretch="Fill"/>

 

<Image Source="myFolder/myPic.bmp" Margin="17,90,61,22" Name="Image1" Stretch="Fill"/>

1.2.3.   Pack URIs

l  pack URI的全路径:pack://<Authority>/<Folder>/<FileName>

其中<Authority>可以是application:,,, 

也可以是siteOfOrigin:,,,  

l  XAML中写

<Image Source="pack://application:,,,/myFolder/myPic.bmp" Margin="17,90,61,22" Name="Image1" Stretch="Fill"/>

l  C#中写

Image myImage;

myImage.Source = new BitmapImage(new Uri("pack://application:,,,/myFolder/myPic.bmp"));

l  Using Resources in Other Assemblies使用资源用其他集合

pack://application:,,,/<AssemblyName>;component/<Folder>/<FileName>

例如:Pack://application:,,,/myAssembly;component/myFolder/myPic.bmp

 

1.3.  Content Files内容文件

l  project-> ”Add Existing Item”

l  在项目里添加资源文件

l  打开“Properties”窗口,设置“Build Action”属性,修改属性“Copy To Output”成“Copy Always.

 

这种类型的资源文件,如果资源文件有跟换,是不需要编译的,直接从OUTPUTCOPY即可,而Embebd资源文件,则每次都需要编译。

在应用中添加,声音或者媒体文件,必须使用content files的格式,应为MediaPlayerMediaElement不能读取编译好的资源。

1.3.1.  Retrieving Loose Files with siteOfOrigin Pack URIs

使用siteOfOrigin则代表是松散型文件的表达,编译后会COPYoutput目录夹中。

<Image Source="pack://siteOfOrigin:,,,/OfficeFrontDoor.jpg"/>

 

1.3.2.  Retrieving Resources Manually

System.Windows.Resources.StreamResourceInfo myInfo;

string myString;

myInfo = Application.GetResourceStream(new Uri("myTextFile.txt", UriKind.Relative));

System.IO.StreamReader myReader =new System.IO.StreamReader(myInfo.Stream);

// myString is set to the text contained in myTextFile.txt

myString = myReader.ReadToEnd();

1.4.  逻辑资源

1.4.1.  定义一个逻辑资源

使用Resource标签定义一个逻辑资源

<Window.Resources>

<RadialGradientBrush x:Key="myBrush">

<GradientStop Color="CornflowerBlue" Offset="0" />

<GradientStop Color="Crimson" Offset="1" />

</RadialGradientBrush>

</Window.Resources>

 

如果不想这个resource应用在整个windows里,可以定义resources collection对于某个元素

<Grid>

<Grid.Resources>

<RadialGradientBrush x:Key="myBrush">

<GradientStop Color="CornflowerBlue" Offset="0" />

<GradientStop Color="Crimson" Offset="1" />

</RadialGradientBrush>

</Grid.Resources>

</Grid>

 

TIPS:其中RadialGradientBrush使用的是与LinearGradientBrush一样的比例坐标系统,是做页面渲染的,使用GradientStop指定不同颜色的渐变及偏移量。其中Offset为偏移量,0为中心,1为最外层;

l  LinearGradientBrush 对角线状渲染

l  RadialGradientBrush 半扇形渲染

 

SliverLight中:

RadialGradientBrush使用放射性渐变来进行颜色填充,用GradientOrigin来指定放射源的位置坐标,Center指定图形的中心位置坐标,RadiusXRadiusY分别指定在X轴和Y轴上的放射半径,同样使用GradientStop指定不同颜色的渐变及偏移量

RadialGradientBrush主要有三个属性:

Center:默认为(0.5,0.5)圆的中央,渐变的开始点

RadiusY:Y轴半径的比例

RadiusX:X轴半径的比例

GradientOrigin:单词直译为渐变原点,它主要负责渐变的偏移,

一般情况下,我都是保持Center不变,然后改变此值

 

但是一般都在Windows.Resources中使用。

为了使用静态资源,在refer to it,你必须用XAML代码定义资源。

 

每一个Object作为Resource,都必须设置x:Key属性,这个是其他WPF的元素使用这个资源的名称。

只有一个例外:Style object可以通过设置TargetType属性,而不要设置x:Key;

x:Key的名称不需要唯一性,只需要在他定义的这个Resource collection中唯一即可。

 

1.4.2.  Application Resources 应用资源

create App.xaml, add application resources

for instance:

<Application x:Class="WpfApplication2.App"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

StartupUri="Window1.xaml">

<Application.Resources>

<SolidColorBrush x:Key="appBrush" Color="PapayaWhip" />

</Application.Resources>

</Application>

1.4.3.  Accessing a Resource in XAML XAML中获取资源

{StaticResource myBrush} 可以把这个插在markup存放在任何需要Brush obj的地方.

eg:

<Grid Background="{StaticResource myBrush}">

</Grid>

当资源在XAML中被引用(when a resource is referenced in XAML,resources search with a matching key.

1.Resouece collection of declaring object

2.element's parent's resources collecion

3.an so on ,up to the window that hosts the element and to the applicaion Resources collection

 

 

1.5.  Static and Dynamic Resources 静态和动态资源

1.5.1.1.        使用方法:

{DynamicResource myBrush}

{StaticResource myBrush}

 

1.5.1.2.        DynamicResourceStaticResource的差别

l  lie in how the referencing elements retrieve the resources

l  StaticResourced 在资源的生命周期中只引用一次,

l  DynamicResource 只要被引用的对象使用,就能每次都获得这个资源 are acquired every time the referenced object is used

l  StaticResource,引用对象不会随下属资源的变化而变化。但是这个是可以解决的,WPF对象会自动提示依赖属性的变化,可以在这时对变化的资源属性进行变化。

WPF objects that implement dependency properties automatically incorporate change notification,and changes made to the properties of the resource are picked up by any objects using that resource.

eg:

<Window x:Class="WpfApplication2.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Window.Resources>

<SolidColorBrush x:Key="BlueBrush" Color="Blue" />

</Window.Resources>

<Grid Background="{StaticResource BlueBrush}">

</Grid>

在这里Grid使用了蓝色底色,如果BlueBrush中的Color变化成红色,the background of the grid would render as red because change notification would notify all objects using that resource that the property had changed.

 

l  DynamicResource中能够随下属资源变化而变化。

<Grid Background="{DynamicResource BlueBrush}">

</Grid>

1.5.1.3.        DynamicResourceStaticResource的使用原则

使用动态资源对应用性能造成一定影响,应为每次使用资源都需要获取一下,降低了效率,so  downside of using dynamic resources.

最好的方法:使用静态资源,除非有特定的理由使用动态资源,例如使用SystemBrushed, SystemFonts, SystemParameters类作为资源时,可以使用动态资源

 

 

1.6.  Resource Dictionary资源字典

1.6.1.  Creating a Resource Dictionary 创建资源字典

Resource Dictionary 是一种资源集合,通过一个分离的XAML文件(separate XAML)能够引入你的应用。能在一个solution的多个Project中共用这些Resource资源。

Create Resource dictonaty:

l  From the Project menu, choose Add Resource Dictionary. The Add New Item dialog box opens. Choose the name for the resource dictionary and click Add. The new resource dictionary is opened in XAML view.

l  Add resources to the new resource dictionary in XAML view. You can add resources to the file in XAML view, as shown in bold here:

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<SolidColorBrush x:Key="appBrush" Color="DarkSalmon" />

</ResourceDictionary>

 

1.6.2.  Merging Resource Dictionaries 合并资源字典

为了资源在你的应用程序中使用,必须使用Resources collection来合并资源字典文件,例如使用Window.ResourcesApplication.Resources

如何合并资源字典?通过在ResourceDictionary.MergedDictionaries添加引用的资源字段名称;

例如:resource dictionary Dictionary1.xaml & Dictionary2.xaml

<Window.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="Dictionary1.xaml" />

<ResourceDictionary Source="Dictionary2.xaml" />

</ResourceDictionary.MergedDictionaries>

<SolidColorBrush x:Key="BlueBrush" Color="Blue" />

</ResourceDictionary>

</Window.Resources>

如果在Resources collection在定义另外的资源,必须使用ResourceDictionary标签。

1.6.3.  Choosing Where to Store a Resource 选择在那里保存资源

可以从以下几个方面考虑资源保存的地方

l  ease of accessibility by referencing elements,考虑资源的易获得性

l  readability and maintainability of the code,可读性和可维护性

l  reusability,可重用性

 

l  如果是应用程序中的元素用的资源,可以存放在Application. Resources collection中;

l  如果是仅仅是某个Windows的元素用的资源,可以存放在Window.Resources collection中;可读性高,只要在一个文件中看到代码就可以了;

l   如果需要在一个Solution中的多个Project中使用资源,只能保存在Resource dictionary;可重用性高,可以在多个项目中使用,也方便被隔离出来被其他项目使用;

1.6.4.  Retrieving Resources in Code 获得资源

l  使用FindResourceKEY值来获取资源,

SolidColorBrush aBrush;  //定义资源

aBrush = (SolidColorBrush)Button1.FindResource("myBrush");

l  如果找不到这个资源,则会抛异常,所以可以使用TryFindResource来获得资源,更安全;

SolidColorBrush aBrush;  //定义资源

aBrush = (SolidColorBrush)Button1. TryFindResource ("myBrush");  找不到这个资源名,也不报错,可直接运行;

l  直接Resources collection直接获取资源;

SolidColorBrush aBrush; //定义资源

aBrush = (SolidColorBrush)this.Resources["myBrush"];

这么写的话,资源时可读写的(read-write),可以对资源进行变换。使用this.Resources无论静态资源,还是动态资源都可以对资源精心变更。使用了this指针之后,是不是每次LOAD资源都需要去调用this指针的东西。

SolidColorBrush aBrush = new SolidColorBrush(Colors.Red);  //NEW一个红颜色的资源

this.Resources["myBrush"] = aBrush; //把程序中的myBrush资源颜色更换成这个红色;

 

 

posted on 2012-09-06 10:33  馋宝宝  阅读(2041)  评论(0)    收藏  举报

导航