Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To: Share Resource Dictionaries in .Net Libraries #4

Open
jingkecn opened this issue Dec 3, 2019 · 0 comments
Open

How To: Share Resource Dictionaries in .Net Libraries #4

jingkecn opened this issue Dec 3, 2019 · 0 comments

Comments

@jingkecn
Copy link
Owner

jingkecn commented Dec 3, 2019

Problematic

In .Net development,resource dictionaries are a common way to define objects that we expect to use more than once, such as strings, styles, sizes, etc.
And it is no secret that resources defined in App.xaml of the startup project are shared for all assemblies.
However, when referring to a resource in non-startup project, Visual Studio does not manage to do code navigation and will launch a Resource '*' is not found warning at design time, even if it will work at runtime after deployment, it is not quite practical without the ability of code navigation.

Solution

What Microsoft does not tell us is that an App.xaml of the local library project can be used for design time.
So we just have to add an App.xaml into the library project, and merge the common resource dictionaries in this file as we do for the startup project:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <!-- Libraries -->
        <ResourceDictionary Source="ms-appx:///${COMMON_ASSEMBLY_NAME}/${PATH_TO_RESOURCE}.xaml" />
        <!-- Other merged dictionaries here -->
      </ResourceDictionary.MergedDictionaries>
      <!-- Other app resources here -->
    </ResourceDictionary>
  </Application.Resources>
</Application>

Where:

  • ${COMMON_ASSEMBLY_NAME} is the common library where to put the shared resources.
  • ${PATH_TO_RESOURCE} is an unified access of the shared resources.
+ solution
  + resource.common.lib
    + resource.entry.xaml
  + lib.refer.to.common.resource
    + app.xaml
...
@jingkecn jingkecn changed the title How to: Share Resource Dictionaries in .Net Libraries How To: Share Resource Dictionaries in .Net Libraries Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant