【UWP】在不同类库使用ResourceDictionaries

跨程序集资源引用
本文介绍如何在WinRT和Silverlight应用中通过Uri方式从不同程序集中引用样式资源字典,实现资源文件的有效管理和分离。

  通常我们在类库中定义资源的时候可以在Theme/Generic.xaml中定义,当类库加载的时候,会自动加载Generic.xaml文件中的资源,通常用在控件库中,但如果控件多了之后,所有的Style都定义在Generic.xaml中会是的文件很大很臃肿,这时候我们可以通过Uri的方式引用外部的资源,把资源定义在多个文件中,可以更方便于管理,在程序集中定义引用外部ResourceDictionary比较特殊,方法如下

WinRT:  

  ms-appx:///{ASSEMBLY_NAME}/{RESOURCE_FILE_PATH}
    如:ms-appx:///Contoso.Core/Resources/CoreStyles.xaml

 

Sliverlight

  /{ASSEMBLY_NAME};component/{RESOURCE_FILE_PATH}
    如:/Contoso.Core;component/Resources/CoreStyles.xaml

 

 

下面为完整Demo

1、WinRT

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

    <!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ms-appx:///Contoso.Core/Resources/CoreStyles.xaml" />
                <ResourceDictionary Source="ms-appx:///Contoso.UI.Core/Resources/UICoreStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <!-- Other style definitions can still go here -->
            <Style TargetType="Button">
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

2、Silverlight

<Application
 x:Class="Contoso.UI.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Contoso.Core;component/Resources/CoreStyles.xaml"/>
                <ResourceDictionary Source="/Contoso.UI.Core;component/Resources/UICoreStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <!-- Other style definitions can still go here -->
            <Style TargetType="Button">
            </Style>

        </ResourceDictionary>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
 Launching="Application_Launching" Closing="Application_Closing"
 Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

 

 

参考链接:

  http://blogs.msdn.com/b/madenwal/archive/2015/02/09/how-to-link-style-resource-dictionaries-located-in-different-assemblies-in-winrt.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值