首页 > 其他 > 详细

【WP8】ResourceDictionary

时间:2014-03-23 08:54:41      阅读:489      评论:0      收藏:0      [点我收藏+]

WP8中引用资源字典

  当我们定义的样式太多的时候,我们可以把样式分别定义在不同的文件中,然后通过 MergedDictionaries 应用到其他资源字典中,看下面Demo

 

  我们可以把样式定义在多个文件中,然后再App.xaml中引用

我们先定义三个文件

  1、蓝色按钮资源文件,定义按钮的Foreground为Blue

bubuko.com,布布扣
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="Foreground" Value="Blue"></Setter>
    </Style>
    
</ResourceDictionary>
BlueButtonResource.xaml

  2、红色按钮资源文件,定义按钮的Foreground为Red

bubuko.com,布布扣
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="Foreground" Value="Red"></Setter>
    </Style>
    
</ResourceDictionary>
RedButtonResource.xaml

  3、全局资源文件(在App.xaml引用),这里只定义TextBlock为Teal

bubuko.com,布布扣
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Teal"></Setter>
    </Style>

</ResourceDictionary>
GlobalResourceDictionary.xaml

 

然后再App.xaml全局引用全局资源文件

bubuko.com,布布扣
    <!--应用程序资源-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--这里引用其他文件,当然,也可以设置多个ResourceDictionary-->
                <ResourceDictionary Source="GlobalResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            
            <!--这里定义其他样式-->
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="Teal"></Setter>
            </Style>
            
            <Style TargetType="Rectangle">
                <Setter Property="Fill" Value="{StaticResource PhoneAccentBrush}"></Setter>
            </Style>
        </ResourceDictionary>

        <!--其他样式也可以定义在这里-->
        <Style TargetType="TextBox">
            <Setter Property="Foreground" Value="Maroon"></Setter>
        </Style>
    </Application.Resources>
bubuko.com,布布扣

 

 前面蓝色和红色按钮的两个资源文件,可以用来当成不同的主题,我们可以在代码中动态加载

            ResourceDictionary resourceDictionary = new ResourceDictionary();
            //这里的Uri格式:/解决方案;component/资源文件路径
            Application.LoadComponent(resourceDictionary, new Uri("/LoadResourceDictionary;component/BlueButtonResource.xaml", UriKind.Relative));
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

 

bubuko.com,布布扣

【WP8】ResourceDictionary,布布扣,bubuko.com

【WP8】ResourceDictionary

原文:http://www.cnblogs.com/bomo/p/3618522.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!