maui: Merging ResourceDictionary from referenced assembly fails to compile

Description

I want to create ResourceDictionary in separate project, and reuse it in other projects, but when I try to add it to application resources, I getting compilation error. I’ve created simple .NET MAUI app (Main app), added another project as MAUI Class Library (Library project) and added reference to it to Main app. In Class Library, created custom ResourceDictionary file (CustomDictionary.xaml), added some styles to it. After that, to App.xaml I’ve added following to my application:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/SharedProject;component/CommonResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

But after compilation, I’m getting error “Invalid URI: Invalid port specified.” Even after I’ve changed it to pack://application:80,,,... still the same error. Same if adding this in App constructor: Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/SharedProject;component/CommonResources.xaml") });

Steps to Reproduce

  1. Create new MAUI app
  2. Add new MAUI Class Library and reference of it to App project
  3. Add ResourceDictionary.xaml to Class Library project
  4. Add this resource dictionary to App.xaml like this <ResourceDictionary Source="pack://application:,,,/TRGT.Core.Platform;component/CommonResources.xaml"/>
  5. Try to compile
  6. You getting error “Invalid URI: Invalid port specified.”

Version with bug

Preview 12 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

All

Did you find any workaround?

No response

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 26 (15 by maintainers)

Most upvoted comments

I think you just add it using the type name. For example, assume you have some dictionary MyResources, then you can just add it:

<Application xmlns:mine="assembly include here">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <mine:MyResources />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

I tried 4398.zip from @XamlTest and was able to reproduce the issue in Visual Studio for Mac 17.5.6 (build 3). As @cmonto suggested, referencing a class from the SharedProject1 makes it work, but only if I use

<ResourceDictionary Source="CommonResources.xaml;assembly=SharedProject1" />

(the sample provided build just fine on mac, using dotnet build)