WP7中怎样定义和使用资源(Resource)

来源:互联网 发布:玉兰与木兰知乎 编辑:程序博客网 时间:2024/06/06 02:53
1. 系统资源。 在wp7开发中可以看到很多使用系统资源(resource)的例子,例如默认的新page: span style = font-size:18px; TextBlock x:Name = PageTitle Text = PageTitle Margin = 9,-7,0,0 Style = {StaticResourcePhoneTextTitle1Style} / / span 这里的PhoneTextTitle1Style便是资源。 系统的资源定义在这里:C:\Prog
  

  1. 系统资源。

  在wp7开发中可以看到很多使用系统资源(resource)的例子,例如默认的新page:

  1. <span style="font-size:18px;"><TextBlock x:Name="PageTitle" Text="PageTitle" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/></span>  

  这里的PhoneTextTitle1Style便是资源。

  系统的资源定义在这里:C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Design , 根据安装路径不同,这个路径有一些差异。

  这个路径下有DarkBlue、DarkBrown、LightBlue等文件夹,分别对应手机上选定的主题色+高亮色。

  DarkBlue为例,.\DarkBlue\ThemeResources.xaml 中PhoneTextTitle1Style是这样定义的:

  1. <span style="font-size:18px;"><Style x:Key="PhoneTextTitle1Style" TargetType="TextBlock" BasedOn="{StaticResource PhoneTextBlockBase}">  
  2.   <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiLight}"/>  
  3.   <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeExtraExtraLarge}"/>  
  4. </Style>  
  5. </span>  

  PhoneFontFamilySemiLight 和 PhoneFontSizeLarge 的定义

  1. <span style="font-size:18px;"><FontFamily x:Key="PhoneFontFamilySemiLight">Segoe WP SemiLight</FontFamily>  
  2. </span>  
  1. <span style="font-size:18px;"><System:Double x:Key="PhoneFontSizeExtraExtraLarge">72</System:Double></span>  

  2. 自定义资源

  资源可以定义在3个地方:Page.xaml, App.xaml 及单独的资源辞典文件中。

  3个地方定义没什么差别,这是使用时存在细微差别。

  以页面中定义为例子:

  1. <span style="font-size:18px;">    <phone:PhoneApplicationPage.Resources>  
  2.         <Color x:Key="myForce" >Red</Color>  
  3.         <Color x:Key="myBack" >GreenYellow</Color>  
  4.         <system:String x:Key="myString" >12345</system:String>  
  5.         <SolidColorBrush x:Key="myForceBrush" Color="Orange" />  
  6.         <SolidColorBrush x:Key="myBackBrush" Color="{StaticResource myBack}" />  
  7.     </phone:PhoneApplicationPage.Resources></span>  

  system是命名空间,要使用它需要在前面加上一行

  1. <span style="font-size:18px;">    xmlns:system="clr-namespace:System;assembly=mscorlib"  
  2. </span>  

  3. Xaml中使用资源

<span style="font-size:18px;"><TextBlock x:Name="ApplicationTitle" Text="{StaticResource myString}" Foreground="{StaticResource myForceBrush}" 
Style=
"{StaticResource PhoneTextNormalStyle}"/></span>

  4. MainPage.Xaml.cs文件中使用资源

  1. <span style="font-size:18px;"if (this.ApplicationBar == null)  
  2.  {  
  3.      this.ApplicationBar = new ApplicationBar();  
  4.      this.ApplicationBar.MenuItems.Add(new ApplicationBarMenuItem() { Text = "Menu1" });  
  5.  }  
  6.   
  7.  this.ApplicationBar.BackgroundColor = (Color)this.Resources["myBack"];  
  8.  this.ApplicationBar.ForegroundColor = (Color)Application.Current.Resources["PhoneAccentColor"];</span>  

  其实"PhoneAccentColor"可以通过this.Resources 加载到,但反之不行,因为资源"myBack"是定义在Page中的,Application类中不可见

  1. <span style="font-size:18px;">this.ApplicationBar.ForegroundColor = (Color)this.Resources["PhoneAccentColor"];</span>  

Silverlight全局(App.xaml)和局部样式汇总


1、内联方式--即直接在控件内部利用其属性进行设置

<UserControl x:Class="RemoveTextBoxBorder.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="75" Width="125" BorderBrush="Green" Foreground="Blue" />
    </Grid>
</UserControl>

  2、全局方式--在 App.xaml Resources 文件中进行定义

  当你在VS2008中创建Silverlight项目中,你会得到一个名叫 "App.xaml"的文件,此文件格式如下:

 <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="RemoveTextBoxBorder.App"
             >
    <Application.Resources>
    </Application.Resources>
</Application>

  在此文件中你可以定义自己的样式,定义样式采用如下格式:

 <Style x:Key="样式名" TargetType="样式所针对的控件类型"> 
   <Setter Property="控件属性名" Value="控件属性值" />
 </Style>

  在本示例中我们在此文件加入如下样式定义,加入后的App.xaml文件内容如下

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="RemoveTextBoxBorder.App"
             >
    <Application.Resources>
        <Style x:Key="ButtonStyleOne" TargetType="Button">
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="BorderThickness" Value="4,4,4,4" />
        </Style>
        <Style x:Key="ButtonStyleTwo" TargetType="Button">
            <Setter Property="BorderBrush" Value="Blue" />
            <Setter Property="Foreground" Value="Green" />
        </Style>
    </Application.Resources>
</Application>

  然后我们在控件的XAML文件中引用所定义的样式

<UserControl x:Class="RemoveTextBoxBorder.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="75" Width="125" Style="{StaticResource ButtonStyleOne}"  />        
    </Grid>
</UserControl>

 

  3、在 C#代码中动态设置控件样式

  为了在代码中引用此控件,我们必须要为此控件命名,在此我们命名为"MyButton"

  <Button x:Name="MyButton" Content="Button" Height="75" Width="125" />    

  然后在Page.xaml的code-behind 文件中,在其构造函数中加入如下代码:

  MyButton.Style = Application.Current.Resources["ButtonStyle"] as Style;

  后台代码如下:

  public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            MyButton.Style = Application.Current.Resources["ButtonStyleOne"] as Style;
        }
    }

 

 

字体-----------------------------------------------------

 

1 在Silverlight项目上单击右键,选择“添加”——“现有项”,在此将您要使用的字体文件添加到项目中;在这里,我添加一个华文行楷体文件,STXINGKA.TTF,字体的英文名称(可以到注册表中查)为STXingkai
2 在添加好的字体文件上右键单击,选择“属性”;

    从这里开始,有两种做法:

一 将“复制到输出目录”属性值改为“如果较新则复制”,将“生成操作”属性值改为“内容”;使用字体时,代码如下:

格式:  /字体文件名#字体英文名
例如:<TextBlock FontFamily="/STXINGKA.TTF#STXingkai" Text="尚未加载章节信息"/>

二 将“复制到输出目录”属性值改为“不复制”,将“生成操作”属性值改为“Resource”;使用字体时,代码如下:

格式:  字体文件名#字体英文名
例如:<TextBlock FontFamily="STXINGKA.TTF#STXingkai" Text="尚未加载章节信息"/>


原创粉丝点击