Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6539789
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-07-28 14:05:44

用于特殊目的的动态资源
用于引用Resources字典中的项的StaticResource的替代方法是DynamicResource,如果您在上面的示例中仅使用DynamicResource替换StaticResource,则程序似乎运行相同。但是,这两个标记扩展非常不同。在解析XAML并构建页面时,StaticResource仅访问字典中的项目一次。但DynamicResource维护字典键和该字典项的属性集之间的链接。如果键引用的资源字典中的项发生更改,DynamicResource将检测到该更改并将新值设置为该属性。
持怀疑态度?我们来试试吧。 DynamicVsStatic项目有一个XAML文件,它定义了一个类型为string的资源项,其键为currentDateTime,即使字典中的项是字符串“Not actual a DateTime”!
此字典项在XAML文件中被引用四次,但其中一个引用被计算出来。在前两个示例中,Label的Text属性是使用StaticResource和DynamicResource设置的。在后两个示例中,Span对象的Text属性设置类似,但在Span对象上使用DynamicResource会出现在注释中:  

点击(此处)折叠或打开

  1. <ContentPage xmlns=""
  2.              xmlns:x=""
  3.              x:Class="DynamicVsStatic.DynamicVsStaticPage"
  4.              Padding="5, 0">
  5.  
  6.     <ContentPage.Resources>
  7.         <ResourceDictionary>
  8.             <x:String x:Key="currentDateTime">Not actually a DateTime</x:String>
  9.         </ResourceDictionary>
  10.     </ContentPage.Resources>
  11.     <StackLayout>
  12.         <Label Text="StaticResource on Label.Text:"
  13.                VerticalOptions="EndAndExpand"
  14.                FontSize="Medium" />
  15.  
  16.         <Label Text="{StaticResource currentDateTime}"
  17.                VerticalOptions="StartAndExpand"
  18.                HorizontalTextAlignment="Center"
  19.                FontSize="Medium" />
  20.         <Label Text="DynamicResource on Label.Text:"
  21.                VerticalOptions="EndAndExpand"
  22.                FontSize="Medium" />
  23.         <Label Text="{DynamicResource currentDateTime}"
  24.                VerticalOptions="StartAndExpand"
  25.                HorizontalTextAlignment="Center"
  26.                FontSize="Medium" />
  27.  
  28.         <Label Text="StaticResource on Span.Text:"
  29.                VerticalOptions="EndAndExpand"
  30.                FontSize="Medium" />
  31.         <Label VerticalOptions="StartAndExpand"
  32.                HorizontalTextAlignment="Center“
  33.                FontSize="Medium">
  34.             
  35.                 
  36.                     {StaticResource currentDateTime}" />
  37.                 
  38.             
  39.         
  40.  
  41.         
  42.         
    阅读(2653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~