.netnugetmauiapp.xaml

How to include a style from a Nuget package inside the .NET Maui App.xaml?


I have a nuget package that contains some general styles that are used company wide. And I have a .NET Maui application where I want to use these styles.

The project of the nuget package looks like this:

enter image description here

MyStyle.xaml contains a Ressource dictionary that merges different styles together in a single dictionary.

In my .NET MAUI App.xaml I tried adding the dictionary like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MyPackage.UI;component/Styles/MyStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

When I try building the project I get the error:

Invalid URI: Invalid port specified.

Am I doing something wrong? I thought pack://application:,,, should give me the correct port.


Solution

  • How to include a style from a NuGet package inside the .NET Maui App.xaml?

    About how to use a style from NuGet, you can refer to this case: .NET MAUI - Styles packed in a MAUI Library are not available for InteliSense after released as nuget. The question section described usage for NuGet.

    In addition, you can see the official doc: Merge resource dictionaries from other assemblies.

    Hope these can help you.