Search code examples
xamluwpstylesresourcedictionaryclass-library

UWP styles and themes in a nuget cannot find resource file


I have a UWP project and a class library for theme dictionary and some control default styles all merged into 1 file called "Themes.xaml" and I reference to that project and simply import it in "app.xaml" like so :

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GI.UI/Themes.xaml" />
            <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

and it works as expected but when I create a nuget with the same styles project and use that instead, then project throws an exception it is unable to locate Themes.xaml at this path.

Update 1

I also tried adding an empty code behind file with "InitializeComponent()" for the ResourceDictionary "Themes.xaml" and it works perfectly when I import in the way showed below, but again it only works when I reference the project and doesn't work with nuget.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <theme:Themes />
            <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

(Themes.xaml.cs) Resource Dictionary code behind.

using Windows.UI.Xaml;

namespace GI.UI
{
    public sealed partial class Themes : ResourceDictionary
    {
        public Themes() => InitializeComponent();
    }
}

Themes.xaml

<ResourceDictionary
x:Class="GI.UI.Themes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:BelowWindows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:Windows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<!-- some styles and themes here -->
</ResourceDictionary>

Update 2

Noticed this warning of an exception by xaml compiler .

enter image description here

So Now I have the code of that nuget project along with almost empty sample app, feel free to reference from project and then try referencing from nuget package as well.

project url : https://github.com/touseefbsb/UWP_Styles_Nuget

nuget package : https://www.nuget.org/packages/GI.UI/0.0.16

I have also given the nusepc file in the project which I used to create the package.

Note

When I create the nuget file I get following warning

enter image description here

enter image description here


Solution

  • I was able to solve the issue by following the folder structure as shown below :

    enter image description here

    and then editing nuspec file the following way :

    enter image description here