Freigeben über


How to link style/resource dictionaries located in different assemblies in WinRT and SilverLight

If you're architecting a modern application for Windows or Windows Phone for reuse or even for performance, you'll quickly realize that putting everything in to the main application project isn't the wisest of ideas.  When you start separating your views/controls into modules (separate projects), you'll need to move your common styles into a separate resource file which can be referenced by and shared by all the various libraries you create instead of in the global App.xaml file. If you don't, your application will actually still execute, but you won't be able to see any of your custom styles in the designer view.  So you create yourself the resource file in your common assembly but then how do you reference/link that resource file to your App.xaml file?  I personally haven't been able to figure out how to do this in Blend so I just do it by hand.

If your solution is a WinRT solution, you’ll need to follow this convention:

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

For a SilverLight Windows Phone solution, you’ll need to follow this convention:

/{ASSEMBLY_NAME};component/{RESOURCE_FILE_PATH}
i.e. /Contoso.Core;component/Resources/CoreStyles.xaml

 

Full samples of the App.xaml file as follows:

WinRT

 

 <Application
 x:Class="Contoso.UI.App"
 xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="https://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>
 
 

SilverLight

 <Application
 x:Class="Contoso.UI.App"
 xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="https://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>