3

We have a UWP app using Template 10. Resources are in a UWP class library in the same solution. When we run the app we get the error

{Windows.UI.Xaml.Markup.XamlParseException:Cannot locate resource from ‘ms-resource:///Files/Styles\ButtonStyle.xaml’.

In App.xaml we have

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Styles\Custom.xaml" />
    <ResourceDictionary Source="Styles\ButtonStyle.xaml"/>
    <ResourceDictionary Source="Styles\ListsStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

We followed guidance in

Windows 10 Universal Merged Dictionaries

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ms-appx:///Styles/Custom.xaml" />
    <ResourceDictionary Source="ms-appx:///Styles/ButtonStyle.xaml"/>
    <ResourceDictionary Source="ms-appx:///Styles/ListsStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

then the error is

{Windows.UI.Xaml.Markup.XamlParseException: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type ...

We also tried the solution in ResourceDictionary in separate library

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/LibraryName;component/Styles/Custom.xaml" />
    <ResourceDictionary Source="pack://application:,,,/LibraryName;component/Styles/ButtonStyle.xaml"/>
    <ResourceDictionary Source="pack://application:,,,/LibraryName;component/Styles/ListsStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>

then the error is

{Windows.UI.Xaml.Markup.XamlParseException: Cannot locate resource from pack://application:,,,/LibraryName;component/Styles/Custom.xaml’

How do we reference a ResourceDictionary in a UWP Class Library?

Community
  • 1
  • 1
Vague
  • 2,198
  • 3
  • 18
  • 46

1 Answers1

8

As @Justin has pointed out, the problem here is that you forgot to add the Class Library Name in your URI. Let's assume you have a UWP Class Library named "ClassLibrary1" in your solution. Then you can merge them like:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ClassLibrary1/Styles/Custom.xaml" />
        <ResourceDictionary Source="ClassLibrary1/Styles/ButtonStyle.xaml"/>
        <ResourceDictionary Source="ClassLibrary1/Styles/ListsStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Or

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ms-appx:///ClassLibrary1/Styles/Custom.xaml" />
        <ResourceDictionary Source="ms-appx:///ClassLibrary1/Styles/ButtonStyle.xaml"/>
        <ResourceDictionary Source="ms-appx:///ClassLibrary1/Styles/ListsStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Also don't forget to add "ClassLibrary1" into your main project's References.

Besides, if your class library is not in the same solution, then you will need to check the "Generate library layout" option in the Build configuration under the class library's Properties page.

Because in WinRT environment, the resources are no longer embedded in the assembly but are placed next to the dll as content. So we need to generate library layout so that we can reference the dll in other project conveniently. For more info, please see my previous answer.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49