【问题标题】:Windows 10 Universal Merged DictionariesWindows 10 通用合并字典
【发布时间】:2024-01-15 17:32:02
【问题描述】:

所以我有一个 Windows 通用类库,其中有一个资源字典,我想在 App.xaml 中与我的 Windows 10 通用应用程序的主资源字典合并。

我的 App.xaml 只是从同一个程序集合并到我的主资源字典中。

<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

然后,从我的主资源字典 (Styles/Styles.xaml) 中,我正在合并来自同一个程序集的其他资源字典。这是我想从另一个程序集合并到资源字典中的地方:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Fields.xaml"/>
    <ResourceDictionary Source="DataTemplates.xaml"/>
    <!--<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />-->
    <!--<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />-->
    <ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />
</ResourceDictionary.MergedDictionaries>

我已尝试将其添加到我的主要资源字典中:

<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />

还有这个……

<ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />

基于this article 关于 Windows 8.x 应用商店应用,这似乎是它应该工作的方式。但是还是不行。

还有这个……

<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />

(这是 WPF 方式,我知道,但我想我还是会尝试一下!)

但似乎没有一个工作......

我的应用程序程序集中的资源字典的构建操作设置为“页面”。这些资源字典只是在合并中使用它:

<ResourceDictionary Source="Styles/Styles.xaml"/>

我收到以下神秘错误:

未能分配给属性 'Windows.UI.Xaml.ResourceDictionary.Source' 因为类型 'Windows.Foundation.String' 不能分配给类型 'Windows.Foundation.Uri'。 [行:12 位置:37]

【问题讨论】:

  • 您是否已将您的库添加为项目的参考?我正在使用&lt;ResourceDictionary Source="ms-appx:///MyLibrary/Resources.xaml"/&gt;,它似乎可以正常工作。
  • 如何将此资源字典添加到合并字典中?然后会发生什么?
  • 是的,添加 {AssemblyName} 作为对我的应用程序项目的引用 :)
  • 你们在另一个程序集中的资源字典上设置了什么构建操作?
  • 好的,看来它确实适用于您建议的 Romasz。我不知道怎么做,但我怀疑当我尝试构建操作/源 URI 的所有不同组合时,我必须在尝试该技术时将构建操作设置为 Page 以外的其他内容。

标签: xaml uwp


【解决方案1】:

正如 Romasz 在评论中提到的,您需要引用包含样式的项目。然后使用下面的代码来引用。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///UserControlLibs/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 在您给出的示例中,假设您希望 UserControlLibs 成为一个块状包。那么,如何合并资源字典呢?
【解决方案2】:

XAML 合并字典比看起来要复杂。如果您引用的是本地项目,那么一切都很好,那么您的 Source= 路径也很好。

如果您引用的是外部 DLL(不在解决方案中),则引用的 DLL 文件夹还必须包含所有 *.xml、*.xr.xml、*.xbf、*.jpg/ png/gif等。

我遵循的程序是: 1. 引用包含合并字典(XAML 样式表)的 DLL。 2. 确保参考路径包含所有必需的文件。 3. 将合并后的字典引用添加到您的 App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///NAMESPACE_HERE/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这个 PostBuild.bat 文件描述了我如何让 VS 在成功构建时复制所有必需的文件:

Echo Starting: PostBuildEvent: Call $(ProjectDir)PostBuild.Bat $(ProjectDir) $(OutDir) $(TargetPath) $(RootNameSpace)
Echo With Parameters: %1 %2 %3 %4

REM ***
REM *** Variables
REM ***
SET BuildLocationBin=..\..\..\..\..\..\..\..\bin

REM ***
Echo *** Publish to Bin
REM ***
MD %BuildLocationBin%
%WINDIR%\system32\attrib.exe %BuildLocationBin%\*.* -r /s
%WINDIR%\system32\xcopy.exe %1Properties\*.rd.xml %BuildLocationBin%\%4\Properties\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.png %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xbf %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xml %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %3 %BuildLocationBin%\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.pri %BuildLocationBin%\*.* /s/r/y

Echo *** Postbuild Complete ***

希望这会有所帮助!

【讨论】:

  • 我发现选中“生成库布局”,您可以在属性 | 中找到一个选项。构建,实际上将文件放置到正确的位置。
最近更新 更多