【问题标题】:UWP styles and themes in a nuget cannot find resource filenuget 中的 UWP 样式和主题找不到资源文件
【发布时间】:2020-06-28 13:19:16
【问题描述】:

我有一个 UWP 项目和一个主题字典的类库,一些控件默认样式都合并到一个名为“Themes.xaml”的文件中,我引用了该项目并简单地将其导入“app.xaml”,如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GI.UI/Themes.xaml" />
            <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

它按预期工作,但是当我创建一个具有相同样式的项目并使用它时,项目会引发异常,它无法在此路径中找到 Themes.xaml。

更新 1

我还尝试为 ResourceDictionary“Themes.xaml”添加一个带有“InitializeComponent()”的空代码隐藏文件,当我按照下面显示的方式导入时它可以完美运行,但它仅在我引用项目时才有效,并且不适用于 nuget。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <theme:Themes />
            <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

(Themes.xaml.cs) 后面的资源字典代码。

using Windows.UI.Xaml;

namespace GI.UI
{
    public sealed partial class Themes : ResourceDictionary
    {
        public Themes() => InitializeComponent();
    }
}

Themes.xaml

<ResourceDictionary
x:Class="GI.UI.Themes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:BelowWindows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractNotPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:Windows10version1809="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<!-- some styles and themes here -->
</ResourceDictionary>

更新 2

注意到 xaml 编译器的异常警告。

所以现在我有了那个 nuget 项目的代码以及几乎空的示例应用程序,请随意从项目中引用,然后也尝试从 nuget 包中引用。

项目网址https://github.com/touseefbsb/UWP_Styles_Nuget

nuget 包https://www.nuget.org/packages/GI.UI/0.0.16

我还在项目中提供了 nusepc 文件,用于创建包。

注意

当我创建 nuget 文件时,我收到以下警告

【问题讨论】:

  • 你的意思是当你在主项目中安装类库创建的nuget包并尝试引用Themes.xaml时,它抛出找不到Themes.xaml?另外,能否提供一个简单的可以复制的样本供我们测试?
  • 是的,这正是发生的事情,我会制作一个样本并提供一个
  • @Faywang-MSFT 请查看该问题的更新 2 部分,非常感谢,谢谢
  • 我注意到没有文件名Theme.xaml,在包里我看到Themes.xaml
  • @LanceMcCarthy 是的,我会用新的文件名更新我的问题,但文件名不是问题,它不能与 theme.xaml 一起使用,也不能与 theme.xaml 一起使用参考名称是正确的为什么当我引用项目本身时它可以完美运行。

标签: xaml uwp styles resourcedictionary class-library


【解决方案1】:

你可以尝试这样使用:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///GI.Standards.Theme/Theme.xaml" />
            <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

使用 ms-appx 引用来自应用程序包的文件。

Source="ms-appx:///LibraryName/Folder/File.xaml"

【讨论】:

  • 我用你的 nuget 包做了一个小测试。我无法运行示例应用程序,但我有一些想法。当您编译包含 xaml 文件的类库时,它会转换为 xbf 文件,您需要手动包含它。这里是参考:dschenkelman.github.io/2014/06/25/... 您也可以尝试删除 GI.UI 中的点,希望对您有所帮助
  • 您提供的链接似乎不完整,您可以再发一次吗?
  • 我可能错了,一个类也可以正常工作,但是每当我为 UWP 和 WPF 编写 ResourceDictionaries 时,它们只是一个 XAML 文件(没有带 CTOR 的代码)。我只见过 Xamarin.Forms 需要类方法
【解决方案2】:

我可以通过如下所示的文件夹结构来解决这个问题:

然后按以下方式编辑 nuspec 文件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多