注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

wp8资源复用  

2013-05-25 11:40:48|  分类: windows phone |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Understanding Resources The first level of consistency has to do with sharing common resources. When creating an application, you often will want to use common colors or brushes across the application. Silverlight allows you to create objects to be used in more than one area by specifying them in a Resources section and identifying the object with an x:Key attribute. For example, you could define a SolidColorBrush in a Resources section like so:
<Grid x:Name="LayoutRoot">
  <Grid.Resources>
    <SolidColorBrush x:Key="mainBrush"
                     Color="Blue" />
  </Grid.Resources>
...
</Grid>
Every class that derives from FrameworkElement (which means most XAML elements) supports a collection of Resources. We can refer to these named elements using the StaticResource markup extension, like so:
<Grid x:Name="LayoutRoot">
  <Grid.Resources>
    <SolidColorBrush x:Key="mainBrush"
                      Color="Blue" />
  </Grid.Resources>
  <TextBlock Foreground="{StaticResource mainBrush}"
             Text="Hello World" />
...
</Grid>
The StaticResource markup extension tells the XAML parser to replace the foreground with the main brush. You can use the resource in several places, which isolates it from changes so that later, when you change the main brush to a LinearGradientBrush, it cascades down to wherever the StaticResource was used. The StaticResource markup extension looks up through the XAML document to find the resource with the correct name (through the hier- archy), and will continue beyond the beginning of the XAML document. Above the XAML document is the App.xaml file in the phone application project. Normally this is where you would place any application-wide resources. For example, if the App.xaml file looked like this:
ptg999
 84 Chapter 3: XAML Overview
<Application x:Class="PhoneControls.App"
             xmlns="..."
             xmlns:x="..."
             xmlns:phone="..."
             xmlns:shell="...">
  <!--Application Resources-->
  <Application.Resources>
    <SolidColorBrush x:Key="mainBrush"
                     Color="Blue" />
  </Application.Resources>
...
</Application>
the mainBrush would then be defined at the application level so that any XAML document that wanted to use the brush could do so, like so:
<Grid x:Name="LayoutRoot">
  <TextBlock Foreground="{StaticResource mainBrush}"
             Text="Hello World" />
...
</Grid>
Although this example shows a brush (which is a very commonly shared resource), it is not limited to only brushes. Any creatable object can be used in this way. In addition, when you want to share these resources across projects you can accomplish this with ResourceDictionary objects. Resource dictionaries are XAML files that contain shared resources that can be imported into App.xaml using merged dictionaries. These diction- aries can be flat XAML files or can be contained in separate assemblies that are referenced to your phone application. For more information on merged dictionaries, see the documentation.
  评论这张
 
阅读(562)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017