• 如何使用和定义字典资源(ResourceDictionary)


    1、首先需要创建一个资源字典的文件,也就是一个xaml的文件。

    文件的语法格式如下:Design.xaml

     1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     2                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     3                     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
     4                     xmlns:system="clr-namespace:System;assembly=mscorlib"
     5                     xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
     6                     xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls">
     7 
     8     <!--如果想预览界面效果,把一下代码加到页面里。不加的话在运行时匹配.-->
     9     <!--<phone:PhoneApplicationPage.Resources>
    10         <ResourceDictionary>
    11             <ResourceDictionary.MergedDictionaries>
    12                 <ResourceDictionary Source="../Design/Design.xaml"></ResourceDictionary>
    13             </ResourceDictionary.MergedDictionaries>
    14         </ResourceDictionary>
    15     </phone:PhoneApplicationPage.Resources>-->
    16 
    17     <!--(主)字体颜色-->
    18     <SolidColorBrush x:Key="Host_Color" Color="Black"/>
    19 
    20     <!--(所有页面)正在加载提示-->
    21     <Style TargetType="TextBlock" x:Key="LoadingTextBlock">
    22         <Setter Property="FontSize" Value="25"/>
    23         <Setter Property="Text" Value="正在加载"/>
    24         <Setter Property="Foreground" Value="#FF476FBF"/>
    25     </Style>
    26     <Style TargetType="ProgressBar" x:Key="LoadingProgressBar">
    27         <Setter Property="IsIndeterminate" Value="True"/>
    28         <Setter Property="Width" Value="320"/>
    29         <Setter Property="Margin" Value="-20,0,0,-15"/>
    30         <Setter Property="Foreground" Value="#FF476FBF"/>
    31     </Style>
    32 
    33     <!--定义样式资源-->
    34     <Style TargetType="TextBlock">
    35         <Setter Property="Foreground" Value="{StaticResource Host_Color}"/>
    36         <Setter Property="VerticalAlignment" Value="Center"/>
    37         <Setter Property="HorizontalAlignment" Value="Center"/>
    38     </Style>
    39 
    40 </ResourceDictionary>

    Style的x:Key属性是资源字典里面的资源的唯一的标示符,也是作为在其他页面调用的一个唯一的Key来进行调用。

    2、调用资源资源中的资源

    在MainPage.xaml页面中添加资源字典,语法如下

    1 <phone:PhoneApplicationPage.Resources>
    2      <ResourceDictionary>
    3           <ResourceDictionary.MergedDictionaries>
    4                 <ResourceDictionary Source="Design.xaml"/>
    5           </ResourceDictionary.MergedDictionaries>
    6       </ResourceDictionary>
    7  </phone:PhoneApplicationPage.Resources>

    注意:Source的路径是指当前使用Design.xaml文件的页面,相对Design.xaml的位置;例如
    ----文件夹View

          ----文件Page.xaml

    ----Design.xaml

    如果文件Page.xaml要使用Design.xaml,Source="../Design.xaml"

    ResourceDictionary.MergedDictionaries   获取 ResourceDictionary 字典的集合,这些字典构成了合并字典中的各种资源字典。 

    如果想在程序启动时加载所有的资源,可以再App.xaml页面上添加资源的加载,语法如下

     1 <Application
     2     x:Class="DataVisualizationOnWindowsPhone.App"
     3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     5     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
     6 
     7 
     8 
     9     <Application.Resources>
    10         <!-- 添加资源 -->
    11         <ResourceDictionary>
    12             <ResourceDictionary.MergedDictionaries>
    13                 <ResourceDictionary Source="Design.xaml"/>
    14             </ResourceDictionary.MergedDictionaries>
    15         </ResourceDictionary>
    16     </Application.Resources>
    17 
    18 ……
    19 
    20 </Application>

    3、使用字典资源中的资源

    在MainPage.xaml页面中的控件调用自定义的资源,语法如下

    调用字典资源中x:Key值为LoadingTextBlock的样式资源

    1 <TextBlock Text="Some Text" Style="{StaticResource LoadingTextBlock}"/>

    在cs页面调用字典资源,语法如下

    1 ControlTemplate template;
    2 template = Application.Current.Resources["ControlTemplateL"] as ControlTemplate;
    3 MyCtrl.Template = template;

    参考

    MSDN的Silverlight资源字典详细介绍

    http://msdn.microsoft.com/zh-cn/library/cc903952(v=VS.95).aspx

  • 相关阅读:
    自定义CollectionView实现流式布局和动态图片的展现
    Java设计模式之观察者模式
    HashMap工作原理
    SpringBoot 实现多线程
    十大排序算法
    IDEA集成 plant uml 画图工具
    解决国内访问github速度慢的问题
    SpringBoot整合JWT Token
    SpringBoot在idea中配置热部署
    Spring-Security教程【一】简单的登录认证
  • 原文地址:https://www.cnblogs.com/qq278360339/p/2565454.html
Copyright © 2020-2023  润新知