Home > database >  ResourceDictionary as NuGet Package
ResourceDictionary as NuGet Package

Time:02-24

I'm currently trying to create sort of a XAML "Style collection" which we can use in all of our UI projects, to achieve consistency in our visuals.

I've tried creating a class library and added a ResourceDictionary with my style:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ToggleSwitch" TargetType="{x:Type CheckBox}">
        Some style in here
    </Style>
</ResourceDictionary>

Enabled building the NuGet Package in the build options and added it to another UI project. I just can't quite get my head around about how to actually use the style in the UI project now.

Is this even a valid approach? Googling this specific intend didn't get me any reasonable results.

CodePudding user response:

That is a valid approach and it is no different from creating and consuming resource dictionaries locally, except for how you reference them if they are located in a referenced assembly.

As an example for the application resources for an assembly YourReferencedAssembly.dll and a resource dictionary YourStyleResourceDictionary.xaml directly within the assembly.

<Application.Resources>
   <ResourceDictionary>
      <!-- ...other resources. -->
      <ResourceDictionary.MergedDictionaries>
         <!-- ...other resource dictionaries. -->
         <ResourceDictionary Source="pack://application:,,,/YourReferencedAssembly;component/YourStyleResourceDictionary.xaml" />
      </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Application.Resources>

The source path is a so-called pack URI and can take various forms. You can refer to the resource file pack URI documentation. This is how the path for resources in referenced assemblies looks in detail:

Authority: application:///.

Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format:

AssemblyShortName{;Version]{;PublicKey];component/Path

AssemblyShortName: the short name for the referenced assembly.

;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.

;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.

;component: specifies that the assembly being referred to is referenced from the local assembly.

/Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.

Implicit styles, as usual, are automatically applied in the scope where the resource dictionary is added and explicit styles are referenced either using StaticResource or DynamicResource. There is no difference where the resource dictionary comes from, the same assembly or a different one.

  • Related