Reference ResourceDictionary in UWP Class Library

邮差的信 提交于 2019-12-05 06:02:26

问题


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?

|improve this question

回答1:


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.



来源:https://stackoverflow.com/questions/44509715/reference-resourcedictionary-in-uwp-class-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!