一般咱們在類庫中定義資源的時候能夠在Theme/Generic.xaml中定義,當類庫加載的時候,會自動加載Generic.xaml文件中的資源,一般用在控件庫中,但若是控件多了以後,全部的Style都定義在Generic.xaml中會是的文件很大很臃腫,這時候咱們能夠經過Uri的方式引用外部的資源,把資源定義在多個文件中,能夠更方便於管理,在程序集中定義引用外部ResourceDictionary比較特殊,方法以下shell
WinRT: app
ms-appx:///{ASSEMBLY_NAME}/{RESOURCE_FILE_PATH}
如:ms-appx:///Contoso.Core/Resources/CoreStyles.xamlui
Sliverlightspa
/{ASSEMBLY_NAME};component/{RESOURCE_FILE_PATH}
如:/Contoso.Core;component/Resources/CoreStyles.xamlcode
下面爲完整Democomponent
一、WinRTxml
<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>
二、Silverlightblog
<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>
參考連接:資源