Home > database >  How to reference/use Resource Dictionary between multiple projects inside solution
How to reference/use Resource Dictionary between multiple projects inside solution

Time:09-29

I am building a WPF desktop app with .net framework 4.7.2. and need some help for merging resource dictionaries.

I have 1 solution with 2 projects:

Solution structure

  1. Main parent project (ProcesingDesktopHub) which only has a nav bar and depending on user click its injecting a view/viewmodel from child project (DopisiUzPovrate).

Inside the parent project I have created a ResourceDictionary.xaml with some colors etc. and I want to be able to propagade that dictionary to the child project so it can be used if I run the child project separately from the parent (testing, designing etc.)

I have tried putting this inside the child but it does not work:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://ProcesingDesktopHub:,,,/ProcResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

I hope I described my question correctly.

CodePudding user response:

A solution cannot have circular references between projects.
Since the parent project refers to the child, then, accordingly, the child cannot refer to the parent in any way.
One of the solutions: move the shared resources (if you need not only resources, but also types, controls, etc.) to a third project that will be referenced by the parent and child projects.

Second variant: if you do not provide for the use of the child project outside the parent project, then you can use the design-time resource creation technique described here: https://stackoverflow.com/a/17712041/13349759
And in runtime, the resources of the parent project will be used.

Design time sounds good but I would need to run the parent project for debugging purposes each time if I understood it correctly.

If you want to separately debug a child project, then creating a third project with shared resources will be the best varaint. In addition, if you ALWAYS use the parent project only together with the child, then pay attention to the answer from @MuhammadSulaiman.

CodePudding user response:

Move ResourceDictionary.xaml to child project (i.e. DopisiUzPovrate), and the structure of resources in ProcesingDesktopHub (i.e. Application.xaml) would be like this

<Application>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/DopisiUzPovrate;component/Path/To/ResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <!-- Converters, Colors and styles --> 
        </ResourceDictionary>
    </Application.Resources>
</Application>

Update: In case you have one application (ex. ProcesingDesktopHub) and several modules (DopisiUzPovrate1, DopisiUzPovrate2, etc...), and all of these would use shared resources, then you have to create a SharedModule that hosts the shared resources and once you reference the ResourceDictionary.xaml in desktop application, it'd be available to the modules and application at runtime..

  • Related