xaml - UWP 类库中的引用资源字典

标签 xaml uwp uwp-xaml

我们有一个使用模板 10 的 UWP 应用。资源位于同一解决方案中的 UWP 类库中。当我们运行应用程序时,我们得到了错误

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

在 App.xaml 中我们有

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

我们遵循了

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>

那么错误是

{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 ...

我们还尝试了 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>

那么错误是

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

我们如何在 UWP 类库中引用 ResourceDictionary?

最佳答案

正如@Justin 所指出的,这里的问题是您忘记在URI 中添加类库名称。假设您的解决方案中有一个名为“ClassLibrary1”的 UWP 类库。然后你可以像这样合并它们:

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

或者

<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>

别忘了将“ClassLibrary1”添加到您的主项目的References中。

另外,如果你的类库不在同一个解决方案中,那么你需要在类下的Build配置中勾选"Generate library layout"选项图书馆的属性页面。

因为在 WinRT 环境下,资源不再嵌入到程序集中,而是作为内容放置在 dll 旁边。所以我们需要生成库布局,以便我们可以方便地引用其他项目中的dll。更多信息,请参阅 my previous answer .

关于xaml - UWP 类库中的引用资源字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44509715/

相关文章:

c# - UWP Xaml Textblock 数据绑定(bind) - 即使更新属性,UI 也不更新

events - 如何收听 UWP Xaml Slider 操作开始/结束事件?

xaml - 弹出窗口中应用栏按钮的单击事件在 UWP 中不起作用

wpf - WPF按钮中的文本内容未垂直居中

c# - 通过 xaml 设置用户控件自定义依赖属性

c# - Windows.UI.Xaml.dll 中的应用程序内部异常

c# - UWP:将 InlineUIContainer 添加到超链接失败

c# - 如何通过提供 Windows.Media.FaceAnalysis DetectedFace 列表使用 Microsoft Cognitive 服务检测人脸属性?

entity-framework - 用户无法从 UWP 应用程序中选择 SQLite 数据库位置

C++ 和 XAML - 来自文本框的数字输入