1

I have defined my resourceDictionary in a separate library as below

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cultures="clr-namespace:My_Localization.Cultures"
    xmlns:properties="clr-namespace:My_Localization.Properties">

    <ObjectDataProvider x:Key="Resources" ObjectType="{x:Type cultures:CultureResources}" MethodName="GetResourceInstance"/>

    <ObjectDataProvider x:Key="CultureResourcesDS" ObjectType="{x:Type cultures:CultureResources}"/>

</ResourceDictionary> 

I have used this library from another library as below (Header of xaml only)

<msRibbon:RibbonTab x:Class="Ribbon.Planner.PlannerRibbon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:msRibbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    mc:Ignorable="d" 
    d:DesignHeight="100" d:DesignWidth="500" Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}" 
    >
    <Grid>
     ...
     ...
     ...
    </Grid>

I have added the reference of My_Localization lib and I am only changing the header. All works fine but the only problem is that in design time I have "Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}" underlined. When I hover my mouse there is hint "The resource "Resources" could not be resolved"

Why is there an error like hint in my xaml? and then why does it all work fine?

My Solution structure

  1. MainExe - Contains app.xaml. I have merged the resource dictionary here. No problems in xaml since the merge dictionary exists in app.xaml
  2. My_Localization - Lib containing the resource dictionary (code above)
  3. Lib1 - References My_Localization and there are problems in xaml as explained
  4. Lib2 - References My_Localization and there are problems in xaml as explained
  5. Lib3 - References My_Localization and there are problems in xaml as explained
user2837961
  • 1,505
  • 3
  • 27
  • 67

3 Answers3

3

You need to provide a reference to the ResourceDictionary either in the app.xaml file or locally. Resources in app.xaml are available globally to all xaml files of the application.

For Xaml files in library projects the designer works a little differently.

At runtime it will be the app.xaml in the startup project that will be used for all assemblies. At designtime it will be the app.xaml of the local assembly. This means, you can add a app.xaml file to libraies, which will only be used by the Visual Studio Designer when rendering xaml files from that specific library (Set build action of file to Page).

To reference a ResourceDictionary do this:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Application.Resources>
        <ResourceDictionary>

            <!-- Other global resources -->

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ASSEMBLYNAME;component/FOLDERPATH/RDNAME.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Where ASSEMBLYNAME is the name of the assembly where the ResourceDictionary is (check properties of project).

Example:

Project with assembly name: "MyAssembly" and ResourceDictionary in folder path "Resources/RDs/MyRd.xaml"

Source="pack://application:,,,/MyAssembly;component/Resources/RDs/MyRd.xaml"
Anders
  • 1,590
  • 1
  • 20
  • 37
  • How can a add app.xaml to a library? Do you mean just add a class and call it app.xaml? without the app.cs? – user2837961 Jul 08 '15 at 08:16
  • 1
    Exactly. just the xaml part. If you add a ResourceDictionary named `app.xaml` to the root of the library project, and then change the xaml node to `` instead, then it will work. – Anders Jul 08 '15 at 08:36
  • My library is a class library hence gives an error on Application "The default namespace is not defined" – user2837961 Jul 08 '15 at 08:54
  • try adding this: `` – Anders Jul 08 '15 at 09:03
1

In App.xaml, you must add a MergedDictionary to the resources which will reference your other dictionary.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RibbonControlsLibrary;component/Your/Dictionary/Path.xaml" />
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Mike Eason
  • 9,525
  • 2
  • 38
  • 63
  • My application has atleast 5-6 libraries and in each I want to use My_Localization lib. Now these libraries do not contain app.xaml – user2837961 Jul 08 '15 at 07:21
  • @eranotzap Ah, I was not aware of that. OP, in which case, you should put your merged dictionary inside your `RibbonTab` Resources. – Mike Eason Jul 08 '15 at 07:22
  • @user2837961 have you done what Mike wrote ? How are you consuming your resources ..? – eran otzap Jul 08 '15 at 07:23
  • There is an easy solution when using class libraries and xaml. Check my answer. – Anders Jul 08 '15 at 07:50
1

You can include the ResourceDictionary in each xaml

<msRibbon:RibbonTab x:Class="Ribbon.Planner.PlannerRibbon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:msRibbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    mc:Ignorable="d" 
    d:DesignHeight="100" d:DesignWidth="500" Header="{Binding Path=Ribbon_About, Source={StaticResource Resources}}">
    <msRibbon:RibbonTab.Resources>
        <ResourceDictionary Source="pack://application:,,,/<ASSEMBLY_NAME>;component/<RESOURCES_FILE>.xaml"/>
    </msRibbon:RibbonTab.Resources>
    <Grid>
     ...
     ...
     ...
    </Grid>
Nacho
  • 962
  • 6
  • 14